【问题标题】:data transfer from ec2 to s3 bucket with time constraint [closed]具有时间限制的从 ec2 到 s3 存储桶的数据传输 [关闭]
【发布时间】:2021-06-17 17:36:22
【问题描述】:

我想知道是否有可能将数据从 ec2 传输到超过一个月的 s3。基本上,是否可以在数据传输过程中应用时间限制

【问题讨论】:

  • 是的,有可能。
  • @Marcin 是在文档中的某处吗?抱歉,我找不到它。
  • 不,不是。这需要自定义解决方案。

标签: amazon-web-services amazon-s3 amazon-ec2


【解决方案1】:

AWS CLI 没有内置此功能。

因此,您需要单独识别文件,然后调用 AWS CLI,或者您可以编写一个程序(例如在 Python 中)检查文件日期,然后使用 boto3 库将文件复制到 Amazon S3 .

【讨论】:

    【解决方案2】:

    我认为您不能直接从 AWS CLI 执行此操作,但您可以使用如下简单的 python 脚本

    import boto3
    import logging
    from botocore.exceptions import ClientError
    import time
    import os
    
    def put_object(file_name, bucket, object_name=None):
        # If S3 object_name was not specified, use file_name
        if object_name is None:
            object_name = file_name
    
        # Upload the file to s3
        s3_client = boto3.client('s3')
        try:
            response = s3_client.upload_file(file_name, bucket, object_name)
        except ClientError as e:
            logging.error(e)
            return False
        return True
    
    
    def upload_files(bucket,path):
        current_time = time.time()
        for file in os.listdir(path):
            creation_time = os.path.getctime(file)
            if (current_time - creation_time) // (24 * 3600) >= 30:
                put_object(file, bucket, object_name=None)
                print('{} moved to s3'.format(file))
                #os.unlink(file) <- if you need you can delete file from s3
    
    
    if __name__ == '__main__':
        bucket = "my-s3-bucket"
        path = "path/to/folder"
        upload_files(bucket,path)
    

    创建一个 cron 作业并在您喜欢的任何时间窗口中运行此脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 2016-04-24
      • 2018-05-05
      • 1970-01-01
      相关资源
      最近更新 更多