Created
March 25, 2023 04:50
-
-
Save 100daysofdevops/6d8e69495e0f876f746bd02d82324056 to your computer and use it in GitHub Desktop.
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 = boto3.client('s3') | |
paginator = s3.get_paginator('list_objects_v2') | |
page_size = 100 | |
bucket_name = 'your-bucket-name' | |
prefix = 'your-prefix' | |
response_iterator = paginator.paginate( | |
Bucket=bucket_name, | |
Prefix=prefix, | |
PaginationConfig={ | |
'PageSize': page_size | |
} | |
) | |
results = [] | |
for page in response_iterator: | |
page_results = page['Contents'] | |
results.extend(page_results) | |
total_results = len(results) | |
total_pages = (total_results + page_size - 1) // page_size | |
# Now you can return the total number of pages to your frontend app | |
# and when the user requests a particular page, you can adjust the | |
# `PageNumber` parameter in the `paginate` method accordingly to | |
# retrieve only the results for that page. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment