//snippet-sourcedescription:[ListObjects.java demonstrates how to list objects located in a given bucket.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[s3]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[]
//snippet-sourceauthor:[scmacdon-aws]

package com.example.s3;

// snippet-start:[s3.java2.list_objects.complete]
// snippet-start:[s3.java2.list_objects.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import java.util.List;
import java.util.ListIterator;
// snippet-end:[s3.java2.list_objects.import]


public class ListObjects {

    public static void main(String[] args) {

        if (args.length < 1) {
            System.out.println("Please specify a bucket name");
            System.exit(1);
        }

        // snippet-start:[s3.java2.list_objects.main]
       String bucketName = args[0];

        try {
            Region region = Region.US_WEST_2;
            S3Client s3 = S3Client.builder().region(region).build();

            ListObjectsRequest listObjects = ListObjectsRequest
                    .builder()
                    .bucket(bucketName)
                    .build();

            ListObjectsResponse res = s3.listObjects(listObjects);
            List<S3Object> objects = res.contents();

            for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
                S3Object myValue = (S3Object) iterVals.next();
                System.out.print("\n The name of the key is " + myValue.key());
                System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
                System.out.print("\n The owner is " + myValue.owner());
            }
        }
        catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }

    //convert bytes to kbs
    private static long calKb(Long val)
    {
        return val/1024;

    }
    // snippet-end:[s3.java2.list_objects.main]
}
// snippet-end:[s3.java2.list_objects.complete]

@@ -1,24 +1,575 @@
+/**%0A * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.%0A *%0A * This file is licensed under the Apache License, Version 2.0 (the %22License%22).%0A * You may not use this file except in compliance with the License. A copy of%0A * the License is located at%0A *%0A * http://aws.amazon.com/apache2.0/%0A *%0A * This file is distributed on an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR%0A * CONDITIONS OF ANY KIND, either express or implied. See the License for the%0A * specific language governing permissions and limitations under the License.%0A *%0A */%0A%0A
 //snippet-sourcedescript
@@ -651,16 +651,16 @@
 ucket.%5D%0A
-
 //snippe
@@ -1115,10 +1115,216 @@
 del.
-*;
+ListObjectsRequest;%0Aimport software.amazon.awssdk.services.s3.model.ListObjectsResponse;%0Aimport software.amazon.awssdk.services.s3.model.S3Exception;%0Aimport software.amazon.awssdk.services.s3.model.S3Object;%0A
 %0Aimp
@@ -1421,17 +1421,16 @@
 mport%5D%0A%0A
-%0A
 public c
@@ -1683,16 +1683,17 @@
 s.main%5D%0A
+ 
        S
@@ -1720,16 +1720,16 @@
 rgs%5B0%5D;%0A
-
 %0A       
@@ -2553,24 +2553,16 @@
        %7D
-%0A       
  catch (
@@ -2718,16 +2718,16 @@
  to kbs%0A
+
     priv
@@ -2757,20 +2757,16 @@
 ong val)
-%0A   
  %7B%0A     

Update ListObjects.java