Calculating the size of your S3 bucket using Python


For the following script you need to have installed Python >= 2.6 and Boto:

apt-get install -y python-boto

Next create a file ~/.boto containing the following lines:

[Credentials]
region = us-east-1
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCCESS_KEY

Put in the AWS credentials to access your bucket

Then create the script, replace NAME_OF_YOU_BUCKET with your actual bucket name, make it executable and run it:

#!/usr/local/bin/python2.6

import boto
from boto.s3.connection import Location

def sizeof_fmt(num):
   for x in ['bytes','KB','MB','GB','TB']:
       if num < 1024.0:
           return "%3.1f %s" % (num, x)
       num /= 1024.0


s3 = boto.connect_s3()
bucket = s3.lookup('NAME_OF_YOUR_BUCKET')

total_bytes = 0
for key in bucket:
 total_bytes += key.size


print sizeof_fmt(total_bytes)