且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 Boto3 按上次修改日期过滤 s3 对象

更新时间:2022-11-07 12:24:25

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

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

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))