title: AWS S3 file search using Java
body: <p>We are using a java class to dowload a file from AWS s3 bucket with the following code </p>
  
  <pre><code>inputStream = AWSFileUtil.getInputStream(
              AWSConnectionUtil.getS3Object(null),
              "cdn.generalsentiment.com", filePath);
  </code></pre>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>Please suggest me a solution for this.</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>The updated one </p>
  
  <pre><code>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;
  }
  </code></pre>