Spring Cloud AWS – File upload to S3

Spring cloud AWS simplifies using AWS managed services in a spring and spring boot applications. S3 is used to store files in a cloud. Spring Cloud AWS provides a spring boot starter to auto-configure the various S3 integration related components.


Spring Cloud AWS BOM for the starter:


Used S3Template to upload file to S3. Spring Cloud AWS provides S3Template which has methods for most S3 use cases.

Define following Spring Cloud AWS related properties:

spring.cloud.aws.s3.enabled = true
spring.cloud.aws.s3.endpoint = ${S3_ENDPOINT}
spring.cloud.aws.credentials.access-key = ${ACCESS_KEY}
spring.cloud.aws.credentials.secret-key = ${SECRET_KEY}
spring.cloud.aws.s3.region = ${AWS_REGION}
spring.cloud.aws.region.static = ${AWS_REGION}
s3.bucket.name = ${BUCKET_NAME}

Rest Controller for the file upload:

public class S3FileUploadController {
	UploadService service;
	@PostMapping(path="/upload", consumes="multipart/form-data")
	public Flux<Object> fileUpload(@RequestPart(name="file",required=true) FilePart file){
		log.info("inside controller");
		return service.csvFileUpload(file);

Fileupload to S3:

public class UploadServiceImpl implements UploadService {

	S3Template s3Template;
	String bucketName;

	public Flux<Object> csvFileUpload(FilePart filePart) {
		log.info("inside csvFileUpload" + filePart.filename());

		return filePart.content().map(dataBuffer -> {

			InputStream is = dataBuffer.asInputStream();
			S3Resource resource = s3Template.upload(bucketName, "employees.csv", is);
			try {	
				return resource.getURL();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				return Mono.error(e);




Source Code: https://github.com/MMahendravarman/Springboot_Examples




Leave a Reply

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