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

How do find smallest file size in s3 bucket using AWS CLI?


I'm trying to get min/max of file size in S3 bucket and I always get 3 values instead of one. I think it's controlled by pagination but I'm unable to disable it.

aws s3api list-objects-v2 
--bucket my-bucket-dev 
--prefix subscription/2019/04/01/23 
--output text  
--query 'sort_by(Contents,&Size)[:1].Size'

Result:

618
616
620

How do i get 616 as result without the need to sort locally?

NOTE: I tried sort(), min(), max() max_by(), --no-pagination, --page-size 100000

they all give at least 3 records as result.


Solution

  • aws s3api list-objects-v2 --bucket my-bucket --query 'sort_by(Contents,&Size)[0].Size' 
    

    The sort_by() sorts by size, so you just want the first element by using [0].