Created
November 5, 2020 14:52
-
-
Save JoshCrosby/384460b2895f2d14dcd4025d56f1fa9a to your computer and use it in GitHub Desktop.
Get more than 1000 objects for list objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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