From 7fdc2cdaeab1c5a0a17a0f2be20687e367a27ce6 Mon Sep 17 00:00:00 2001 From: Rachel Hagerman <110480692+rlhagerm@users.noreply.github.com> Date: Fri, 10 Jan 2025 08:28:23 -0600 Subject: [PATCH] Python: S3 Update Hello S3 to use pagination. (#7185) Updates to hello S3 python example. --- python/example_code/s3/s3_basics/hello.py | 31 ++++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/python/example_code/s3/s3_basics/hello.py b/python/example_code/s3/s3_basics/hello.py index 20bbb8a1b31..9291f2b4ca1 100644 --- a/python/example_code/s3/s3_basics/hello.py +++ b/python/example_code/s3/s3_basics/hello.py @@ -8,14 +8,37 @@ def hello_s3(): """ Use the AWS SDK for Python (Boto3) to create an Amazon Simple Storage Service - (Amazon S3) resource and list the buckets in your account. + (Amazon S3) client and list the buckets in your account. This example uses the default settings specified in your shared credentials and config files. """ - s3_resource = boto3.resource("s3") + + # Create an S3 client. + s3_client = boto3.client("s3") + print("Hello, Amazon S3! Let's list your buckets:") - for bucket in s3_resource.buckets.all(): - print(f"\t{bucket.name}") + + # Create a paginator for the list_buckets operation. + paginator = s3_client.get_paginator("list_buckets") + + # Use the paginator to get a list of all buckets. + response_iterator = paginator.paginate( + PaginationConfig={ + "PageSize": 50, # Adjust PageSize as needed. + "StartingToken": None, + } + ) + + # Iterate through the pages of the response. + buckets_found = False + for page in response_iterator: + if "Buckets" in page and page["Buckets"]: + buckets_found = True + for bucket in page["Buckets"]: + print(f"\t{bucket['Name']}") + + if not buckets_found: + print("No buckets found!") if __name__ == "__main__":