首页 > 解决方案 > 在 S3 中查找特定文件的最后修改日期

问题描述

根据这个答案:https ://stackoverflow.com/a/9688496/13067694

>>> import boto
>>> s3 = boto.connect_s3()
>>> bucket = s3.lookup('mybucket')
>>> for key in bucket:
       print key.name, key.size, key.last_modified
index.html 13738 2012-03-13T03:54:07.000Z
markdown.css 5991 2012-03-06T18:32:43.000Z
>>>

我可以做这样的事情来找到 last_modified 日期。在这里,他们使用的是 boto.connect_s3。但我无法在 AWS Lambda 上导入 boto,它可能不再使用。

在我的代码片段中,我已经在使用 boto3s3_clients3_resource. 我尝试了最后一行来查找存储桶中特定文件的最后修改日期:

s3 = boto3.client('s3')
lst = s3.list_objects(Bucket='testunzipping')['Contents']

s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket('testunzipping')

s3.download_file('testunzipping','DataPump_10000838.zip','/tmp/DataPump_10000838.zip')
lastModified = bucket['DataPump_10000838.zip'].last_modified

但它给了我一个错误"errorMessage": "'s3.Bucket' object is not subscriptable"

是否仍然可以仅使用 boto3.client 和资源找到 last_modified 日期?另外,我还使用 s3.download_file 下载文件。我不能在下载时只提取 last_modified 日期吗?

标签: pythonamazon-web-servicesamazon-s3boto3boto

解决方案


你可以这样做:

import boto3

s3 = boto3.resource("s3")
s3_object = s3.Object("your_bucket", "your_object_key")
print(s3_object.last_modified)

推荐阅读