Integrating your Spring Boot project with Amazon S3
Tags: java, spring, aws, s3
Published: 2021-09-01 13:20:20

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.

_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._

{% medium https://mmafrar.medium.com/setting-up-your-first-spring-boot-app-e3a42362677f %}

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/191z30wcuobk9wzrxknb.png)

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.

{% gist https://gist.github.com/mmafrar/10a22cc93629949d41c9bf9cbde1ddaa %}

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

{% gist https://gist.github.com/mmafrar/019c0396bc04d61e0e4515b15cea7339 %}

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

{% gist
https://gist.github.com/mmafrar/889733cb2c767fde44814aaf1b98a942 %}

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](https://mmafrar.medium.com/configuring-multiple-data-sources-with-spring-boot-2-and-spring-data-jpa-8e236844e80f).

{% post mmafrar/setting-up-your-first-spring-boot-app-8mk %}