Skip to content

Instantly share code, notes, and snippets.

@robinkraft
Last active October 24, 2021 11:43
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save robinkraft/2667939 to your computer and use it in GitHub Desktop.
Save robinkraft/2667939 to your computer and use it in GitHub Desktop.
Simple python script to calculate size of S3 buckets
import sys
import boto
# based on http://www.quora.com/Amazon-S3/What-is-the-fastest-way-to-measure-the-total-size-of-an-S3-bucket
# assumes you've already configured your access id & secret key
s3 = boto.connect_s3()
def get_bucket_size(bucket_name):
'''Given a bucket name, retrieve the size of each key in the bucket
and sum them together. Returns the size in gigabytes and
the number of objects.'''
bucket = s3.lookup(bucket_name)
total_bytes = 0
n = 0
for key in bucket:
total_bytes += key.size
n += 1
if n % 2000 == 0:
print n
total_gigs = total_bytes/1024/1024/1024
print "%s: %i GB, %i objects" % (bucket_name, total_gigs, n)
return total_gigs, n
if __name__ == '__main__':
if len(sys.argv) > 1:
bucket_list = sys.argv[1:]
else:
bucket_list = [i.name for i in s3.get_all_buckets()]
bucket_sizes = []
for bucket_name in bucket_list:
size, object_count = get_bucket_size(bucket_name)
bucket_sizes.append(dict(name=bucket_name, size=size, count=object_count))
print "\nTotals:"
for bucket_size in bucket_sizes:
print "%s: %iGB, %i objects" % (bucket_size["name"], bucket_size["size"], bucket_size["count"])
@srinathmatti
Copy link

srinathmatti commented Apr 25, 2017

import boto
import boto.s3.connection
access_key = ''
secret_key = ''
conn = boto.connect_s3( aws_access_key_id = access_key, aws_secret_access_key = secret_key)
for bucket in conn.get_all_buckets():
	total_bytes = 0
        name = bucket.name
	for key in bucket:
		total_bytes += key.size
	total_bytes = total_bytes/1024/1024/1024
	print "Bucket Name:" ,name, "Size: ",total_bytes ,"GB"

@learneriq
Copy link

hi
how can we calculate size of every bucket of S3 in boto3
example: Bucket_name total size ('bucket_A ', 0) ('Bucket_B', 51090)

@keenan-v1
Copy link

This article has a much faster way to do this, especially when you have large buckets: https://www.slsmk.com/getting-the-size-of-an-s3-bucket-using-boto3-for-aws/

@Subhasis180689
Copy link

Subhasis180689 commented Feb 19, 2020

This article has bucket size with boto3 without cloudwatch
https://theleadcoder.wordpress.com/2020/02/18/s3-bucket-size-with-boto3/

@jayk2020
Copy link

This article has bucket size with boto3 without cloudwatch
https://theleadcoder.wordpress.com/2020/02/18/s3-bucket-size-with-boto3/

great article! solved the issue!

@StanSilas
Copy link

StanSilas commented Dec 16, 2020

@keenan-v1 @jayk2020 @Subhasis180689 @srinathmatti how do I find out the size of a given prefix in a bucket so that versions are also enabled as only that will give the true versions.

Ex bucket-A
has prefix-a prefix-b

I wanna find out the total size of prefix-a including versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment