0
votes

I'm using the AWS SDK v2 for S3 software.amazon.awssdk.s3. I have a bucket and I'm listing the keys to retrieve the objects. I'm expecting to list object keys, and get something like this:

...
ACTIVE/somefile.jpg
ACTIVE/someotherfile.jpg
...

However, the listing process will also return the root-key so to speak of

ACTIVE/

When I go to run the GetOjbectRequest for the key ACTIVE/, it throws an error because you can't retrieve that. So how do I pass to my request that I either only want to list keys of actual objects, or filter my response so that I only have actual objects before GetOjbectRequest-ing them?

My code currently looks like the following:

List<String> activeRuleKeys = listObjectsV2Responses.stream()
            .map(listObjectsV2Response -> listObjectsV2Response.contents())
            .flatMap(s3Objects -> s3Objects.stream())
            .map(s3Object -> s3Object.key())
            .collect(Collectors.toList());

// Error happens below
List<String> ruleSet = activeRuleKeys.stream().map(s -> {
        GetObjectRequest build = GetObjectRequest.builder().bucket(bucketName).key(s).build();
        String ruleString = this.s3Client.getObject(build, ResponseTransformer.toBytes()).asUtf8String();
        return ruleString;
    }).collect(Collectors.toList());

Any help is greatly appreciated

1

1 Answers

0
votes

You will need to use the attribute prefix in order to filter "folders" in your example. Something like that :

ListObjectsV2Request.builder()
  .bucket("your-bucket")
  .prefix("ACTIVE/")
  .build();

And remember that even if in the console you can create a "folder", behind the scenes it is not. It's just an object that AWS stores. That's why there is no built-in option in the SDK to create/list/whatever folders.