How to filter s3 objects by last modified date with Boto3

后端 未结 1 1079
孤街浪徒
孤街浪徒 2020-12-19 13:14

Is there a way to filter s3 objects by last modified date in boto3? I\'ve constructed a large text file list of all the contents in a bucket. Some time has passed and I\'d l

相关标签:
1条回答
  • 2020-12-19 14:02

    The following code snippet gets all objects under specific folder and check if the file last modified is created after the time you specify :

    Replace YEAR,MONTH, DAY with your values.

    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))
    
    0 讨论(0)
提交回复
热议问题