Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In this tutorial, we'll explore how to check if a specified key exists in an Amazon S3 bucket using Java.

S3 is a popular cloud storage service that provides a scalable, secure, and highly available platform for storing and retrieving data.

It's essential for developers to know that a specific key exists to manipulate or access it as needed. We'll walk through the steps required to set up the AWS SDK and use it to perform this check.

2. Maven Dependencies

Before we get started, we need to declare the AWS S3 SDK dependency in our project's pom.xml:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.12.440</version>
</dependency>

3. Create an Instance of the AmazonS3 Client

Once we have the AWS SDK for Java set up, we create an instance of the AmazonS3 client to interact with a bucket.

Let's specify the AWS credentials and the bucket location region and create the client:

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
  .withRegion(Regions.US_EAST_1)
  .withCredentials(new AWSStaticCredentialsProvider(credentials))
  .build();

4. Check if the Key Exists With doesObjectExists()

The easiest and most obvious way to check if a specific key exists in the S3 bucket is to use the doesObjectExists() method:

boolean exists = s3Client.doesObjectExist(bucketName, key);

This method checks if an object exists at the specified bucket location and returns a boolean value indicating its presence. This method can easily verify if the object is available for manipulation or access as needed.

It's a simple and effective way to ensure an object exists in the S3 bucket, avoiding potential errors due to non-existent keys.

5. Check if the Key Exists With getObjectMetadata()

If the previous method isn't available in our version of the SDK, we can write our own using getObjectMetadata().

We just need to specify the object's key and bucket name. If the object exists, the method will return metadata, such as the object size, content type, and other relevant information.

However, if the object doesn't exist, the method will throw an exception. Therefore, we should wrap our code in a trycatch block to handle this situation appropriately:

boolean doesObjectExistByMetaData(String bucket, String key) {
    try {
        s3Client.getObjectMetadata(bucket, key);
        return true;
    } catch (AmazonServiceException e) {
        if (e.getStatusCode() == 404) {
            return false;
        } else {
            throw e;
        }
    }
}

6. Check if the Key Exists With listObjects()

Another option is to use the listObjects() method. This method returns a list of objects metadata in the specified bucket. We can then iterate through this list to check if the desired key is present:

boolean doesObjectExistByListObjects(String bucket, String key) {
    return s3Client.listObjects(bucket)
      .getObjectSummaries()
      .stream()
      .filter(s3ObjectSummary -> s3ObjectSummary.getKey().equals(key))
      .findFirst()
      .isPresent();
}

While this method may be less efficient than doesObjectExists() or getObjectMetadata(), it can be helpful when those options are unavailable.

Additionally, listObjects() has the added benefit of allowing us to list multiple objects simultaneously. It may be helpful in specific scenarios.

However, this method can be slower and less efficient due to many iterations. It's crucial to weigh the tradeoffs and choose the best option for the use case.

7. Conclusion

In this article, we've explored several ways to check if a specific key exists in an S3 bucket using AWS SDK for Java.

We've learned how to set up the AmazonS3 client and use the doesObjectExist() method to check for the existence of a key. We've also explored getObjectMetadata() and listObjects() methods as alternative check methods.

Each method has its advantages and disadvantages, and the choice of which one to use will depend on the specific requirements of the use case.

As always, the complete code of this article is available over on GitHub.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Cloud footer banner
Comments are closed on this article!