-
-
Save filipeandre/e8d8673a3ba598ba89dc0c66b7aa7834 to your computer and use it in GitHub Desktop.
s3 list paginator tricks.
This file contains hidden or 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_client = boto3.client('s3') | |
def list_dirs(bucket, prefix): | |
""" Yield direct child folders of the given prefix. | |
""" | |
if not prefix.endswith('/'): | |
prefix += '/' | |
paginator = s3_client.get_paginator('list_objects_v2') | |
results = paginator.paginate(Bucket=bucket, Prefix=prefix, Delimiter='/') | |
for result in results: | |
for prefix in result.get('CommonPrefixes', []): | |
# Prefixes look like "<prefix>/<subdir>/" | |
# This code replaces "<prefix>/" with an empty | |
# space leaving "<subdir>" from the common prefix. | |
yield prefix['Prefix'].replace(prefix, '', 1).strip('/') | |
def list_s3_keys(bucket, prefix='', suffix=''): | |
s3_client = boto3.client('s3') | |
params = {'Bucket': bucket} | |
if isinstance(prefix, str): | |
params['Prefix'] = prefix | |
paginator = s3_client.get_paginator('list_objects_v2') | |
for result in paginator.paginate(**params): | |
for obj in result['Contents']: | |
key = obj['Key'] | |
if key.startswith(prefix) and key.endswith(suffix): | |
yield key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment