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.

<dependency>
    <groupId>io.awspring.cloud</groupId>
    <artifactId>spring-cloud-aws-starter-s3</artifactId>
</dependency>

Spring Cloud AWS BOM for the starter:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.awspring.cloud</groupId>
            <artifactId>spring-cloud-aws-dependencies</artifactId>
            <version>{project-version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

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:

@RestController
@Slf4j
public class S3FileUploadController {
	@Autowired
	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:

@Service
@Slf4j
public class UploadServiceImpl implements UploadService {

	@Autowired
	S3Template s3Template;
	
	@Value("${BUCKET_NAME}")
	String bucketName;

	@Override
	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
				e.printStackTrace();
				return Mono.error(e);
			}

		});

	}

}

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

References:

https://awspring.io/

https://docs.awspring.io/spring-cloud-aws/docs/3.0.0-SNAPSHOT/reference/html/index.html

Leave a Reply

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