Search code examples
amazon-web-servicesamazon-s3aws-cli

Sorting AWS CLI objects listing from S3 bucket by 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.


Solution

  • 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)"