使用Java进行AWS S3文件搜索 [英] AWS S3 file search using Java

查看:1639
本文介绍了使用Java进行AWS S3文件搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用java类从AWS s3存储桶下载文件,其中包含以下代码

We are using a java class to dowload a file from AWS s3 bucket with the following code

inputStream = AWSFileUtil.getInputStream(
            AWSConnectionUtil.getS3Object(null),
            "cdn.generalsentiment.com", filePath);

AWSFileUtil是一个检查凭据并使用getInputStream方法从S3bucket获取输入流的类。是cdn.generalsentiment.com存储桶中的文件。

AWSFileUtil is a class which check the credentials and gets the inputstream from S3bucket using the getInputStream method.The filePath is the file inside cdn.generalsentiment.com bucket.

我们想编写一个方法,只需检查AWS S3存储桶中是否存在特定文件,并返回布尔值或其他值。

We want to write a method which can just check whether the particular file exists or not in the AWS S3 bucket and returns a boolean or some other value.

请为我推荐一个解决方案。

Please suggest me a solution for this.

public static boolean isValidFile(AmazonS3 s3,
        String bucketName,
        String path) throws AmazonClientException {
    try {
        ObjectMetadata objectMetadata =  
s3.getObjectMetadata("cdn.generalsentiment.com", path);
    } catch (NotFoundException nfe) {
        nfe.printStackTrace();
    }

    return true;
}

如果文件存在则返回true,否则抛出NotFoundException,我想要捕获并返回isValidFile方法结果为false。
对于方法体或返回类型,任何其他替代方案都会很棒。

If the file exists it returns true, else it throws NotFoundException, which i want to catch and return the "isValidFile" method result as false. Guys any other alternative for the method body or return type would be great.

更新后的

public static boolean doesFileExist(AmazonS3 s3,
        String bucketName,
        String path) throws AmazonClientException,
        AmazonServiceException {
    boolean isValidFile = true;
    try {
        ObjectMetadata objectMetadata = 
s3.getObjectMetadata("cdn.generalsentiment.com", path);

    } catch (NotFoundException nfe) {
        isValidFile = false;
    }
   catch (Exception exception) {
        exception.printStackTrace();
        isValidFile = false;
    }
    return isValidFile;
}


推荐答案

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;
}

这篇关于使用Java进行AWS S3文件搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆