File tree Expand file tree Collapse file tree 1 file changed +9
-8
lines changed
javav2/example_code/s3/src/main/java/com/example/s3 Expand file tree Collapse file tree 1 file changed +9
-8
lines changed Original file line number Diff line number Diff line change 88import software .amazon .awssdk .services .s3 .S3Client ;
99import software .amazon .awssdk .services .s3 .model .Bucket ;
1010import software .amazon .awssdk .services .s3 .model .ListBucketsResponse ;
11+ import software .amazon .awssdk .services .s3 .paginators .ListBucketsIterable ;
12+
1113import java .util .List ;
1214
1315/**
@@ -35,14 +37,13 @@ public static void main(String[] args) {
3537 * @param s3 The {@link S3Client} instance to use for interacting with the Amazon S3 service.
3638 */
3739 public static void listAllBuckets (S3Client s3 ) {
38- ListBucketsResponse response = s3 .listBuckets ();
39- List <Bucket > bucketList = response .buckets ();
40- // Print bucket names
41- System .out .println ("Your Amazon S3 buckets are:" );
42- for (Bucket bucket : bucketList ) {
43- System .out .println (bucket .name ());
44- System .out .println (bucket .creationDate ());
45- }
40+ ListBucketsIterable response = s3 .listBucketsPaginator ();
41+
42+ // Iterate over each response page and print bucket names.
43+ response .stream ()
44+ .map (ListBucketsResponse ::buckets )
45+ .flatMap (buckets -> buckets .stream ())
46+ .forEach (bucket -> System .out .println ("Bucket Name: " + bucket .name ()));
4647 }
4748}
4849// snippet-end:[s3.java2.list.buckets.main]
You can’t perform that action at this time.
0 commit comments