Skip to content

Instantly share code, notes, and snippets.

@anamorph
Last active August 29, 2015 14:10
Show Gist options
  • Save anamorph/699dbae01ff0f4942acc to your computer and use it in GitHub Desktop.
Save anamorph/699dbae01ff0f4942acc to your computer and use it in GitHub Desktop.
aws s3api list-objects --bucket %bucketname% --query 'sum(Contents[].Size)' --output json | awk 'BEGIN {total=0}{total+=$0}END{print total/1024/1024/1024" GB"}'
@sebsto
Copy link

sebsto commented Feb 11, 2015

Nice usage of JMESPath queries !

You can make it shorter :
aws s3api list-objects --bucket $BUCKETNAME --query 'sum(Contents[].Size)' | awk '{print $0/1024/1024/1024" GB"}'

--output json is not required
AWK's variable is not required neither

@anamorph
Copy link
Author

anamorph commented Jul 2, 2015

yet another way of doing this, via python/boto:

#!/usr/local/bin/python

import sys

from boto.s3.connection import S3Connection

s3bucket = S3Connection().get_bucket(sys.argv[1])
size = 0
totalCount = 0

for key in s3bucket.list():
    totalCount += 1
    size += key.size

print 'total size:'
print "%.3f GB" % (size*1.0/1024/1024/1024)
print 'total count:'
print totalCount

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