Skip to content

Instantly share code, notes, and snippets.

@lardissone
Created February 14, 2018 18:12
Show Gist options
  • Save lardissone/6e6a07385bf68713025ff302489a192f to your computer and use it in GitHub Desktop.
Save lardissone/6e6a07385bf68713025ff302489a192f to your computer and use it in GitHub Desktop.
List all S3 buckets and storage size
import boto3
s3 = boto3.resource('s3')
def human_size(bytes, units=[' bytes','KB','MB','GB','TB', 'PB', 'EB']):
return str(bytes) + units[0] if bytes < 1024 else human_size(bytes>>10, units[1:])
def get_bucket_size(balde):
bucket = s3.Bucket(balde)
total_size = 0
for k in bucket.objects.all():
total_size += k.size
return total_size
for bucket in s3.buckets.all():
bucket_size = human_size(get_bucket_size(bucket.name))
print('%s: %s' % (bucket.name, bucket_size))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment