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