Created
January 28, 2019 12:54
-
-
Save robcowie/7391ea2781b593cc03455b72676c9d80 to your computer and use it in GitHub Desktop.
List S3 with pagination and metadata
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
def list_s3_with_metadata(s3_conn, prefix): | |
"""List all keys at `prefix` and return metadata.""" | |
bucket, prefix = prefix.split('://')[1].split('/', 1) | |
paginator = s3_conn.get_paginator('list_objects_v2') | |
response = paginator.paginate(Bucket=bucket, Prefix=prefix) | |
def attrs(d): | |
return {'Key': 's3://{}/{}'.format(bucket, d['Key']), 'ETag': d['ETag'].replace('"', ''), 'Size': d['Size']} | |
return (attrs(k) for page in response for k in page['Contents']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment