如何使用AWS Java SDK for S3查询匹配对象(文件)名称的AWS S3存储桶。

huangapple go评论153阅读模式
英文:

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 &lt;bucket-name&gt; --query &quot;Contents[?contains(Key, &#39;1018441&#39;)]&quot;

result:

 {
        &quot;LastModified&quot;: &quot;2020-11-31T20:36:28.000Z&quot;,
        &quot;ETag&quot;: &quot;\&quot;b861daa5cc3775f38519f5de6566cbe7\&quot;&quot;,
        &quot;StorageClass&quot;: &quot;STANDARD&quot;,
        &quot;Key&quot;: &quot;clients/&lt;client name&gt;/programs/0ced4d20939fe16978df9e6d8f8985ad/test123-94343.pdf&quot;,
        &quot;Owner&quot;: {
            &quot;DisplayName&quot;: &quot;owner&quot;,
            &quot;ID&quot;: &quot;123&quot;
        },
        &quot;Size&quot;: 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&lt;S3ObjectSummary&gt; execute(String key) {
    var keyLower = key.toLowerCase();
    var keys = new ArrayList&lt;S3ObjectSummary&gt;();
    ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(bucketName);
    ListObjectsV2Result result;
    try {
        do {
            result = s3Client.listObjectsV2(request);
            result.getObjectSummaries()
                    .stream()
                    .filter(obj -&gt; 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;

huangapple
  • 本文由 发表于 2020年10月12日 06:55:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64309798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定