python - 在 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_client
和s3_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 日期吗?
解决方案
你可以这样做:
import boto3
s3 = boto3.resource("s3")
s3_object = s3.Object("your_bucket", "your_object_key")
print(s3_object.last_modified)
推荐阅读
- python - 正则表达式匹配所有现有的可能性
- heroku - 如何删除 Heroku 数据剪辑?
- javascript - 根据对象属性的前 3 个单词从对象数组中删除重复项
- python - 过滤小时差 - pandas
- sql - 如何根据同一张表的不同条件计算并返回 1 个 sql 查询中的 sum 和 count?
- arrays - 给定某些条件,为数组元素赋值
- .net-core - Azure DevOps 管道 DotNetCore 测试路径项目模式
- go - Google Cloud Monitoring - 警报政策:状态代码:409 - 对项目配置的并发编辑过多
- javascript - 使用花括号导出时无法测试
- javascript - 使用 JavaScript 扩展 HTML 单选按钮