File/Photo Uploading Web Application using Spring Boot and AWS S3

Pavani Thrimavithana
5 min readJul 12, 2018

--

Amazon Simple Storage Service(S3) has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web.

Amazon S3 is intentionally built with a minimal feature set that focuses on simplicity and robustness. It include some advantages like create buckets, store data in buckets, download data and permissions.

In this article I’m going to explain how to upload file/photos to AWS S3 bucket using a Spring Boot application.

To get started you need to create an account if you don’t have a AWS account. After you create an account. let’s create a S3 bucket.

AWS Free Tier (12 Month Introductory Period),offers are only available to new AWS customers, and are available for 12 months following your AWS sign-up date. When your 12 month free usage term expires or if your application use exceeds the tiers, you simply pay standard, pay-as-you-go service rates . Refer AWS Free Tier Details for more details

After you login to the account you will be directed to the Console home.

Access AWS S3 service
Create a Bucket

After you choose the “Create bucket” button you will have to enter the details of your bucket. In the Bucket name field, type a name for your new bucket.The name must be unique across all existing bucket names in Amazon S3. After you create the bucket you cannot change the name, so choose wisely. Choose a bucket name that reflects the objects in the bucket because the bucket name is visible in the URL that points to the objects that you’re going to put in your bucket. For Region , choose a region where you want the bucket to reside.Then choose the “Create” button and create your bucket.

Now we need to provide access permissions for the user to use the bucket. We can give access permission using AWS IAM user.

AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use resources.

Access IAM

Choose IAM under Security, Identity & Compliance you will be directed to the IAM dashboard. Then click on the “Add User” button to set the user details. For the Access type check the “ Programmatic access”. Click on the next button.

Set Permissions

Then we need to add permissions to this user. Click “Attach existing policy directly”, in the search field enter S3 and among found permissions choose AmazonS3FullAccess. Click on the next button and review the details. After that “Create User”. Then you would be able to see the Access key ID and Secret ket ID for the created user. Make sure to download the keys by clicking on the “Download.csv” button.

Now lets create the spring boot application. You can generate a Spring Boot web application using Spring initializr . Create the Spring Boot application as the below file structure.

Project Structure
  • FileController is a Controller which has request mapping methods to display our page.
  • webapp folder contains all necessary JSP file and static resource.

In the pom.xml file of your application include the maven dependency for AWS Java Client is as follows.

<!--Amazon-->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.133</version>
</dependency>

Remember that we will use the Packaging as war. Also add the following dependencies for pom.xml jsp file and static resource.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

For the application.yml include the amazon S3 properties as follows.

amazonProperties:
endpointUrl:
https://s3.us-east-2.amazonaws.com
accessKey: XXXXXXXXXXXXXXXXX
secretKey: XXXXXXXXXXXXXXXXXXXXXXXXXX
bucketName: your-s3-bucket-name
region: your-bucket-region

Now lets create the Rest Controller for the application and the AmazonClient class.

AmazonS3 is a class from amazon dependency. The Value annotation will bind application properties directly to class fields during application initialization.

Then extends @SpringBootAplication class with SpringBootServletInitializer and add the code below:

public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}

Now lets create the UI for the application. Create a index.jsp under webapp->WEB-INF->jsps. A single UI to upload the file/photos to S3 bucket.

Now run the Spring Boot application and upload file/photo to the S3 bucket.

UI

The full implementation of this project can assessed in my GitHub account.

If you enjoyed this story, please click the 👏 button and share to help others find it! Feel free to leave a comment below.

--

--

Pavani Thrimavithana

Associate Lead - Software Quality Engineering @VHIL | ISTQB Certified Tester | Postgraduate student @ UCSC | SE Graduate @ SLIIT | Artisan