Working with Amazon S3 presigned URLs in Spring Boot
Tags: java, spring, aws, s3
Published: 2021-12-01 05:05:03

Hello Everyone! Welcome to another DEV Community post. This is going to be a continuation of my previous DEV Community post [Integrating your Spring Boot project with Amazon S3](https://dev.to/aws-builders/integrating-your-spring-boot-project-with-amazon-s3-3e3). If you have directly visited here, you might have to go back and copy some code snippets from previous story for a fully working solution. If you’re ready, let’s get started without waiting further.

As per the implementation in the last DEV Community post whenever a user needs to upload a file, the file should be first uploaded to the server and from the server it will be uploaded to Amazon S3. Similarly when a user requests for a file it will be downloaded to the server first and then will be sent to the user. Whoever reads the above sentence will clearly understand that the implementation uploads a file twice and downloads it twice. Hence this will be a wastage of server resources and will double time taken to process a file. Luckily presigned URLs are there to save us!

___

With presigned URLs first the user need to send the metadata about the file to server in order to create a key and to generate a presigned URL. Server will send the generated presigned URL back to user and user has to upload the file directly to Amazon S3 using the presigned URL along with request parameters sent by the server. Similarly when retrieving a file user has to send the filename (aka key) to the server and server will generate a presigned URL to download the requested resource. Once the presigned URL is returned, user has to make a request again to download the file directly from Amazon S3.


![Photo by https://www.webiny.com/blog/upload-files-to-aws-s3-using-pre-signed-post-data-and-a-lambda-function-7a9fb06d56c1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jh9h03npvqct7fi1yfp2.png)

First open the FileService.java class from the previous story. We’ll be removing convertMultiPartFileToFile method which is no more needed and making changes to findByName, save methods. Make the changes by referring to below Gist.

{% gist https://gist.github.com/mmafrar/26f545833021ef01b0ce9709a8cc2331 %}

Once you’re done making changes to the service class, open the FileController.java class to make some additional changes. Refer the code snippet shared below and make changes accordingly.

{% gist https://gist.github.com/mmafrar/c4dcf6630196a7df3822672e3655cb41 %}

Once you’re done with the changes we have simple Spring Boot application which will interact with Amazon S3 to generate presigned URLs for file upload and download. Please note that above code is not production ready and should undergo several changes before being deployed.


![Requesting a presigned URL to upload a file
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/728mmfaoj3sku2azh9qc.png)

As shown above make a request to server via Postman or any other client with requested metadata to generated a presigned URL to upload a file. Once the server returns a presigned URL, make another call via the client to upload the file as below.


![Uploading a file to Amazon S3 via presigned URL provided](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3fex3iaoeefewyvg49e.png)

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

{% link mmafrar/how-to-add-an-icon-for-an-ios-app-312c %}

Cover Image: Photo by [Maksym Kaharlytskyi](https://unsplash.com/@qwitka) on [Unsplash](https://unsplash.com)