查找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日期。这里,他们使用的是bot .connect_s3。但是我无法在AWS Lambda上导入boto,它可能已经不再使用了。

在我的代码片段中,我已经使用boto3的s3_clients3_resource。我尝试在最后一行查找bucket中特定文件的最后修改日期:

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找到last_modified日期?仅仅是客户端和资源?另外,我还使用s3.download_file下载文件。我不能只是提取last_modified日期,而下载它?

你可以这样做:

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

最新更新