Reputation: 417
I am able to search for a file name containing test123 in the S3 bucket. I have slightly redacted some bits of the path as this is a query of production data in the below code example. I tried to search AWS documentation for SDK V2, but no luck. Can you please share if there is any way to do this using AWS SDK similar to AWS CLI? It would be helpful if anyone can share a reference to a sample. Thanks
$ aws s3api list-objects --bucket <bucket-name> --query "Contents[?contains(Key, '1018441')]"
result:
{
"LastModified": "2020-11-31T20:36:28.000Z",
"ETag": "\"b861daa5cc3775f38519f5de6566cbe7\"",
"StorageClass": "STANDARD",
"Key": "clients/<client name>/programs/0ced4d20939fe16978df9e6d8f8985ad/test123-94343.pdf",
"Owner": {
"DisplayName": "owner",
"ID": "123"
},
"Size": 27032
}
Upvotes: 2
Views: 2177
Reputation: 562
import com.amazonaws.services.s3.model.*;
public List<S3ObjectSummary> execute(String key) {
var keyLower = key.toLowerCase();
var keys = new ArrayList<S3ObjectSummary>();
ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(bucketName);
ListObjectsV2Result result;
try {
do {
result = s3Client.listObjectsV2(request);
result.getObjectSummaries()
.stream()
.filter(obj -> obj.getKey().toLowerCase().contains(keyLower))
.forEach(keys::add);
var token = result.getContinuationToken();
request.setContinuationToken(token);
} while (result.isTruncated());
} catch (AmazonS3Exception e) {
LOG.error(e.getAdditionalDetails().entrySet().toString());
throw e;
}
return keys;
Upvotes: 4