Skip to content

Instantly share code, notes, and snippets.

@holly
Last active December 17, 2022 16:21
Show Gist options
  • Save holly/2eaa571b0c6eff68a6c58c1bcdee7f4a to your computer and use it in GitHub Desktop.
Save holly/2eaa571b0c6eff68a6c58c1bcdee7f4a to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ -z "${AWS_DEFAULT_PROFILE}" ]; then
export AWS_DEFAULT_PROFILE=default
fi
set -e
set -u
START_TIME=$(date -u +%Y-%m-%dT00:00:00Z -d "1 day ago")
END_TIME=$(date -u +%Y-%m-%dT00:00:00Z)
PERIOD=86400
get_s3_bucket_size() {
aws cloudwatch get-metric-statistics --namespace AWS/S3 --metric-name BucketSizeBytes \
--dimensions Name=BucketName,Value=$1 Name=StorageType,Value=StandardStorage --statistics Sum \
--start-time $START_TIME --end-time $END_TIME --period $PERIOD |
jq '.Datapoints[].Sum'
}
get_s3_object_num() {
QUERY="[
{
\"Id\": \"objectCount\",
\"MetricStat\": {
\"Metric\": {
\"Namespace\": \"AWS/S3\",
\"MetricName\": \"NumberOfObjects\",
\"Dimensions\": [
{
\"Name\": \"StorageType\",
\"Value\": \"AllStorageTypes\"
},
{
\"Name\": \"BucketName\",
\"Value\": \"$1\"
}
]
},
\"Period\": 86400,
\"Stat\": \"Maximum\"
}
}
]"
# result
#"MetricDataResults": [
# {
# "Id": "objectCount",
# "Label": "NumberOfObjects",
# "Timestamps": [
# "2022-12-16T00:00:00+00:00"
# ],
# "Values": [
# 4492.0
# ],
# "StatusCode": "Complete"
# }
#],
#"Messages": []
#aws cloudwatch get-metric-data --metric-data-queries "${QUERY}" \
#--start-time $START_TIME \
#--end-time $END_TIME |
#jq '.MetricDataResults[0].Values[0]'
aws cloudwatch get-metric-statistics --namespace AWS/S3 --metric-name NumberOfObjects \
--dimensions Name=BucketName,Value=$1 Name=StorageType,Value=AllStorageTypes --statistics Maximum \
--start-time $START_TIME --end-time $END_TIME --period $PERIOD |
jq '.Datapoints[0].Maximum'
}
echo -e "Bucket\tRegion\tSize\tObjects"
for bucket in $(aws s3 ls | awk '{ print $3 }'); do
region=$(aws s3api get-bucket-location --bucket $bucket | jq -r ".LocationConstraint")
if [ "${region}" = "null" ]; then
region="us-east-1"
fi
if [ "${region}" = "EU" ]; then
region="eu-west-1"
fi
#size=$(aws s3 ls s3://${bucket} --recursive --summarize | tail -1 | awk '{print $3}')
size=$(get_s3_bucket_size $bucket)
object_num=$(get_s3_object_num $bucket)
echo -e "$bucket\t$region\t$size\t$object_num"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment