Skip to content

Commit

Permalink
updated the S3 listbucket example to use pagnation
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon committed Jan 14, 2025
1 parent f78a78b commit 3d2f2b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion javav2/example_code/s3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.26.14</version>
<version>2.29.45</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.Bucket;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
import software.amazon.awssdk.services.s3.paginators.ListBucketsIterable;

import java.util.List;

/**
Expand Down Expand Up @@ -35,14 +37,13 @@ public static void main(String[] args) {
* @param s3 The {@link S3Client} instance to use for interacting with the Amazon S3 service.
*/
public static void listAllBuckets(S3Client s3) {
ListBucketsResponse response = s3.listBuckets();
List<Bucket> bucketList = response.buckets();
// Print bucket names
System.out.println("Your Amazon S3 buckets are:");
for (Bucket bucket : bucketList) {
System.out.println(bucket.name());
System.out.println(bucket.creationDate());
}
ListBucketsIterable response = s3.listBucketsPaginator();

// Iterate over each response page and print bucket names.
response.stream()
.map(ListBucketsResponse::buckets)
.flatMap(buckets -> buckets.stream())
.forEach(bucket -> System.out.println("Bucket Name: " + bucket.name()));
}
}
// snippet-end:[s3.java2.list.buckets.main]

0 comments on commit 3d2f2b3

Please sign in to comment.