Use ListDirectoryBuckets
with an AWS SDK
The following code example shows how to use ListDirectoryBuckets
.
- 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 all directory buckets.
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.Bucket; import software.amazon.awssdk.services.s3.model.ListDirectoryBucketsRequest; import software.amazon.awssdk.services.s3.model.ListDirectoryBucketsResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import java.util.List; import java.util.UUID; 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.deleteDirectoryBucket; /** * Lists all S3 directory buckets and no general purpose buckets. * * @param s3Client The S3 client used to interact with S3 * @return A list of bucket names */ public static List<String> listDirectoryBuckets(S3Client s3Client) { logger.info("Listing all directory buckets"); try { // Create a ListBucketsRequest ListDirectoryBucketsRequest listDirectoryBucketsRequest = ListDirectoryBucketsRequest.builder().build(); // Retrieve the list of buckets ListDirectoryBucketsResponse response = s3Client.listDirectoryBuckets(listDirectoryBucketsRequest); // Extract bucket names List<String> bucketNames = response.buckets().stream() .map(Bucket::name) .collect(Collectors.toList()); return bucketNames; } catch (S3Exception e) { logger.error("Failed to list buckets: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } }
-
For API details, see ListDirectoryBuckets 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.
HeadObject
ListMultipartUploads