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:

Java Program to Implement Adjacency Matrix
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to Implement Hash Tables Chaining with List Heads
Disable DNS caching
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
A Custom Data Binder in Spring MVC
Converting a Stack Trace to a String in Java
Map Serialization and Deserialization with Jackson
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Implement Sieve Of Sundaram
Java Program to Implement Uniform-Cost Search
Hướng dẫn Java Design Pattern – Iterator
Convert Hex to ASCII in Java
Java Program to Represent Graph Using Linked List
Returning Custom Status Codes from Spring Controllers
Guide to Spring @Autowired
Apache Commons Collections BidiMap
Java Program to Implement Sorted Doubly Linked List
Java Program to Implement CopyOnWriteArraySet API
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Returning Image/Media Data with Spring MVC
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Implement Circular Doubly Linked List
Java Program to Implement AA Tree
Java Program to Implement the Vigenere Cypher
Receive email by java client
Guide to Apache Commons CircularFifoQueue
A Quick Guide to Spring MVC Matrix Variables
Check If a File or Directory Exists in Java
Spring Data JPA @Modifying Annotation
Java Byte Array to InputStream