【问题标题】:find last modified date of a particular file in S3在 S3 中查找特定文件的最后修改日期
【发布时间】:2021-12-08 23:03:23
【问题描述】:

根据这个答案: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,它可能已不再使用。

在我的代码 sn-p 中,我已经在使用 boto3 的 s3_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 日期吗?

【问题讨论】:

    标签: python amazon-web-services amazon-s3 boto3 boto


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

    • 这里的关键是什么?这不会给我整个存储桶的 last_modified,而不是那个文件吗?
    • 根据您的示例,密钥将是 /tmp/DataPump_10000838.zip
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    相关资源
    最近更新 更多