Skip to content

Commit

Permalink
Updates to hello S3 python example.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlhagerm committed Jan 8, 2025
1 parent db53bdc commit 2451f97
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 2451f97

Please sign in to comment.