Amazon S3 – SpringBoot RestAPIs Upload/Download File/Image to S3

Amazon Simple Storage Service (Amazon S3) is object storage built to store and retrieve any amount of data from web or mobile. Amazon S3 is designed to scale computing easier for developers. In the tutorial, we build SpringBoot RestAPIs to upload/download files/images to Amazon S3.

Next post:
Amazon S3 – SpringBoot RestAPIs List All Files in S3 Bucket
Amazon S3 – Delete File SpringBoot RestAPI

Technologies

  • Java 8
  • Maven 3.6.1
  • pring Tool Suite: 3.9.4.RELEASE
  • Spring Boot: 2.0.4.RELEASE
  • Amazon S3

Spring Amazon S3

– For init an AmazonS3 client, we use:

– For upload S3 objects, we use:

– For download S3 objects, we use:

Practice

In the tutorial, we will setup an Amazon S3 bucket, an IAM user and create a SpringBoot RestAPIs to upload/download files/images to/from S3.

springboot-rest-api-upload-download-file-image-s3-aws + project-structure

Setup Amazon S3
Sign Up

Go to https://aws.amazon.com/s3/

springboot-rest-api-upload-download-file-image-s3-aws + sign-up

Choose Get started with Amazon S3.

Follow the instructions on the screen for sign up.
-> AWS will notify you by email when your account is active and available for you to use.

Create S3 Bucket

Now, your account is ready to use with AWS.
-> Sign in to AWS: https://console.aws.amazon.com/s3

springboot-rest-api-upload-download-file-image-s3-aws + create-bucket

Choose Create bucket, input information for creating bucket:

springboot-rest-api-upload-download-file-image-s3-aws + create bucket

Press Create -> result:

springboot-rest-api-upload-download-file-image-s3-aws + create bucket succeed

Create IAM user

Go to https://console.aws.amazon.com/iam/
In the navigation pane, choose Users and then choose Add user.

springboot-rest-api-upload-download-file-image-s3-aws + s3-add-user-step-0

Input User name, choose Programmatic access for Access type:

springboot-rest-api-upload-download-file-image-s3-aws + s3-add-user-step-1

Press Next: Permissions button -> go to Set permissions for grokonez-user screen.
Now, choose Attach existing policies directly -> filter policy type s3, then check AmazonS3FullAccess:

springboot-rest-api-upload-download-file-image-s3-aws + s3-add-user-step-2

Press Next: Review:

springboot-rest-api-upload-download-file-image-s3-aws + s3-add-user-step-3

Press Create user:

springboot-rest-api-upload-download-file-image-s3-aws + s3-add-user-step-4

Press Download.csv for {Access key ID, Secret access key}.

SpringBoot RestAPIs
Create SpringBoot project

Using SpringToolSuite to create a SpringBoot project. Then add needed dependency aws-java-sdk:

Configure Amazon Client

Create a S3Config.java file for setup AmazonS3 client:

Open ‘application.properties’, add aws configuration:

Note: Keep private for secret_access_key.

Implement S3 Upload/Download Service

Create S3Services.java interface:

Implement S3Services.java interface with S3ServicesImpl.java:

Implement Upload/Download RestAPIs

– Expose Upload RestAPI in controller UploadFileController.java:

– Expose Download RestAPI in controller DownloadFileController.java:

Run & Check results

– Build and Run the SpringBoot project with commandlines {mvn clean install, mvn spring-boot:run}.

-> Upload File:

springboot-rest-api-upload-download-file-image-s3-aws + upload-files

springboot-rest-api-upload-download-file-image-s3-aws + upload-file-on-s3-bucket

-> Download File:

springboot-rest-api-upload-download-file-image-s3-aws + download-files

SourceCode

– In application.properties file, change gkz.aws.access_key_id & gkz.aws.secret_access_key to yours

– Sourcecode ->

SpringRestAPIsS3Amazon

By grokonez | August 4, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*