Created
July 31, 2019 07:52
-
-
Save alunux/20a102128d5914747917fad977b9f453 to your computer and use it in GitHub Desktop.
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
import boto3 | |
s3_paginator = boto3.client('s3').get_paginator('list_objects_v2') | |
def keys(bucket, prefix='/', delimiter='/', start_after=''): | |
prefix = prefix[1:] if prefix.startswith(delimiter) else prefix | |
start_after = (start_after or prefix) if prefix.endswith(delimiter) else start_after | |
for page in s3_paginator.paginate(Bucket=bucket, Prefix=prefix, StartAfter=start_after): | |
for content in page.get('Contents', ()): | |
yield content['Key'] | |
list_files = keys(bucket=BUCKET_NAME, prefix=PREFIX) | |
fh = open('index', 'w') | |
for i in (list_files): | |
i = i.replace('PREFIX', '') | |
fh.write(f'{i}\n') | |
fh.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment