What is meant by Spring Cloud ?

Spring Cloud is the open-source library–makes it easy to develop JVM applications for the cloud. With it, applications can connect to services and discover information about the cloud environment easily in multiple clouds such as Cloud Foundry and Heroku.

Table of Content :

Features of Spring Cloud

Spring Cloud focuses on providing good out of box experience for typical use cases and extensibility mechanism to cover others.

  • Distributed/versioned configuration
  • Service registration and discovery
  • Routing
  • Service-to-service calls
  • Load balancing
  • Circuit Breakers
  • Global locks
  • Leadership election and cluster state
  • Distributed messaging

The Spring Framework provides a org.springframework.core.io.ResourceLoader abstraction to load files from the filesystem, servlet context and the classpath.

Spring Cloud AWS adds support for the Amazon S3 service to load and write resources with the resource loader and the s3 protocol.

Upload and Download file to S3 using Spring Cloud

Maven Dependencies

We need maven dependency

<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.11.133</version> </dependency> OR <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-aws</artifactId> </dependency>



We need to configure s3 config properties in application.yml file as

cloud.aws.stack.auto=false cloud.aws.region.static=ap-south-1 app.s3.bucketname=testbucket

We have to create s3Client bean using the above credentials so that the Spring resource loader internally uses it to upload/download files to the s3 bucket.

Spring Framework 3.1 onwards the resource loader can also be used to upload files with the org.springframework.core.io.WritableResource interface which is a specialization of the org.springframework.core.io.ResourceLoader interface. Clients can upload files using the WritableResource interface. The next example demonstrates an upload of a resource using the resource loader.


Configure S3 Client

We need s3 client bean as given below

@Configuration public class S3Configuration { @Value("${cloud.aws.region.static}") private String region; @Bean("s3Client") public static AmazonS3 amazonS3Client() { return AmazonS3ClientBuilder.standard() .withCredentials(new DefaultAWSCredentialsProviderChain()) .withRegion(region).build(); } }

Here we used DefaultAWSCredentialsProviderChain to load aws credentials.To know more about this, please go through article Load aws credentials for java sdk.



Uploading file to s3 bucket - Spring Cloud AWS

For uploading files to the s3 bucket, we have to provide an object key URL and File to be uploaded into the s3 bucket. We can upload the file stream too.

The s3 object URL should be in the format

s3://testbucket/pending/customer001.csv is s3 protocol.

Here is the method to do that

@Autowired private ResourceLoader resourceLoader; @Value("${app.s3.bucketname}") private String bucketName; public void uploadFile(File file, String key) throws IOException { String s3ObjectKey= "s3://"+bucketName+"/pending/"+key; WritableResource resource = (WritableResource) resourceLoader .getResource(s3Url); try (OutputStream outputStream = resource.getOutputStream()) { Files.copy(file.toPath(), outputStream); } }

public void uploadBytes() throws IOException { Resource resource = this.resourceLoader.getResource("s3://testbucket/pending/customer001.csv"); WritableResource writableResource = (WritableResource) resource; try (OutputStream outputStream = writableResource.getOutputStream()) { outputStream.write("test".getBytes()); } }



Download file from s3 bucket - Spring Cloud AWS

Downloading files can be done by using the s3 protocol to reference Amazon S3 buckets and objects inside their bucket. The typical pattern is s3://<bucket>/<object> where bucket is the global and unique bucket name and object is a valid object name inside the bucket.

The object name can be a file in the root folder of a bucket or a nested file within a directory inside a bucket.

@Autowired ResourceLoader resourceLoader; public void downloadFileFromS3Bucket(String s3Url) throws IOException { Resource resource = resourceLoader.getResource(s3Url); File downloadedS3Object = new File(resource.getFilename()); try (InputStream inputStream = resource.getInputStream()) { Files.copy(inputStream, downloadedS3Object.toPath(), StandardCopyOption.REPLACE_EXISTING); } }



In this article, we have seen Spring Cloud AWS – S3 upload/download Examples. All source code in the article can be found in the GitHub repository.