In this post, you will see code samples for how to upload a file to AWS S3 bucket from a Java Spring Boot app. The code you will see here is from one of my open-source repositories on Github, called document-sharing.


Let’s say you are building a document sharing app where you allow your users to upload the file to a public cloud solution. Now, let’s say you are building the API for your app with Spring Boot and you are using AWS S3 as your public cloud solution. How would you do that? This blog post contains the code that can help you achieve that.


First make sure you have added the following dependency to the AWS SDK in your repo,


Then you can start by adding an endpoint in the controller of your app

    public ResponseEntity<ServiceResponse> handleFileUpload(@RequestParam("file") MultipartFile file,
                                                            @RequestParam("userId") String userId)
            throws IOException {
        InputStream is = file.getInputStream();
        String filename = file.getOriginalFilename();"Request to upload file = " + filename);
        ServiceResponse response = storageService.uploadFile(file, userId);
        return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus()));

As you can see, your controller calls the uploadFile method from the StorageService, the code for which is below

     * 1. Get the filename along with userid
     * 2. Save all that info in the Document class
     * 3. Save the Document in the DocumentRepositoryImpl
     * 4. Move on to uploading the actual file to S3 via S3Repository
    public ServiceResponse uploadFile(MultipartFile file, String userId) throws IOException {

        DocumentMetadataUploadRequest request = new DocumentMetadataUploadRequest();
        request.setUserId(userId);"Set the documentMetaUploadRequest obje3ct");
        // ignore the part of documentService.saveDocumentMetadata for now
        ServiceResponse metadataUploadResp = documentService.saveDocumentMetadata(request);
        if (metadataUploadResp.getStatus() > 299) {
            return ServiceResponse.builder()
                    .message("Something went wrong, please try again later")
        String filename = userId + "/" + file.getOriginalFilename();;
        return s3Repository.putS3Object(convertMultipartFile(file), filename);

Source code for the DocumentMetadataUploadRequest is below,

package com.mydaytodo.sfa.asset.model;

import com.amazonaws.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

public class DocumentMetadataUploadRequest {

    private String name;
    private String path;
    private String assetType;
    private String id;
    private String userId;

     * @param uploadRequest
     * @return
    public static Document convertRequest(DocumentMetadataUploadRequest uploadRequest) {
        return Document.builder()
                .id(StringUtils.isNullOrEmpty(uploadRequest.getId())? null: uploadRequest.getId())

Upload to AWS S3 bucket from Java Spring Boot

As you can see, this in-turn calls the putS3Object from the S3Repository class, the code for which is below,

    public ServiceResponse putS3Object(File file,
                                       String assetKey) throws IOException {"About to upload a file to S3");

        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(awsConfig.getS3UploadBucketName()
                    , assetKey, file);

            PutObjectResult result = awsConfig.s3Client().putObject(putObjectRequest);
  "Finished uploading..........");
            return ServiceResponse.builder()
        } catch (Exception e) {
            return ServiceResponse.builder()

In case you are curious about the ServiceResponse class, it looks something like this

The idea of the ServiceResponse class is to provide a template for how the API responds to requests,

package com.mydaytodo.sfa.asset.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

public class ServiceResponse {
    private Integer status;
    private Object data;
    private String message = "";


Hope you found this blogpost useful, and you can find the full source code for this entire app which includes the code for the dependencies can be found here on Github.

If you have any questions on this, feel free to leave a comment on this post or send me an email directly.

If you find any of my posts useful and want to support me, you can buy me a coffee 🙂

While you are here, maybe try one of my apps for the iPhone.

Products – My Day To-Do (

Here are some of my other bloposts on Java

How to build a full stack Spring boot API with ReactJS frontend – My Day To-Do (

How to call REST API with WebClient – My Day To-Do (

How to build a jokes client in Java Spring Boot with RestTemplate – My Day To-Do (

Have a read of some of my other posts on AWS

Deploy NodeJS, Typescript app on AWS Elastic beanstalk – (

How to deploy spring boot app to AWS & serve via https – My Day To-Do (

Categories: AWSJava


Leave a Reply

Avatar placeholder
Verified by MonsterInsights