How to Upload Multiple Files to Amazon S3 Buckets in Spring Boot

In this example, we will show you how to upload multiple files to an Amazon S3 bucket using a REST API in Spring Boot.

Follow the steps below to complete this example:

Adding Dependency

To upload files to S3, you will need to add the AWS Java SDK For Amazon S3 dependency to your application. Here is the Maven repository for Amazon S3 SDK for Java.

Gradle Dependency

Add the following dependency to the build.gradle file:

implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.12.158'
Maven Dependency

Add the following dependency to the pom.xml file:

Add Configurations

First, add the following credentials to your resources/ configuration file:


aws.access-key = your aws access key here
aws.access-secret-key = your aws secret key here
aws.region = us-east-1

Create Configuration Class

Create a configuration Java class for the AmazonS3 Client:

package com.s3.sample.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;

public class AwsConfig {

	private String awsAccessKey;
	private String awsAccessSecretKey;
	private String awsRegion;

	public AwsConfig(@Value(value = "${aws.access-key}") String awsAccessKey,
			@Value(value = "${aws.access-secret-key}") String awsAccessSecretKey,
			@Value(value = "${aws.region}") String awsRegion) {
		this.awsAccessKey = awsAccessKey;
		this.awsAccessSecretKey = awsAccessSecretKey;
		this.awsRegion = awsRegion;

	public AWSStaticCredentialsProvider getAwsCredentialsProvider() {
		BasicAWSCredentials awsCred = new BasicAWSCredentials(this.awsAccessKey, this.awsAccessSecretKey);
		return new AWSStaticCredentialsProvider(awsCred);

	public AmazonS3 getAmazonS3Client() {
		return AmazonS3ClientBuilder.standard().withRegion(this.awsRegion).withCredentials(getAwsCredentialsProvider())

Create Service

Create a service class with a method to upload multiple files to an Amazon S3 bucket:


package com.s3.sample.demo.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

public class S3UploadMultipleFilesServiceExample {

	private String s3BaseUrl = "";

	private String bucketName = "my-test-bucket";

	private String folderName = "/myfolder/images/";

	private AmazonS3 s3Client;

	public Map<String, String> uploadMultipleFileToS3(List<MultipartFile> multipartfiles) {
		Map<String, String> response = new HashMap<>();
		if (!multipartfiles.isEmpty()) {
			multipartfiles.forEach(multipartfile -> {
				String filePathName = multipartfile.getOriginalFilename();
				File file = new File(filePathName);

				try (FileOutputStream fos = new FileOutputStream(file)) {

					if (!file.exists()) {


					/* uploading file to S3 */
					s3Client.putObject(new PutObjectRequest(bucketName, folderName + "/" + file.getName(), file)

					/* Url location of the uploaded file in S3. You should save it in database */
					String s3FileAccessUrl = s3BaseUrl.concat(bucketName).concat(folderName).concat(file.getName())
							.replaceAll("\\s", "+");

					response.put("fileUrl", s3FileAccessUrl);


				} catch (FileNotFoundException e) {
				} catch (IOException e) {
		return response;


Create Web Controller

Create a controller class with a REST API endpoint to upload multiple files to S3:


package com.s3.sample.demo.controller;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.s3.sample.demo.service.S3UploadMultipleFilesServiceExample;

@RequestMapping(value = "/api/files")
public class UploadMultipleFilesExampleController {

	private S3UploadMultipleFilesServiceExample s3UploadMultipleFilesServiceExample;

	@PostMapping(value = "/multiple-upload")
	public ResponseEntity<Map<String, String>> uploadMultipleFiles(
			@RequestPart(name = "multipartfiles", required = true) List<MultipartFile> multipartfiles) {

		return ResponseEntity.ok(s3UploadMultipleFilesServiceExample.uploadMultipleFileToS3(multipartfiles));

The code is complete. You can run and test your application.