Skip to content

Instantly share code, notes, and snippets.

@abridgett
Last active December 10, 2018 04:56
Show Gist options
  • Save abridgett/7615c1df69274f359ea86f18aa189219 to your computer and use it in GitHub Desktop.
Save abridgett/7615c1df69274f359ea86f18aa189219 to your computer and use it in GitHub Desktop.
s3_bucket_sizes
#!/bin/sh
# Report S3 bucket sizes
get_bucket_size() {
bucket="$1"
today=$(date +"%Y-%m-%d")
if [ $(uname -s) = "Darwin" ]; then
yesterday=$(date -v-1d +"%Y-%m-%d")
else
yesterday=$(date -d yesterday +"%Y-%m-%d")
fi
aws cloudwatch get-metric-statistics --namespace AWS/S3 \
--start-time $yesterday --end-time $today --period 86400 \
--statistics Average --metric-name BucketSizeBytes \
--dimensions Name=BucketName,Value="$bucket" \
Name=StorageType,Value=StandardStorage \
| awk '/DATAPOINTS/ {printf("%.2f GB", $2/1024/1024/1024)}'
}
buckets=$(s3cmd ls |awk '{sub("^s3://","",$3); print $3}')
for bucket in $buckets; do
echo "$bucket" $(get_bucket_size "$bucket")
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment