Skip to content

Commit

Permalink
Python: S3 Update Hello S3 to use pagination. (#7185)
Browse files Browse the repository at this point in the history
Updates to hello S3 python example.
  • Loading branch information
rlhagerm authored Jan 10, 2025
1 parent 21aea1b commit 7fdc2cd
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions python/example_code/s3/s3_basics/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down

0 comments on commit 7fdc2cd

Please sign in to comment.