Skip to main content

AWS S3 integration with Spring Boot (AWS S3 + Spring Boot)

Updated for: Spring Boot 3

In this article we will demonstrate - how to "Download & Upload files to/from AWS S3 in a Spring Boot" project.

Create a AWS S3 bucket

Let's first create a AWS S3 bucket, we will use this bucket to upload/download files from a Spring Boot project.

1) Go to AWS Console and search for S3 service as shown in the picture below:

2) Now you should land on "Create Bucket" page as shown in below picture, click on "Create Bucket" button in top-right corner:

3) Provide appropriate "Bucket Name" and select "AWS Region" from the dropdown, as shown in the picture below:

4) That's it, the bucket is now created and we can use it to download/upload files:

5) In order to access this bucket from "Spring Boot" application, we need to generate "security credentials".

Go to "My security credential" page by clicking on top-right dropdown and selecting "Security credentials".

Now generate "Access Key ID" and "Secret Access Key" by clicking on "Create access key" button.

We need these credentials (Access Key ID, Secret Access Key) to access this "AWS S3" bucket from "Spring Boot" application.

Create Spring BOOT Application

Here we are donw with creating a "S3 bucket", let's now create a Spring BOOT Application to "Download & Upload files to/from AWS S3".

1) Dependencies

The final "pom.xml" should look something like this:


2) Configuration

In the configuration below, we are adding aws credentials, MySql configuration, JPA related configurations and logging stuff.

3) AWSCredentials

This is a simple spring boot "@Configuration" file, we are using this to generate a "AWSCredentials" object, we will communicate with S3 Bucket using this object only.

4) Model

This "FileMeta" is used to represent and store file-metadata in mysql, we can use this info to list/download files back from S3.

5) Repository

This is where "Spring Data JPA" comes into picture, we are creating an "FileMetaRepository" implementing "CrudRepository", this will save us from writing any boilerplate code for JDBC stuff.

6) Service

We have defined two interface-backed services, "AmazonS3ServiceImpl" is used to communicate with S3 to get/upload data and "MetadataServiceImpl" is kind of an abstraction in between the application logic and S3 related stuff.

7) Controller

Finally, this is the place where we are defining URLs for this application; here "thymeleaf" is used as a server-side Java template engine for web/UI layer.

8) Thymeleaf UI

This is a simple UI interface, we have added a form to upload files and a table to list/download already uploaded files.

9) Spring Boot

This is plain old Spring Boot driver class; all the execution starts from here.

10) Testing

Now we are all done with our project setup, run the application and navigate to http://localhost:8080/dashboard to access download/upload files dashboard.

Source code: GitHub


Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

How to create a basic Spring 6 project using Maven

Below is a step-by-step guide to creating a basic Spring project using Maven. 1) Create a Maven Project Use the following Maven command to create a new Maven project. mvn archetype:generate -DgroupId=com.tb -DartifactId=spring-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 2) Import in IntelliJ IDEA If you haven't already, open IntelliJ IDEA on your system. Go to "File" > "New" > "Project from Existing Sources..." . In the file dialog, navigate to the directory where your Maven project is located. Select the pom.xml file within the project directory and click "Open." 3) Update pom.xml In total, the application requires the below-mentioned dependencies: 4) Create Spring Configuration Create a Java configuration class that uses annotations to define your Spring beans and their dependencies. This class should be annotated with @Configuration . 5) Create the Main Application C...