How to list all AWS S3 objects in a bucket using Java

后端 未结 11 1298
攒了一身酷
攒了一身酷 2020-11-30 20:34

What is the simplest way to get a list of all items within an S3 bucket using Java?

List s3objects = s3.listObjects(bucketName,prefix)         


        
11条回答
  •  长情又很酷
    2020-11-30 21:29

    Try this one out

    public void getObjectList(){
            System.out.println("Listing objects");
            ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
                    .withBucketName(bucketName)
                    .withPrefix("ads"));
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                System.out.println(" - " + objectSummary.getKey() + "  " +
                                   "(size = " + objectSummary.getSize() + ")");
            }
        }
    

    You can all the objects within the bucket with specific prefix.

提交回复
热议问题