Skip to content

Instantly share code, notes, and snippets.

@flyher
Forked from nberserk/calculatingS3Size.py
Created November 28, 2018 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyher/1a0b85ed0226a6ce7276af4abe67ece3 to your computer and use it in GitHub Desktop.
Save flyher/1a0b85ed0226a6ce7276af4abe67ece3 to your computer and use it in GitHub Desktop.
calculating S3 file size containing <your_filter> in <your_bucket> using boto3 python library.
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('<your_bucket>')
size = 0
for o in bucket.objects.all():
if '<your_filter>' in o.key:
print o, o.size
size += o.size
print 's3 size = %.3f GB' % (size/1024/1024/1024)
@flyher
Copy link
Author

flyher commented Nov 28, 2018

@nberserk
Thank you very much!
And we can get one file size with this code:

for onefile in bucket.objects.filter(Prefix='<filename>'):
  print(onefile.size)

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