英文:
Sorting AWS CLI objects listing from S3 bucket by Name
问题
AWS CLI有办法按照Name
列的字母顺序返回ls
命令的结果吗?换句话说,是否可以在整个存储桶上进行排序,然后返回结果对象?因为我正在使用--page-size
参数来截断结果的数量(类似于分页的偏移量),因为AWS CLI没有提供这个功能。
我有一个包含图像的存储桶,每个图像都有一个Name
列。
以下是当前用于列出对象的带有sort_by
查询的命令,但它不起作用(它返回所有对象,但排序未应用):
aws s3 ls s3://bucket_path --page-size page_size --query "sort_by(@, &Name)" --summarize
另外,列出存储桶对象时的默认排序是什么?
我得到的结果格式如下:
2023-08-09 09:49:08 7031141 image_name.jpg
但它们既不按LastModified
日期排序,也不按Name
排序。
英文:
Is there a way for AWS CLI to return ls
results sorted alphabetically by Name
column? In other words, do the sorting on the whole bucket and then return resulting objects, because I'm using --page-size
argument to truncate number of results (doing a sort of pagination with offset, since AWS CLI doesn't offer that out-of-the-box)?
I have a bucket of images and each image has Name
column.
Current command for listing objects with sort_by
query that does not work (it returns all objects, but sort is not applied):
aws s3 ls s3://bucket_path --page-size page_size --query "sort_by(@, &Name)" --summarize
Also, what is default sort for objects when listing bucket?
I'm getting results formatted like this:
2023-08-09 09:49:08 7031141 image_name.jpg
but they aren't sorted neither by LastModified
date, nor by Name
.
答案1
得分: 1
不要使用aws s3 ls
命令,该命令试图提供类似Linux的列表。而是使用以下命令:
aws s3api list-objects-v2 --bucket bucket-name
这将始终按对象的Key
排序返回结果。请注意,Key
包括对象的完整路径(包括子目录)。
list-objects-v2
调用直接映射到用于列出S3中对象的API调用,并返回S3提供的原始数据。
默认响应将是JSON格式,但你可以修改它以仅获取Key
:
aws s3api list-objects-v2 --bucket bucket-name --query 'Contents[].[Key]' --output text
英文:
Rather than using aws s3 ls
, which attempts to provide a Linux-like listing, use:
aws s3api list-objects-v2 --bucket bucket-name
This will always return results sorted by the Key
of the object. Note that the Key includes the full path of the object (including subdirectories).
The list-objects-v2
call directly maps to the API call used to list objects in S3 and it returns the raw data provided by S3.
The default response will be JSON, but you can modify it to just get the Key:
aws s3api list-objects-v2 --bucket bucket-name --query 'Contents[].[Key]' --output text
答案2
得分: 0
CLI输出命令的结果可以使用查询在客户端进行过滤,正如官方文档中所述,而过滤器是在服务器端应用的。
查询使用JMESPath标准来管理JSON结构上的路径和查询,但s3 CLI的ls命令不处理JSON输出。
您可以使用list-objects命令,例如按Key(也称为名称)字段排序:
$ aws --output json s3api list-objects --bucket ${bucket_name} --query "sort_by(Contents, &Key)"
英文:
CLI output command results can be filtered client-side using queries, as stated in the official doc, whereas filters are applied server-side.
Queries operate using the JMESPath standard for managing paths and queries on JSON structures, but the s3 CLI ls command doesn't handle JSON output.
You can use list-object instead e.g. sorted by the Key (a.k.a. name) field:
$ aws --output json s3api list-objects --bucket ${bucket_name} --query "sort_by(Contents, &Key)"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论