英文:
how to query AWS S3 bucket for matching objects(files) names using AWS Java SDK for S3
问题
我可以搜索S3存储桶中包含test123文件名的文件。由于这是对生产数据的查询,我稍微对路径进行了一些编辑,以下是代码示例。我尝试在AWS V2 SDK中搜索文档,但没有成功。您能否分享是否有使用类似于AWS CLI的AWS SDK进行此操作的方法?如果有人能够提供一个示例的参考,将会很有帮助。谢谢
$ aws s3api list-objects --bucket <bucket-name> --query "Contents[?contains(Key, '1018441')]"
结果:
{
"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
}
英文:
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
}
答案1
得分: 4
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;
}
英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论