@@ -0,0 +1,2152 @@
+//snippet-sourcedescription:%5BListObjects.java demonstrates how to list objects located in a given bucket.%5D%0A//snippet-keyword:%5BSDK for Java 2.0%5D%0A//snippet-keyword:%5BCode Sample%5D%0A//snippet-service:%5Bs3%5D%0A//snippet-sourcetype:%5Bfull-example%5D%0A//snippet-sourcedate:%5B%5D%0A//snippet-sourceauthor:%5Bscmacdon-aws%5D%0A%0Apackage com.example.s3;%0A%0A// snippet-start:%5Bs3.java2.list_objects.complete%5D%0A// snippet-start:%5Bs3.java2.list_objects.import%5D%0Aimport software.amazon.awssdk.regions.Region;%0Aimport software.amazon.awssdk.services.s3.S3Client;%0Aimport software.amazon.awssdk.services.s3.model.*;%0Aimport java.util.List;%0Aimport java.util.ListIterator;%0A// snippet-end:%5Bs3.java2.list_objects.import%5D%0A%0A%0Apublic class ListObjects %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A%0A        if (args.length %3C 1) %7B%0A            System.out.println(%22Please specify a bucket name%22);%0A            System.exit(1);%0A        %7D%0A%0A        // snippet-start:%5Bs3.java2.list_objects.main%5D%0A       String bucketName = args%5B0%5D;%0A%0A        try %7B%0A            Region region = Region.US_WEST_2;%0A            S3Client s3 = S3Client.builder().region(region).build();%0A%0A            ListObjectsRequest listObjects = ListObjectsRequest%0A                    .builder()%0A                    .bucket(bucketName)%0A                    .build();%0A%0A            ListObjectsResponse res = s3.listObjects(listObjects);%0A            List%3CS3Object%3E objects = res.contents();%0A%0A            for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) %7B%0A                S3Object myValue = (S3Object) iterVals.next();%0A                System.out.print(%22%5Cn The name of the key is %22 + myValue.key());%0A                System.out.print(%22%5Cn The object is %22 + calKb(myValue.size()) + %22 KBs%22);%0A                System.out.print(%22%5Cn The owner is %22 + myValue.owner());%0A            %7D%0A        %7D%0A        catch (S3Exception e) %7B%0A            System.err.println(e.awsErrorDetails().errorMessage());%0A            System.exit(1);%0A        %7D%0A    %7D%0A%0A    //convert bytes to kbs%0A    private static long calKb(Long val)%0A    %7B%0A        return val/1024;%0A%0A    %7D%0A    // snippet-end:%5Bs3.java2.list_objects.main%5D%0A%7D%0A// snippet-end:%5Bs3.java2.list_objects.complete%5D%0A

Create ListObjects.java