Skip to content

Commit 165c265

Browse files
rlhagermDavidSouther
authored andcommitted
Python: S3 Update Hello S3 to use pagination. (awsdocs#7185)
Updates to hello S3 python example.
1 parent 88bb20e commit 165c265

File tree

1 file changed

+27
-4
lines changed
  • python/example_code/s3/s3_basics

1 file changed

+27
-4
lines changed

python/example_code/s3/s3_basics/hello.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,37 @@
88
def hello_s3():
99
"""
1010
Use the AWS SDK for Python (Boto3) to create an Amazon Simple Storage Service
11-
(Amazon S3) resource and list the buckets in your account.
11+
(Amazon S3) client and list the buckets in your account.
1212
This example uses the default settings specified in your shared credentials
1313
and config files.
1414
"""
15-
s3_resource = boto3.resource("s3")
15+
16+
# Create an S3 client.
17+
s3_client = boto3.client("s3")
18+
1619
print("Hello, Amazon S3! Let's list your buckets:")
17-
for bucket in s3_resource.buckets.all():
18-
print(f"\t{bucket.name}")
20+
21+
# Create a paginator for the list_buckets operation.
22+
paginator = s3_client.get_paginator("list_buckets")
23+
24+
# Use the paginator to get a list of all buckets.
25+
response_iterator = paginator.paginate(
26+
PaginationConfig={
27+
"PageSize": 50, # Adjust PageSize as needed.
28+
"StartingToken": None,
29+
}
30+
)
31+
32+
# Iterate through the pages of the response.
33+
buckets_found = False
34+
for page in response_iterator:
35+
if "Buckets" in page and page["Buckets"]:
36+
buckets_found = True
37+
for bucket in page["Buckets"]:
38+
print(f"\t{bucket['Name']}")
39+
40+
if not buckets_found:
41+
print("No buckets found!")
1942

2043

2144
if __name__ == "__main__":

0 commit comments

Comments
 (0)