要获取AWS S3存储桶的LastModified属性,可以使用AWS SDK提供的API。以下是使用Python的boto3库获取S3存储桶的LastModified属性的示例代码:
import boto3
def get_bucket_last_modified(bucket_name):
s3_client = boto3.client('s3')
response = s3_client.list_objects_v2(Bucket=bucket_name)
if 'Contents' in response:
last_modified = max(obj['LastModified'] for obj in response['Contents'])
return last_modified
return None
# 示例用法
bucket_name = 'your-bucket-name'
last_modified = get_bucket_last_modified(bucket_name)
print(f"The last modified time of {bucket_name} is {last_modified}")
请确保已安装boto3
库,并替换示例代码中的your-bucket-name
为您要查询的S3存储桶名称。此代码将返回指定S3存储桶中最新的对象的LastModified属性。如果存储桶为空,则返回None。
如果要获取多个存储桶的LastModified属性,可以使用循环迭代每个存储桶并调用get_bucket_last_modified
函数。