Read more

Listing sizes in AWS S3 Buckets

Claus-Theodor Riegg
November 25, 2020Software engineer at makandra GmbH

Getting the whole bucket size

aws s3 ls s3://$BUCKETNAME/ --recursive --human-readable --summarize | tail -n2

Tail is used because otherwise all files will be printed on screen (but you may want that for some reason).

Getting the size of a specific directory/file

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

You just need to add the path to the bucket name:

aws s3 ls s3://$BUCKETNAME/some/dir --recursive --human-readable --summarize | tail -n2

Getting the size of all subdirs

If you imagine there is something like ncdu or du -ha -d 1 /foo/bar you're wrong. If you want to get the size of all subdirs you have to do it yourself:

Your Bucket contains in $BUCKETNAME/some/dir multiple subdirs (for e.g. 2020, 2019 and 2018) and you want to see the size of these 3 directories.

S3PATH='s3://$BUCKETNAME/some/dir/'
for dir in $(aws s3 ls "${S3PATH}" | grep PRE | awk '{print $2}'); do 
    echo "${S3PATH}${dir}:"
    aws s3 ls "${S3PATH}${dir}" --recursive \
                                --human-readable \
                                --summarize\
    | tail -n2
done

A | column after done could improve the readability.

Posted by Claus-Theodor Riegg to makandra Operations (2020-11-25 09:22)