Spring Cloud AWS – S3

1. Simple S3 Download

Let’s start by easily accessing files stored on S3:

@Autowired
ResourceLoader resourceLoader;

public void downloadS3Object(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);
    }
}

2. Simple S3 Upload

We can also upload files:

public void uploadFileToS3(File file, String s3Url) throws IOException {
    WritableResource resource = (WritableResource) resourceLoader
      .getResource(s3Url);
 
    try (OutputStream outputStream = resource.getOutputStream()) {
        Files.copy(file.toPath(), outputStream);
    }
}

3. S3 URL Structure

The s3Url is represented using the format:

s3://<bucket>/<object>

For example, if a file bar.zip is in the folder foo on a my-s3-bucket bucket, then the URL will be:

s3://my-s3-bucket/foo/bar.zip

And, we can also download multiple objects at once using ResourcePatternResolver and the Ant-style pattern matching:

private ResourcePatternResolver resourcePatternResolver;
 
@Autowired
public void setupResolver(ApplicationContext applicationContext, AmazonS3 amazonS3) {
    this.resourcePatternResolver = 
      new PathMatchingSimpleStorageResourcePatternResolver(amazonS3, applicationContext);
 }

public void downloadMultipleS3Objects(String s3Url) throws IOException {
    Resource[] allFileMatchingPatten = this.resourcePatternResolver
      .getResources(s3Url);
        // ...
    }
}

URLs can contain wildcards instead of exact names.

For example the s3://my-s3-bucket/**/a*.txt URL will recursively look for all text files whose name starts with ‘a‘ in any folder of the my-s3-bucket.

Note that the beans ResourceLoader and ResourcePatternResolver are created at application startup using Spring Boot’s auto-configuration feature.

4. Conclusion

And we’re done – this is a quick and to-the-point introduction to accessing S3 with Spring Cloud AWS.

In the next article of the series, we’ll explore the EC2 support of the framework.

As usual, the examples are available over on GitHub.

Related posts:

Immutable Objects in Java
Introduction to Spring Data JPA
Guide to the Java Queue Interface
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Configuring a DataSource Programmatically in Spring Boot
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Deque và ArrayDeque trong Java
Java Program to Implement Hopcroft Algorithm
What is Thread-Safety and How to Achieve it?
Java Program to Solve any Linear Equations
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Using JWT with Spring Security OAuth
Java Program to Generate Random Hexadecimal Byte
Instance Profile Credentials using Spring Cloud
Custom JUnit 4 Test Runners
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Mapping a Dynamic JSON Object with Jackson
Java Program to Implement Floyd Cycle Algorithm
Simple Single Sign-On with Spring Security OAuth2
Cơ chế Upcasting và Downcasting trong java
Mảng (Array) trong Java
Java Program to Implement Suffix Array
Quick Guide to Spring Controllers
Hướng dẫn sử dụng String Format trong Java
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Adding Shutdown Hooks for JVM Applications
Apache Commons Collections OrderedMap
Java Program to Implement the String Search Algorithm for Short Text Sizes
A Guide to Iterator in Java
Spring Boot - Tomcat Deployment