Use ListObjectsV2
with an AWS SDK
The following code example shows how to use ListObjectsV2
.
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. List objects in a directory bucket.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.S3Object; import java.nio.file.Path; import java.util.List; import java.util.stream.Collectors; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Lists objects in the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @return A list of object keys in the bucket */ public static List<String> listDirectoryBucketObjectsV2(S3Client s3Client, String bucketName) { logger.info("Listing objects in bucket: {}", bucketName); try { // Create a ListObjectsV2Request ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() .bucket(bucketName) .build(); // Retrieve the list of objects ListObjectsV2Response response = s3Client.listObjectsV2(listObjectsV2Request); // Extract and return the object keys return response.contents().stream() .map(S3Object::key) .collect(Collectors.toList()); } catch (S3Exception e) { logger.error("Failed to list objects: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } }
-
For API details, see ListObjectsV2 in AWS SDK for Java 2.x API Reference.
-
For a complete list of AWS SDK developer guides and code examples, see Developing with Amazon S3 using the AWS SDKs. This topic also includes information about getting started and details about previous SDK versions.
ListMultipartUploads
ListParts