Saturday, March 28, 2015

Sorting contents of S3 bucket via AWS CLI since AWS console doesn't allow sort today

In AWS console, the columns don't sort if we click on them :-


So the current way to sort would be to use AWS CLI using "sort_by" function as below:

$aws s3api list-objects --bucket sample-bucket --max-items 3 --query "sort_by(Contents, &LastModified)[*].{Key:Key,Size:Size,LastModified:LastModified}"
[
    {
        "LastModified": "2015-02-04T10:55:29.000Z",
        "Key": "sample1.csv",
        "Size": 27848
    },
    {
        "LastModified": "2015-02-04T10:55:29.000Z",
        "Key": "sample2.csv",
        "Size": 469266754
    },
    {
        "LastModified": "2015-02-04T10:55:44.000Z",
        "Key": "sample3.csv",
        "Size": 469529478
    }
]

Since s3api does not have a "sort-order" switch unlike some of the other api's, you will have to use the "reverse" function specified as below:-

$aws s3api list-objects --bucket sample-bucket --max-items 3 --query "reverse(sort_by(Contents, &LastModified)[*].[Key, LastModified])" --output table

sample3.csv 2015-02-04T10:55:44.000Z
sample2.csv  2015-02-04T10:55:29.000Z
sample1.csv  2015-02-04T10:55:29.000Z

For more details on jmespath json query language specification, pl. refer to

http://jmespath.org/

3 comments:

  1. It Works But I want the objects to be sorted in amazon s3

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Thank you.Well it was nice post and very helpful information on
    AWS Online Training

    ReplyDelete