NerdVana

Artificial intelligence will never trump natural stupidity.

s3 bucket usage script

Written by Eric Schwimmer

We needed a summation of all the S3 bucket sizes in our default region (accountants, amirite?). So this script was born:

#!/bin/bash
end=$(date +%s)
start=$((end - 86400))
while read bucket; do
    printf "%-30.30s : " "$bucket"
    aws cloudwatch get-metric-statistics \
        --namespace AWS/S3 \
        --start-time $start \
        --end-time $end \
        --period 86400 \
        --statistics Average \
        --metric-name BucketSizeBytes \
        --dimensions Name=BucketName,Value="$bucket" \
              Name=StorageType,Value=StandardStorage \
        --unit "Bytes" \
    | jq -r '.Datapoints[0].Average' \
    | sed 's/null/0/' 
done < <(aws s3 ls | awk '{print $NF}') \
| sort -rn -k 3 \
| numfmt --to=si --round=nearest --padding -1 --field 3