Skip to content

Instantly share code, notes, and snippets.

@sgasser
Last active March 12, 2019 11:02
Show Gist options
  • Select an option

  • Save sgasser/7e2b472a8fbb01e17001284185a617c7 to your computer and use it in GitHub Desktop.

Select an option

Save sgasser/7e2b472a8fbb01e17001284185a617c7 to your computer and use it in GitHub Desktop.
AWS S3 File Size and Count of ALL buckets
#!/bin/bash
#
# Download and run:
# bash aws_s3_all_buckets.sh > aws_s3_all_buckets.csv
#
# Or call directly:
# bash <(curl -s https://gist.githubusercontent.com/sgasser/7e2b472a8fbb01e17001284185a617c7/raw) > aws_s3_all_buckets.csv
# get all buckets
buckets=$(aws s3api list-buckets --query "Buckets[].Name" | jq -r '.[]')
echo "name;size (GB);count;"
for bucket in $buckets
do
echo -n "$bucket;"
# print size
aws s3api list-objects --bucket "$bucket" --output json --query "sum(Contents[].Size)" | awk '{ byte=$1 /1024/1024/1024; printf byte ";" }'
# print count
aws s3api list-objects --bucket "$bucket" --output json --query "length(Contents[])" | awk '{ count=$1; printf count ";" }'
echo ''
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment