想知道以下 AWS CLI 的 python boto3 代码
aws s3api list-objects-v2 \
--bucket myBucket \
--prefix path1/path2 \
--query 'reverse(sort_by(Contents,&LastModified))[0]'
我没有看到 list_objects_v2 的任何查询选项
https://boto3.readthedocs.io/en/stable/reference/services/s3.html#S3.Client.list_objects_v2
请您参考如下方法:
AWS Command-Line Interface (CLI) 中的--query 功能是 CLI 本身的一项功能,而不是在 API 调用期间执行。
如果您使用的是 boto3 list_object_v2() command , 返回全套结果。
然后您可以使用 Python 来操作结果。
您似乎想要列出存储桶/路径中的最新对象,因此您可以使用类似的东西:
import boto3
client = boto3.client('s3',region_name='ap-southeast-2')
response = client.list_objects_v2(Bucket='my-bucket')
print (sorted(response['Contents'], key=lambda item: item['LastModified'])[-1])
