S3存储桶是否有关于上次更新时间的信息?如何查找最后一次更新存储桶中的任何对象?
铲斗last modified time
没有原生支持。我这样做的方法是使用aws cli
,排序输出,取底线并打印前2个字段。
$ aws s3 ls mybucket --recursive | sort | tail -n 1 | cut -d ' ' -f1,2
2016-03-18 22:46:48
正如其他人所评论的那样,存储此信息的元数据并不神奇。你只需要遍历对象。
使用boto3
执行此操作的代码:
import boto3
from datetime import datetime
def bucket_last_modified(bucket_name: str) -> datetime:
"""
Given an S3 bucket, returns the last time that any of its objects was
modified, as a timezone-aware datetime.
"""
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
objects = list(bucket.objects.all())
return max(obj.last_modified for obj in objects)
关于GET BUCKET对象版本的Amazon S3 API规范(可在:http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETVersion.html获得)表示返回了LastModified属性 - 但我不确定它是否更新了每个对象的更改...