From 3d2f2b32d5295afbe0da9d1a05a6486d5ae82807 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Tue, 14 Jan 2025 13:44:49 -0500 Subject: [PATCH] updated the S3 listbucket example to use pagnation --- javav2/example_code/s3/pom.xml | 2 +- .../main/java/com/example/s3/ListBuckets.java | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/javav2/example_code/s3/pom.xml b/javav2/example_code/s3/pom.xml index e211d6e5572..6ae70b2bae3 100644 --- a/javav2/example_code/s3/pom.xml +++ b/javav2/example_code/s3/pom.xml @@ -58,7 +58,7 @@ software.amazon.awssdk bom - 2.26.14 + 2.29.45 pom import diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java b/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java index 519d2fd91ba..d313e12ee05 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java @@ -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; /** @@ -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 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] \ No newline at end of file