Upload files to Amazon S3 using Java Spring Boot

Tram Ho


In this article we will learn how to upload a file to Amazon S3 Bucket using Spring boot. As you know most mobile or web applications have features for uploading files such as video images, etc. So as a programmer you should choose the best way to store and loosen it. Store these files. There are different ways to deal with:

  • Save directly on the server where the application was deployed
  • Stored in database with binary file type
  • Used in cloud storages (online storage places)

In fact, most developers use a third approach using S3 Bucket Service of Amazon . And here is the reason why it is so popular:

  • Easy to write code to upload any file
  • Amazon supports most programming languages
  • You can view all uploaded files directly using the web interface
  • You can manipulate manually such as upload / delete files using the web interface

Step 1 – Create Amazon S3 bucket.

To start using S3 Bucket you must create an account on the Amazon website .

Once you have an account, the next step is to create the S3 Bucket where the uploaded files are stored. On the menu bar select Services -> S3

Then press the Create bucket button

Enter a bucket name (The bucket name is unique) and select the area nearest you. Click the Create button

Note: Amazon will give you 5GB for free each year for you to store. If you use more than 5GB per year you will have to pay

Your buket has now been created, but you will need to provide user access to this bucket. It is not secure when you give your root user access key to others so you need to create a new IAM use this user who only has permission to use S3 Bucket

Now create the user. Select Services -> IAM . On the left navigation pane select User then select Add user .

Enter the user name and check the box for Accesss type ‘Programmatic access’ . Click the next button.

You must add permissions for the user just created. Click ‘ Attach existing policy directly ‘, in the search box enter ‘s3’ in all searched results select AmazonS3FullAccess

Then click next and Create User you will see the Access key ID and Secret access key will be used in the next section. Now the configuration of S3 Bucket is complete. Let’s go to the next section to write the code to upload the file to the newly created buket s3

Step 2 – Write the Code upload file to S3 Buket.

Create Spring Boot project and add amazon dependency to pom.xml file

Then add the s3 bucket properties you just created above to the application.yml file

Create class AmazonClient

AmazonS3 is a class from the amazon library. All other fields are AWS S3 bucket information declared in application.yml. Anotation @ Value will read the values ​​from application.yml file

initializeAmazon () is a method to set amazon login information for our application. Anotation @ PostConstruct to run this method after the constructor will be called because the fields marked with @ Value have null values ​​in the constructor.

The method of uploading files to the S3 bucket requires a File type parameter, but our data transfer will be of the MultipartFile type. We will need to add a method to convert MultipartFile -> file

Also you can upload a file multiple times so we create a unique name for each upload. Use timestamp to mark and replace filename spaces with underscores to avoid future problems.

Next add the uploadFileTos3bucket method that uploads the file to the S3 bucket

In this method we have added PublicRead permission. It means that anyone who has the path of the file can access the file. This attribute is usually only applied to image files

Finally, we’ll combine the methods just created above to create a service that will be called from the controller. This method will save the file to the s3 bucket and return the path of the newly uploaded file that you can store in the database

Next we create BucketController.class

The uploadFile method takes a MultipartFile parameter. Now that we have finished the code, let’s go to the test. Create a request using Postman, select the POST method in BODY, select the form-data, enter a key file and select the value of the File value type and select any file from your PC. friend

You visit your s3 bucket and you will see the uploaded image

The article comes here is the end. Hope this article was helpful for you. If you have any questions please leave a comment below this article.

Thank you for reading this article References at https://medium.com/oril/uploading-files-to-aws-s3-bucket-using-spring-boot-483fcb6f8646

Share the news now

Source : Viblo