Skip to content

Instantly share code, notes, and snippets.

@JoshCrosby
Created November 5, 2020 14:52
Show Gist options
  • Save JoshCrosby/384460b2895f2d14dcd4025d56f1fa9a to your computer and use it in GitHub Desktop.
Save JoshCrosby/384460b2895f2d14dcd4025d56f1fa9a to your computer and use it in GitHub Desktop.
Get more than 1000 objects for list objects
# https://stackoverflow.com/questions/54314563/how-to-get-more-than-1000-objects-from-s3-by-using-list-objects-v2/54314628
def get_all_s3_objects(s3, **base_kwargs):
continuation_token = None
while True:
list_kwargs = dict(MaxKeys=1000, **base_kwargs)
if continuation_token:
list_kwargs['ContinuationToken'] = continuation_token
response = s3.list_objects_v2(**list_kwargs)
yield from response.get('Contents', [])
if not response.get('IsTruncated'): # At the end of the list?
break
continuation_token = response.get('NextContinuationToken')
for file in get_all_s3_objects(boto3.client('s3'), Bucket=bucket, Prefix=prefix):
print(file['size'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment