AWS Java SDK - Detect if S3 Object exists using doesObjectExist

I was writing a test application which is hosted on EC2 on Amazon Web Services (AWS) and one of the test objectives was to determine if a object on Amazon S3 exists on a certain Bucket.

While googling around, I could not really get an example on this, so thought I'd write this post.

In the following example I will show you how to accomplish a simple task, where we need to determine if a Object on Amazon S3 exists.

We are using the aws-sdk for Java to accomplish the above mentioned task.

Java Code:

import java.io.IOException;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;

public class S3Sample {

    public static void main(String[] args) throws IOException {
        AWSCredentials credentials = null;
        try {
            credentials = new ProfileCredentialsProvider("default").getCredentials();
        } catch (Exception e) {
            throw new AmazonClientException(
                    "Cannot load the credentials from the credential profiles file. " +
                    "Please make sure that your credentials file is at the correct " +
                    "location (/home/username/.aws/credentials), and is in valid format.",

        AmazonS3 s3 = new AmazonS3Client(credentials);
        Region euWest1= Region.getRegion(Regions.EU_WEST_1);
        String bucketname = "myS3Bucket";
        String mys3object = "data/myTestDataObject";
        try {
            boolean exists = s3.doesObjectExist(bucketname, mys3object);
            if (exists) {
            	System.out.println("Object \"" + bucketname + "/" + mys3object + "\" exists!");
            else {
            	System.out.println("Object \"" + bucketname + "/" + mys3object + "\" does not exist!");
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which means your request made it "
                    + "to Amazon S3, but was rejected with an error response for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with S3, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());

End Result:

If the object is found, you will be returned with:

Object "myS3Bucket/data/myTestDataObject" exists!

And if the object is not found:

Object "myS3Bucket/data/myInvalidDataObject" does not exist!

Documentation Reference