We are using a java class to dowload a file from AWS s3 bucket with the following code
inputStream = AWSFileUtil.getInputStream(
AWSConnectionUtil.g
Use the GET Bucket S3 API:
http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGET.html
and specify the full file name as a prefix.
this is simple way to find existing folder in bucket. above answer also true. folder name contain '/' at last , it return true .
note: mybucket/userProfileModule/abc.pdf, it my folder structrue
boolean result1 = s3client.doesObjectExist("mybucket", "userProfileModule/");
System.out.println(result);
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());
}
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.
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;
}