Skip to content

Instantly share code, notes, and snippets.

@jhpacker
Last active February 7, 2020 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhpacker/46e27626563b39226a0ec3a37f2e7c2f to your computer and use it in GitHub Desktop.
Save jhpacker/46e27626563b39226a0ec3a37f2e7c2f to your computer and use it in GitHub Desktop.
s3 storage per bucket
#!/bin/bash
# jason@quantable.com 2020-02-07
# display S3 usage by bucket & type from cloudwatch billing info, not inspecting bucket itself
# requires aws-cli & jq
# usage ./s3_usage.sh profile
# profile is optional, region follows from profile. s3 is region-less but cloudwatch isn't
if [ -z $1 ]; then
PROFILE='default'
else
PROFILE=$1
fi
# uses macosx gnu version of utils like numfmt, grep, etc.
if [[ $(uname -s) = *Darwin* ]]; then
DATE='gdate'
NUMFMT='gnumfmt'
GREP='ggrep'
else
DATE='date'
NUMFMT='numfmt'
GREP='grep'
fi
for type in "StandardStorage" "StandardIAStorage" "ReducedRedundancyStorage"
do echo "-$type-"
for bucket in `aws --profile $PROFILE s3 ls | awk '{print $3}'`
do aws --profile $PROFILE cloudwatch get-metric-statistics --namespace AWS/S3 \
--start-time $($DATE --date "Yesterday" +"%Y-%m-%dT00:00:00Z") --end-time $($DATE --date "Today" +"%Y-%m-%dT00:00:00Z") \
--period 86400 --statistics Average --metric-name BucketSizeBytes \
--dimensions Name=BucketName,Value=$bucket Name=StorageType,Value=$type \
| jq .Datapoints[].Average \
| $NUMFMT -z --to=iec --suffix=B && echo " $bucket"
done | sort -rh | $GREP -vP '^ '
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment