sam*_*sam 3 python amazon-s3 amazon-web-services boto3
我想从 Amazon 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))
Run Code Online (Sandbox Code Playgroud)
那里有一个简单的片段。简而言之,您必须迭代文件才能找到所有文件中的最后修改日期。然后您就有了包含该日期的打印文件(可能不止一个)。
from datetime import datetime
import boto3
s3 = 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():
file_date = file.last_modified.replace(tzinfo=None)
if last_modified_date < file_date:
last_modified_date = file_date
print(last_modified_date)
# you can have more than one file with this date, so you must iterate again
for file in my_bucket.objects.all():
if file.last_modified.replace(tzinfo=None) == last_modified_date:
print(file.key)
print(last_modified_date)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15671 次 |
| 最近记录: |