AWS CLI中的boto3版本的list-objects-v2 --query命令 [英] boto3 version of list-objects-v2 --query command in AWS CLI
问题描述
想知道下面的AWS CLI的python boto3代码
Would like to know the python boto3 code for below AWS CLI
aws s3api list-objects-v2 \
--bucket myBucket \
--prefix path1/path2 \
--query 'reverse(sort_by(Contents,&LastModified))[0]'
我没有看到list_objects_v2的任何查询选项
i didnt see any query option for list_objects_v2
https://boto3.readthedocs.io/en/stable/reference/services/s3.html#S3.Client.list_objects_v2
推荐答案
--query 功能="nofollow noreferrer"> AWS命令行界面(CLI)是CLI本身的功能,而不是在API调用期间执行.
The --query
capability in the AWS Command-Line Interface (CLI) is a feature of the CLI itself, rather than being performed during an API call.
如果您使用的是boto3 list_object_v2()
命令,则返回完整的结果.
If you are using the boto3 list_object_v2()
command, a full set of results is returned.
然后您可以使用Python操作结果.
您似乎想列出存储桶/路径中的最新对象,因此可以使用以下内容:
It appears that you are wanting to list the most recent object in the bucket/path, so you could use something like:
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])
这篇关于AWS CLI中的boto3版本的list-objects-v2 --query命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!