Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS find max file size in S3 bucket

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?

like image 385
xtra Avatar asked Dec 12 '18 14:12

xtra


People also ask

How do I find out the size of my S3?

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.

What is the max size of the file in a bucket?

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.

How do I see how many files are in a S3 bucket?

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.


2 Answers

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:

  • Replace -1 with -N to find the largest N files.
  • Include .[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.

like image 131
sparrowt Avatar answered Sep 18 '22 06:09

sparrowt


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/

like image 34
alecswan Avatar answered Sep 20 '22 06:09

alecswan