Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:40:54+00:00 2026-05-30T09:40:54+00:00

We are using a java class to dowload a file from AWS s3 bucket

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:40:56+00:00Added an answer on May 30, 2026 at 9:40 am

    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.

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following code to download a file from my server then
I am using the URLUTF8Encoder.java class from W3C ( www.w3.org/International/URLUTF8Encoder.java ). Currently, it will
Using Eclipse I want to view the source code for a core Java class
I'm using the Scanner class in java to go through a a text file
I used following code for downloading XML file from ftp to android phone memory
I'm trying to download a gzipped file (~390Mo) from a FTP file using java.
I'm using Java XPath API to extract content from a xhtml file. I'm pasring
Greetings all, I am trying to download 'gz' file using URL class .Code snippet
I'm using Java's Transformer class to process an XML Document object. This is the
I am using java.util.Timer class and I am using its schedule method to perform

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.