Created
February 14, 2018 18:12
-
-
Save lardissone/6e6a07385bf68713025ff302489a192f to your computer and use it in GitHub Desktop.
List all S3 buckets and storage size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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