I want to find the size and name of the biggest file in my S3 bucket.
Currently I have:
aws s3api list-objects --bucket bucket-name --output json --query "[max(Contents[].Size), length(Contents[])]"
which does not allow me to see the name of the file.
I also have the command to list the details of all files on the bucket:
aws s3api list-object-versions --bucket bucket-name --query 'Versions[*].Size'
What command will give me the name and size of the largest file(s) on the S3 bucket?
To find the size of a single S3 bucket, you can use the S3 console and select the bucket you wish to view. Under Metrics, there's a graph that shows the total number of bytes stored over time.
1 Answer. The maximum file size is 5 TB and the maximum size for a single 'Put' operation is 5GB which means that you won't be able to upload 8 Gb files with the single operation.
Go to AWS Billing, then reports, then AWS Usage reports. Select Amazon Simple Storage Service, then Operation StandardStorage. Then you can download a CSV file that includes a UsageType of StorageObjectCount that lists the item count for each bucket. Save this answer.
Using AWS CLI only, this will find the largest file:
aws s3api list-objects-v2 --bucket bucket-name --query "sort_by(Contents, &Size)[-1:]"
or to include non-current versions if applicable:
aws s3api list-object-versions --bucket bucket-name --query "sort_by(Versions[*], &Size)[-1:]"
Optional tweaks:
-1
with -N
to find the largest N files..[Key,Size]
at the end of the --query
to select only those fields.Sadly I think the filtering is done client side because this downloaded 28 MB when run on a large bucket. However it is still a useful 1-liner despite not being quick.
The following should return the name and size of the largest file in the bucket "bucket-name".
aws s3api list-object-versions --bucket bucket-name | jq -r '.Versions[] | "\(.Key)\t \(.Size)"' | sort -k2 -r -n | head -1
The command above uses jq which you can install from https://stedolan.github.io/jq/download/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With