【问题标题】:How to filter s3 objects by last modified date with Boto3如何使用 Boto3 按上次修改日期过滤 s3 对象
【发布时间】:2020-03-27 07:25:37
【问题描述】:

有没有办法按 boto3 中的最后修改日期过滤 s3 对象?我已经构建了一个包含存储桶中所有内容的大型文本文件列表。一段时间过去了,我只想列出上次循环遍历整个存储桶后添加的对象。

我知道我可以使用Marker 属性从某个对象名称开始,所以我可以给它我在文本文件中处理的最后一个对象,但这并不能保证在该对象之前没有添加新对象姓名。例如如果文本文件中的最后一个文件是 Oak.txt 并且添加了一个名为 apple.txt 的新文件,则它不会选择该文件。

s3_resource = boto3.resource('s3')
client = boto3.client('s3')

def list_rasters(bucket):

    bucket = s3_resource.Bucket(bucket)

    for bucket_obj in bucket.objects.filter(Prefix="testing_folder/"):
        print bucket_obj.key
        print bucket_obj.last_modified

【问题讨论】:

    标签: python python-3.x amazon-web-services amazon-s3 boto3


    【解决方案1】:

    以下代码 sn -p 获取特定文件夹下的所有对象,并检查最后修改的文件是否在您指定的时间之后创建:

    用你的价值观替换YEAR,MONTH, DAY

    import boto3
    import datetime
    #bucket Name
    bucket_name = 'BUCKET NAME'
    #folder Name
    folder_name = 'FOLDER NAME'
    #bucket Resource
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(bucket_name)    
    def lambda_handler(event, context):
         for file in bucket.objects.filter(Prefix= folder_name):
             #compare dates 
             if (file.last_modified).replace(tzinfo = None) > datetime.datetime(YEAR,MONTH, DAY,tzinfo = None):
                 #print results
                 print('File Name: %s ---- Date: %s' % (file.key,file.last_modified))
    

    【讨论】:

      【解决方案2】:

      下面的代码 sn-p 将使用 s3 Object 类 get() 操作仅返回满足 IfModifiedSince 日期时间参数的那些。该脚本打印文件,这是原始问题,但也将文件保存在本地。

      import boto3
      import io
      from datetime import date, datetime, timedelta
      
      
      # Defining AWS S3 resources
      s3 = boto3.resource('s3')
      bucket = s3.Bucket('<bucket_name>')
      prefix = '<object_key_prefix, if any>'
      
      # note this based on UTC time
      yesterday = datetime.fromisoformat(str(date.today() - timedelta(days=1)))
      
      # function to retrieve Streaming Body from S3 with timedelta argument
      def get_object(file_name):
          try:
              obj = file_name.get(IfModifiedSince=yesterday)
              return obj['Body']
          except:
              False
      
      
      # obtain a list of s3 Objects with prefix filter
      files = list(bucket.objects.filter(Prefix=prefix))
      
      # Iterating through the list of files
      # Loading streaming body into a file with the same name
      # Printing file name and saving file
      # Note skipping first file since it's only the directory
      
      for file in files[1:]:
          file_name = file.key.split(prefix)[1] # getting the file name of the S3 object
          s3_file = get_object(file) # streaming body needing to iterate through
          if s3_file: # meets the modified by date
              print(file_name) # prints files not modified since timedelta
              try:
                  with io.FileIO(file_name, 'w') as f:
                      for i in s3_file: # iterating though streaming body
                          f.write(i)
              except TypeError as e:
                  print(e, file)
      

      【讨论】:

        猜你喜欢
        • 2017-11-18
        • 1970-01-01
        • 1970-01-01
        • 2020-07-21
        • 1970-01-01
        • 2021-08-23
        • 1970-01-01
        • 1970-01-01
        • 2021-04-11
        相关资源
        最近更新 更多