21

I have multiple AWS accounts and I need to list all S3 buckets per account and then view each buckets total size.

Currently, I can only view the storage size of a single S3 bucket with:

aws s3 ls s3://mybucket --recursive --human-readable --summarize
2

6 Answers 6

20

Resolution 1

So I solved this with the following script. I originally posted the question just in case there was an easier way that I was not aware of.

#!/bin/bash
aws_profile=('profile1' 'profile2' 'profile3');

#loop AWS profiles
for i in "${aws_profile[@]}"; do
  echo "${i}"
  buckets=($(aws s3 ls s3:// --recursive --profile "${i}" --region your_region | awk '{print $3}'))

  #loop S3 buckets
  for j in "${buckets[@]}"; do
  echo "${j}"
  aws s3 ls s3://"${j}" --recursive --human-readable --summarize --profile "${i}" --region your_region | awk END'{print}'
  done

done

Resolution 2

Using Dashboards in CloudWatch in the AWS console.

You can then simply specify all S3 buckets and add the numbers stats to show the storage size metrics.

This won't cost you plenty of API calls and can be significantly faster depending on the size of the s3 buckets(takes quite awhile to get the size on very large buckets).

Verdict

Creating the Dashboard (Resolution 2) on each AWS account was the most efficient option for me cause it is way quicker for me to log in and grab the metrics manually from each AWS account than to wait for the scripts API calls to finish. :(

6
  • Why | awk END'{print}' ?
    – Tensibai
    Commented Oct 4, 2017 at 14:57
  • 1
    Using the recursive option shows the size of every folder and file and I only need the output of the total size of the bucket. Commented Oct 4, 2017 at 15:08
  • 1
    You need to select a '1 day' or greater period for the dashboard to display anything Commented Jul 16, 2018 at 17:38
  • 2
    Beware that the solution proposed in Resolution 2 increments your Cloudwatch cost since dashboards have a $3 cost each at the moment. aws.amazon.com/cloudwatch/pricing/?nc1=h_ls
    – Drubio
    Commented Mar 6, 2019 at 17:52
  • 1
    @Joe: the @ in ${buckets[@]} represents all elements in the array. If you leave off the [@] the loop will only work on the first element. See the section "Looping through arrays" here: opensource.com/article/18/5/…
    – Russell G
    Commented Jul 1, 2021 at 13:22
5

You will need to write a script that parses and queries this data because as far as I'm aware, there is not a tool or cli function that performs this. Luckily, you can gather all of this information with the CLI.

  1. List and parse all of the accounts in your org.

    aws organizations list-accounts
    
  2. For each account, list and parse all of the buckets.

    aws s3api list-buckets --query "Buckets[].Name"
    
  3. Finally, get the size of each bucket within each account. You can use the same cli command you were before, but be warned that you are going to be listing the individual size of each item within the bucket. You can also use this cli command to get bucket size.

    aws s3api list-objects --bucket BUCKETNAME --output json --query "
    [sum(Contents[].Size), length(Contents[])]"
    
1
  • 1
    1. does not list the profile information of the AWS account. I did not have time to regex this from the aws config file where this information is stored so I just hardcoded the values in the script i posted below Commented Oct 4, 2017 at 14:44
3

Easiest way to see this information nowadays is using S3 Storage Lens.

S3 dashboard > S3 storage lens (left navigation pane) > default-account-dashboard > Buckets tab

Scroll down to the "Buckets" section and sort by total storage.

2

You're going to have to write a script to do this unless there's a tool to do it that I can't find.

If you're working with multiple aws accounts you'll need to be switching between accounts to get the bucket sizes.

To get all of the buckets and their total size you can use 'aws s3api'. Using list-buckets you can get all of the buckets in one account and then you'll be able to list their sizes and try the next account.

1
1

None of these show the size of the bucket if Versioning is turned on and massive amounts of data have been "deleted". In the S3 GUI console, the Metrics tab shows what you are being billed for.

Scraped the following command out of a script found at https://serverfault.com/questions/84815/how-can-i-get-the-size-of-an-amazon-s3-bucket . It works well on Windows. Substitute yourregion and yourbucketname:

aws cloudwatch get-metric-statistics --namespace AWS/S3 --start-time 2019-05-09T00:01:00Z --end-time 2019-05-11T23:59:00Z --period 86400 --statistics Average --region yourregion --metric-name BucketSizeBytes --dimensions Name=BucketName,Value=yourbucketname Name=StorageType,Value=StandardStorage

This gives the average size in bytes over the days bounded by the start and end times. The result can shock you if your bucket has Versioning Enabled (active) and you think you have "deleted" massive amounts of data.

To clean up Versioning-enabled buckets, you must set a LifeCycle rule. You can choose to permanently delete Previous Versions. You may omit the checkboxes at the Transitions step.

0

I just had to go through this and wrote/uploaded a script for it in GitHub. https://github.com/johnwhumphreys/s3-buckets-by-size/blob/main/get-bucket-sizes.py

It's a single python script that will quickly grab the cloudwatch metrics for the sizes for your buckets and rank them by order, showing the top 10. Runs in a minute or so on our 200 bucket account.

(no way I could have listed the files we had in any reasonable time frame, so had to find a faster way)

This also deals w/ Intelligent Tiering as of 2023.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.