I have an s3 structure as follows:
s3bucketname -> List of first level keys -> List of second level keys -> List of third level keys -> Actual fi
I did the following code which seems to work fine, you have to pass a prefix and make sure the prefix ends with /, and also specify the delimiter you want to get your list of sub-directories. The following should work:
public List listKeysInDirectory(String bucketName, String prefix) {
String delimiter = "/";
if (!prefix.endsWith(delimiter)) {
prefix += delimiter;
}
ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(bucketName).withPrefix(prefix)
.withDelimiter(delimiter);
ObjectListing objects = _client.listObjects(listObjectsRequest);
return objects.getCommonPrefixes();
}