Skip to content

Instantly share code, notes, and snippets.

@dtenenba
Last active May 23, 2018 18:39
Show Gist options
  • Save dtenenba/aebb89d9593a1fe80a64092e57e2d3b0 to your computer and use it in GitHub Desktop.
Save dtenenba/aebb89d9593a1fe80a64092e57e2d3b0 to your computer and use it in GitHub Desktop.
determine how many bytes of storage are used in all S3 buckets in account
#!/usr/bin/env python3
from datetime import datetime, timedelta
import sys
import boto3
cloudwatch = boto3.client('cloudwatch')
paginator = cloudwatch.get_paginator('list_metrics')
s3 = boto3.client("s3")
buckets = s3.list_buckets()['Buckets']
buckets = [x['Name'] for x in buckets]
total = 0
for bucket_name in buckets:
if not bucket_name.startswith("fh-pi-"):
continue
cloudwatch = boto3.client('cloudwatch')
response = cloudwatch.get_metric_statistics(
Namespace="AWS/S3",
MetricName="BucketSizeBytes",
Dimensions=[
{
"Name": "BucketName",
"Value": bucket_name
},
{
"Name": "StorageType",
"Value": "StandardStorage"
}
],
StartTime=datetime.now() - timedelta(days=1),
EndTime=datetime.now(),
Period=86400,
Statistics=['Average']
)
if not response['Datapoints']:
print("NO DATAPOINTS FOR {}".format(bucket_name))
continue
bucket_size_bytes = response['Datapoints'][-1]['Average']
total += bucket_size_bytes
print("{} has {} bytes".format(bucket_name, bucket_size_bytes))
print("total is {}".format(total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment