I\'ve created a hierarchy in S3 via the AWS S3 Management Console. If I run the following code to list the bucket:
AmazonS3 s3 = new AmazonS3Client(CRED);
Li
This below code worked for me to list all directories in s3.
private static String bucket_name = "";
private static String secret_key = "";
private static String access_key = "";
private static String Regions region = Regions.SELECT_REGION;
public static void main(String[] args) {
System.out.println(listKeysInBucket(bucket_name, "/"));
}
public static List listKeysInBucket(String bucketName, String prefix) {
boolean isTopLevel = false;
String delimiter = "/";
if (prefix.equals("") || prefix.equals(delimiter)) {
isTopLevel = true;
}
if (!prefix.endsWith(delimiter)) {
prefix += delimiter;
}
ListObjectsRequest listObjectsRequest = null;
if (isTopLevel) {
listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withDelimiter(delimiter);
} else {
listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(prefix)
.withDelimiter(delimiter);
}
ObjectListing objects = s3Client().listObjects(listObjectsRequest);
return objects.getCommonPrefixes();
}
public static AmazonS3 s3Client() {
AWSCredentials s3Configs = new BasicAWSCredentials(access_key,secret_key);
return AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(s3Configs)).withRegion(region )
.build();
}