且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Java进行AWS S3文件搜索

更新时间:2021-10-20 22:28:16

Daan的回答使用 GET Bucket(列出对象)(通过 AWS for Java ,见下文)是一次获得多个对象所需信息的最有效方法(+1),当然,你需要相应地发布响应。

Daan's answer using GET Bucket (List Objects) (via the respective wrapper from the AWS for Java, see below) is the most efficient approach to get the desired information for many objects at once (+1), you'll need to post process the response accordingly of course.

这最容易通过类AmazonS3Client ,例如 listObjects(String bucketName)

AmazonS3 s3 = new AmazonS3Client(); // provide credentials, if need be
ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
        .withBucketName("cdn.generalsentiment.com");
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
    System.out.println(objectSummary.getKey());
}



替代



如果您一次只对单个对象(文件)感兴趣,请使用 HEAD对象将更加高效,只要您可以直接从相应的HTTP响应代码中推断存在(参见错误响应详情),即 404 Not Found 对于 NoSuchKey的响应 - 指定的密钥不存在

Alternative

If you are only interested in a single object (file) at a time, using HEAD Object will be much more efficient, insofar you can deduce existence straight from the respective HTTP response code (see Error Responses for details), i.e. 404 Not Found for a response of NoSuchKey - The specified key does not exist.

同样,这可以通过类AmazonS3Client ,即 getObjectMetadata(String bucketName,String key),例如:

Again, this is done most easily via Class AmazonS3Client, namely getObjectMetadata(String bucketName, String key), e.g.:

public static boolean isValidFile(AmazonS3 s3,
        String bucketName,
        String path) throws AmazonClientException, AmazonServiceException {
    boolean isValidFile = true;
    try {
        ObjectMetadata objectMetadata = s3.getObjectMetadata(bucketName, path);
    } catch (AmazonS3Exception s3e) {
        if (s3e.getStatusCode() == 404) {
        // i.e. 404: NoSuchKey - The specified key does not exist
            isValidFile = false;
        }
        else {
            throw s3e;    // rethrow all S3 exceptions other than 404   
        }
    }

    return isValidFile;
}