product-images/123/main-image.png
Here, the words product-images and 123 are just parts of the object’s key - they are not separate folders as you usually see on a file system. That’s the important difference you must know when working with S3’s objects.package net.codejava.aws; import java.util.List; import java.util.ListIterator; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.ListObjectsRequest; import software.amazon.awssdk.services.s3.model.ListObjectsResponse; import software.amazon.awssdk.services.s3.model.S3Object; public class ListObjectsExample1 { public static void main(String[] args) { String bucketName = "nam-public-images"; S3Client client = S3Client.builder().build(); ListObjectsRequest request = ListObjectsRequest.builder().bucket(bucketName).build(); ListObjectsResponse response = client.listObjects(request); List<S3Object> objects = response.contents(); ListIterator<S3Object> listIterator = objects.listIterator(); while (listIterator.hasNext()) { S3Object object = listIterator.next(); System.out.println(object.key() + " - " + object.size()); } } }I think the code example is straightforward and self-explanatory. You need to build a ListObjectsRequest object, pass the bucket name, call listObjects() method of the S3Client object, and get the response as a ListObjectsResponse object.
brand-logos/1/Canon.png - 4010 brand-logos/10/Samsung.png - 7594 brand-logos/11/Olympus.png - 13392 category-images/1/electronics.png - 117245 category-images/10/digital cameras.png - 29225 category-images/11/flashes.png - 23178 product-images/1/canon eos m50.png - 437094 product-images/1/extras/EOS M50 LCD.png - 196919 product-images/1/extras/EOS M50 flash opened.png - 473993 product-images/1/extras/EOS M50 incline.png - 399941 product-images/1/extras/EOS M50 straight.png - 351950It lists up to 1,000 objects in the given bucket.
String bucketName = "nam-public-images"; String folderName = "product-images"; S3Client client = S3Client.builder().build(); ListObjectsRequest request = ListObjectsRequest.builder() .bucket(bucketName) .prefix(folderName).build(); ListObjectsResponse response = client.listObjects(request); List<S3Object> objects = response.contents(); ListIterator<S3Object> listIterator = objects.listIterator(); while (listIterator.hasNext()) { S3Object object = listIterator.next(); System.out.println(object.key() + " - " + object.size()); }The result contains only objects whose keys starting with the specified prefix “product-images”.
ListObjectsRequest request = ListObjectsRequest.builder() .bucket(bucketName) .prefix(folderName) .maxKeys(100) .build();This tell Amazon S3 to send maximum 100 objects in the response.In case you want to exclude objects whose keys contain a given string, use the delimiter() method. For example:
ListObjectsRequest request = ListObjectsRequest.builder() .bucket(bucketName) .prefix(“product-images”) .delimiter("extras") .build();This tell Amazon S3 to get only objects whose keys starting with the prefix “product-images”, and do not contain the word “extras”.For handling exception, you can catch these runtime exceptions which may be thrown by the listObjects() method: NoSuchBucketException, AwsServiceException, SdkClientException and S3Exception.So that’s I have shared with you some Java code examples for listing objects in a bucket on Amazon S3 server. You also learned how to list objects in a “folder” and filter the result using max keys and delimiter.To see the coding in action, I recommend you watch the following video: