File tree Expand file tree Collapse file tree 1 file changed +27
-4
lines changed
python/example_code/s3/s3_basics Expand file tree Collapse file tree 1 file changed +27
-4
lines changed Original file line number Diff line number Diff line change 88def 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
2144if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments