猿问

如何使用s3中的boto3获取最后修改的文件名

我想从亚马逊 s3 的目录中获取最后修改的文件。我现在才尝试仅打印该文件日期,但出现此错误。


类型错误:“datetime.datetime”对象不可迭代


import boto3

s3 = boto3.resource('s3',aws_access_key_id='demo', aws_secret_access_key='demo')


my_bucket = s3.Bucket('demo')


for file in my_bucket.objects.all():

    # print(file.key)

    print(max(file.last_modified))


有只小跳蛙
浏览 246回答 1
1回答

慕的地6264312

你有一个简单的片段。简而言之,您必须遍历文件才能找到所有文件中的最后修改日期。然后你有这个日期的打印文件(可能不止一个)。from datetime import datetimeimport boto3s3 = boto3.resource('s3',aws_access_key_id='demo', aws_secret_access_key='demo')my_bucket = s3.Bucket('demo')last_modified_date = datetime(1939, 9, 1).replace(tzinfo=None)for file in my_bucket.objects.all():&nbsp; &nbsp; # print(file.key)&nbsp; &nbsp; file_date = file.last_modified.replace(tzinfo=None)&nbsp; &nbsp; if last_modified_date < file_date:&nbsp; &nbsp; &nbsp; &nbsp; last_modified_date = file_dateprint(last_modified_date)# you can have more than one file with this date, so you must iterate againfor file in my_bucket.objects.all():&nbsp; &nbsp; if file.last_modified.replace(tzinfo=None) == last_modified_date:&nbsp; &nbsp; &nbsp; &nbsp; print(file.key)&nbsp; &nbsp; &nbsp; &nbsp; print(last_modified_date)
随时随地看视频慕课网APP

相关分类

Python
我要回答