Skip to content

Instantly share code, notes, and snippets.

@matt2000
Last active December 20, 2017 22:01
Show Gist options
  • Save matt2000/0d6675c61f5b39b1cbae3e5ed1355b25 to your computer and use it in GitHub Desktop.
Save matt2000/0d6675c61f5b39b1cbae3e5ed1355b25 to your computer and use it in GitHub Desktop.
import boto3
debug = False
bucket = 'your-bucket-name'
prefix = 'some/s3/prefix/'
s3 = boto3.client('s3')
size = 0
result = s3.list_objects_v2(Bucket=bucket, Prefix=prefix)
try:
size += sum([x['Size'] for x in result['Contents']])
except Exception as e:
if debug:
print(result)
raise e
while result['IsTruncated']:
if debug:
print('Continuing from: ' + result['Contents'][0]['Key'])
result = s3.list_objects_v2(
Bucket=bucket, Prefix=prefix,
ContinuationToken=result['NextContinuationToken'])
size += sum([x['Size'] for x in result['Contents']])
print('Total size in Gb: ' + str(size / (1000**3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment