Integrating your Spring Boot project with Amazon S3

Afrar Malakooth
3 min readJan 15, 2021

--

How I’m I going to store the files (aka objects) in my project? Well, there’s no right answer. Either the file could be stored in the database itself as a BLOB or file can be copied to a folder inside the server and reference can be stored in the database. But with the rise of cloud computing services like object storage have taken a lead which can be used to achieve the same goal in a better way.

Photo by Amazon Web Services

Welcome to another Spring Boot tutorial. For this tutorial I’ll be assuming that you have a basic understanding of Spring Boot and AWS. If you’re totally new to Spring Boot check out my starter tutorial Setting up your first Spring Boot app on Medium. Amazon S3 being one of the widely used object storage service, let’s see how we can integrate it with our Spring Boot project.

When your web or mobile application grows you’ll need to work with images, documents etc. apart from just plain text. And you need resilient and highly available mechanism to store and retrieve those files. That’s where Amazon S3 comes into play. In order to get started with this tutorial, we’ll head onto AWS Management Console and create an IAM user and a S3 bucket initially.

Review window for adding a user

Login as root user or IAM user and navigate to IAM dashboard. On the left pane click on Users and select Add user. I have given my-test-user as the username and chosen only programmatic access. Press next and select attach existing policies directly. Search for AmazonS3FullAccess and attach it to the user. At the time of writing this article you’ll be presented with a review window as shown in the above. Proceed with creating the user and copy the access key id and secret.

Then navigate to S3 dashboard and click on create bucket. I have given my-test-s3-bucket-123456 as the bucket name and us-east-1 as the region. For this tutorial I think you can leave the rest of the settings with default values and click on create bucket. We’re done with AWS, let’s switch to Spring Boot.

# File storage configuration
access.key.id=<your_aws_access_key_id>
access.key.secret=<your_aws_access_key_secret>
s3.region.name
=us-east-1
s3.bucket.name
=my-test-s3-bucket-123456

Head on to your Spring Boot project and add the above properties to your application.properties file. Then add the below dependencies to your build.gradle file.

implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.837')
implementation 'com.amazonaws:aws-java-sdk-s3'

Then we need to add a configuration class to setup Amazon S3 client. Refer the below Gist and create your file accordingly.

After you’re done with the configuration class, we need to create a service class where the logic to interact with Amazon S3 resides.

Once the service class is ready we need to have a controller class which contains the APIs to be exposed to the frontend.

We’re all set now, Happy Coding! Below is a DEV Community video I have recently published and if you’re interested check out my previous Medium story on Configuring multiple data sources with Spring Boot 2 and Spring Data JPA.

--

--

Afrar Malakooth
Afrar Malakooth

Written by Afrar Malakooth

An energetic Software Engineer with 5+ years experience of writing code for leading enterprises. Passionate about social work, volunteering and travelling.

No responses yet