Skip to content

Instantly share code, notes, and snippets.

@filipeandre
Forked from nackjicholson/s3_utils.py
Created January 7, 2023 14:26
Show Gist options
  • Save filipeandre/e8d8673a3ba598ba89dc0c66b7aa7834 to your computer and use it in GitHub Desktop.
Save filipeandre/e8d8673a3ba598ba89dc0c66b7aa7834 to your computer and use it in GitHub Desktop.
s3 list paginator tricks.
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