-
-
Save robinkraft/2667939 to your computer and use it in GitHub Desktop.
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
commented
Apr 25, 2017
•
hi
how can we calculate size of every bucket of S3 in boto3
example: Bucket_name total size ('bucket_A ', 0) ('Bucket_B', 51090)
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/
This article has bucket size with boto3 without cloudwatch
https://theleadcoder.wordpress.com/2020/02/18/s3-bucket-size-with-boto3/
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!
@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.