Skip to content

Instantly share code, notes, and snippets.

@kshailen
Created May 7, 2020 11:38
Show Gist options
  • Save kshailen/1ff28a339422b336a3f82239d337f877 to your computer and use it in GitHub Desktop.
Save kshailen/1ff28a339422b336a3f82239d337f877 to your computer and use it in GitHub Desktop.
Get size details of all s3 buckets in an account
#!/bin/bash
# exit when the command fails
set -o errexit;
# exit when try to use undeclared var
set -o nounset;
Total_num_of_buckets=`aws s3 ls s3:// | awk '{print $3}'| wc -l`
printf "%s: %4d\n" "Total Number of s3 buckets" ${Total_num_of_buckets}
printf "%s: %4s\n" "Bucket Name" "Bucket Size"
region=us-east-1
#Handling for MacOS and Linux
OS_TYPE=`uname`
if [ ${OS_TYPE} == 'Darwin' ]; then
Yesterday=`date -v-1d +%FT%H:%M`
dbf_yes=`date -v-2d +%FT%H:%M`
elif [ ${OS_TYPE} == 'Linux' ]; then
Yesterday=`date -d "1 day ago" '+%FT%H:%M'`
dbf_yes=`date -d "2 day ago" '+%FT%H:%M'`
fi
for i in `aws s3 ls s3:// | awk '{print $NF}'` ; do
size=`aws cloudwatch get-metric-statistics --namespace AWS/S3 --region ${region} --metric-name BucketSizeBytes --dimensions Name=BucketName,Value=${i} Name=StorageType,Value=StandardStorage --start-time ${dbf_yes} --end-time ${Yesterday} --period 86400 --statistic Average | jq .Datapoints[0].Average`
size_in_gb=`echo "scale=2;$size / 1024/ 1024 / 1024"|bc -l`
echo "${i}: ${size_in_gb} GB"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment