Read, Write and Delete Operations for S3 Bucket using Spring boot

Chanaka MBK
5 min readDec 21, 2021

Hi All, In this post, I am going to describe “How to do a CRUD operation for S3 bucket using Spring boot project”. I am using a simple Spring boot project for adding/retrieving and deleting a file to show the implementation. Please see the completed bitbucket example from here.

Overview

An Amazon S3 bucket is a public cloud storage resource available in Amazon Web Services (AWS) Simple Storage Service (S3), an object storage offering. Amazon S3 buckets, which are similar to file folders, store objects, which consist of data and its descriptive metadata.

Mostly in the industry we are using secured(Private) and non-secured(Public) S3 buckets to store static data/files/objects according to the requirement. Also, we can use it to host some static web/HTML pages as well.

In this example I am going to save the employee detail object as a file into the S3 bucket, retrieve the details from the S3 bucket, and remove the file from the file id.

Refer to the official document for more details: https://aws.amazon.com/s3/

Technologies

Java 1.8
Maven 3.6.1
Springboot 2.2.7.RELEASE
log4j
aws-java-sdk-s3

Project Structure

Project Structure

AWS Configuration

Before accessing the S3 bucket through the application we have to build up the link between an application and the AWS provider. There are two ways to do that.

  1. Define AWS credentials in the project property file.
  2. Configure AWS CLI in the application running server.

First, one is very straightforward. But I'm going to use the second option.

As a first step, to configure the AWS CLI we have to install the AWS CLI console.

Then enter the command on your shell called ‘aws configure’. Then it will ask a few questions as follows and you have to provide valid details accordingly.

Example AWS Configuration

If you provided correct details, you ready to access AWS services remotely.

Refer to this https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html for more information.

STEP 1: POM.xml (Dependencies)

Please check the full POM file from here.

AWS dependencies

STEP 2: Controller level changes

In this class I have defined entry points for Add employee, Retrieve Employee by id and Remove Employee by endpoints. This will grab a request and passing to the service layer after validation.

Controller Changes

STEP 3: Service layer changes

Please visit here to get the full class file and see the comments. I have explained each step there.

Service Layer changes

STEP 4: AWS Configuration Class

This is the class where all methods included were related to the AWS S3 bucket accessing. I have defined this class as AWSConfig.

This class have following specific methods.

  • public void init() : Amazon S3 Client initialization.
  • private AmazonS3 getAmazonS3Client() : Return Amazon S3 Client object.
  • public void uploadFile(String objectKey, String content) : Upload Employee details as a file into the S3 bucket.
  • public String retrieveFileContentAsString(String objectKey) : Retrive Employee details from S3 bucket.
  • public void removeFileById(String objectKey) : Remove file by id.

The above methods are using aws-java-sdk-s3 dependency to acquire all the S3 accessing methods.

STEP 5: Testing

Once completed the implementation you can run this application using the IDE(Eclipse / STS) or else you can deploy this as a WAR file and deploying on the tomcat server.

Also, we can test this using 3rd party REST API client(POSTMAN) since this is a REST endpoint-based application.

I will show you the request and responses after executing these 3 endpoints.

‘Create Employee Details File’ Endpoint

Create Employee Endpoint

As you can see request body has Employee related details. Once executed the endpoint, it will return the object key id which means the file name that was uploaded into the S3 bucket with employee details.

How we can check if this is properly uploaded or not?? For that, we can use the S3 browser client. It is a very easy and user-friendly app. You can access this app after configuring AWS CLI to your server or PC.

This is how it is appearing after creating a new employee in the S3 Browser .S3 Bucket name is ‘testtrialogs’.

S3 browser

We can download this file and can open the file using any kind of text editor. The content looks like a JSON object. Please see the file content as given below.

S3 file content

‘Retrieve Employee Details File By Id/File Name’ Endpoint

‘Retrieve Employee Details File By Id/File Name’ Endpoint

‘Remove Employee Details File By Id/ File Name’ Endpoint

‘Remove Employee Details File By Id/ File Name’ Endpoint

Hope you guys enjoy the article !!!

Please visit a completed version from here and I have explained every possible step in the comment section. Please leave a comment if you have concerns or questions.

--

--

Chanaka MBK

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry. Skilled in JAVA,Spring Boot, Angular.