Categories: Uncategorised

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

mahendravarman.m@gmail.com

Recent Posts

Spring Webflux Functional Endpoint – File Upload

In this blog using the Spring WebFlux module, we are going to leverage the functional…

1 year ago

Serverless Functions with Spring Cloud Function, AWS Lambda

Spring Cloud Function is a project within the Spring ecosystem that allows developers to build…

1 year ago

Spring Boot + RabbitMQ – Decoupling Microservices Communication

RabbitMQ is an open-source message broker software that implements the Advanced Message Queuing Protocol (AMQP).…

1 year ago

Spring Integration – Sending files over SFTP

Spring Integration is a powerful extension of the Spring Framework designed to support the implementation…

1 year ago

Spring Cloud Config Client

The Spring Cloud Config Client is a component of the Spring Cloud framework that enables…

1 year ago

Handling CSV in Python

In Python, handling CSV (Comma Separated Values) files is easy using the built-in csv module.…

1 year ago