java 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 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.

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;

}

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.

The updated one

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'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.

This is done most easily via one of the respective methods of Class AmazonS3Client, e.g. 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());

}

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.

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值