Skip to content

Instantly share code, notes, and snippets.

@joehack3r
Created July 14, 2014 04:14
Show Gist options
  • Save joehack3r/4c29515df8b1bc81681c to your computer and use it in GitHub Desktop.
Save joehack3r/4c29515df8b1bc81681c to your computer and use it in GitHub Desktop.
Example Python for get S3 Bucket Size
#!/usr/bin/env python
# This is how I used to write Python scripts. I'm trying to improve:
# https://github.com/joehack3r/aws/blob/master/scripts/s3/getS3BucketSize.py
import argparse
import boto
# Arguments
parser = argparse.ArgumentParser(description='Calcualte S3 bucket size in GB')
parser.add_argument('--buckets', action='store', nargs='+', metavar='<S3 Bucket Name>', dest='s3Buckets', help='S3 buckets to calculate size')
args = parser.parse_args()
for bucket in args.s3Buckets:
s3bucket = boto.s3.connection.S3Connection().get_bucket(bucket)
size = 0
for key in s3bucket.list():
size += key.size
print "%s:%d B:%.1f KiB:%.1f MiB:%.1f GiB" % (bucket, size, size*1.0/1024, size*1.0/1024/1024, size*1.0/1024/1024/1024)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment