簡體   English   中英

使用Java進行AWS S3文件搜索

[英]AWS S3 file search using Java

我們使用java類從AWS s3存儲桶下載文件,其代碼如下

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

AWSFileUtil是一個檢查憑據並使用getInputStream方法從S3bucket獲取輸入流的類.filePath是cdn.generalsentiment.com存儲桶中的文件。

我們想要編寫一種方法,只需檢查AWS S3存儲桶中是否存在特定文件,並返回布爾值或其他值。

請為我建議一個解決方案。

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。 伙計方法體或返回類型的任何其他替代方案都會很棒。

更新的一個

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),您需要發布流程當然是相應的回應。

這通過類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 for NoSuchKey的響應- 指定的鍵不存在

同樣,這最容易通過類AmazonS3Client完成 ,即getObjectMetadata(String bucketName,String key) ,例如:

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

使用GET Bucket S3 API:

http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGET.html

並指定完整的文件名作為前綴。

這是在存儲桶中查找現有文件夾的簡單方法。 以上答案也是如此。 文件夾名稱最后包含'/',它返回true。

注意:mybucket / userProfileModule / abc.pdf,它是我的文件夾structrue

    boolean result1 = s3client.doesObjectExist("mybucket", "userProfileModule/");
    System.out.println(result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM