spring-aws-s3-java

Spring AWS S3 Java Library

License

License

Categories

Categories

Java Languages AWS Container PaaS Providers
GroupId

GroupId

com.github.migangqui
ArtifactId

ArtifactId

spring-aws-s3-java
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

spring-aws-s3-java
Spring AWS S3 Java Library
Project URL

Project URL

https://github.com/migangqui/spring-aws-s3-api
Source Code Management

Source Code Management

https://github.com/migangqui/spring-aws-s3-api

Download spring-aws-s3-java

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.migangqui/spring-aws-s3-java/ -->
<dependency>
    <groupId>com.github.migangqui</groupId>
    <artifactId>spring-aws-s3-java</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.migangqui/spring-aws-s3-java/
implementation 'com.github.migangqui:spring-aws-s3-java:1.1.0'
// https://jarcasting.com/artifacts/com.github.migangqui/spring-aws-s3-java/
implementation ("com.github.migangqui:spring-aws-s3-java:1.1.0")
'com.github.migangqui:spring-aws-s3-java:jar:1.1.0'
<dependency org="com.github.migangqui" name="spring-aws-s3-java" rev="1.1.0">
  <artifact name="spring-aws-s3-java" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.migangqui', module='spring-aws-s3-java', version='1.1.0')
)
libraryDependencies += "com.github.migangqui" % "spring-aws-s3-java" % "1.1.0"
[com.github.migangqui/spring-aws-s3-java "1.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.25

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.18

Project Modules

There are no modules declared in this project.

Spring AWS S3 API (Java/Kotlin)

Here we are a Java and a Kotlin API to manage files of AmazonS3 in Spring framework. In order to use it, are necesaries the following steps:

Add dependency to pom.xml:

If you use Java (https://mvnrepository.com/artifact/com.github.migangqui/spring-aws-s3-java):

<dependency>
	<groupId>com.github.migangqui</groupId>
	<artifactId>spring-aws-s3-java</artifactId>
	<version>${currentVersion}</version>
</dependency>

If you use Kotlin (https://mvnrepository.com/artifact/com.github.migangqui/spring-aws-s3-kotlin):

<dependency>
	<groupId>com.github.migangqui</groupId>
	<artifactId>spring-aws-s3-kotlin</artifactId>
	<version>${currentVersion}</version>
</dependency>

${currentVersion} right now is 1.1.0

Both them are in Maven Central.

Register the following properties in your application.yml:

amazon:
    s3:
        accessKey: [AMAZON_ACCESS_KEY]
        secretKey: [AMAZON_SECRET_KEY]
        bucket.name: example-bucket-s3
    region: [GovCloud("us-gov-west-1"),
               US_EAST_1("us-east-1"),
               US_WEST_1("us-west-1"),
               US_WEST_2("us-west-2"),
               EU_WEST_1("eu-west-1"),
               EU_CENTRAL_1("eu-central-1"),
               AP_SOUTH_1("ap-south-1"),
               AP_SOUTHEAST_1("ap-southeast-1"),
               AP_SOUTHEAST_2("ap-southeast-2"),
               AP_NORTHEAST_1("ap-northeast-1"),
               AP_NORTHEAST_2("ap-northeast-2"),
               SA_EAST_1("sa-east-1"),
               CN_NORTH_1("cn-north-1")]**

** Only one and only the string of the region.

Enable async

Add @EnableAsync annotation in your Spring Application class to enable async upload method.

File size

To controle max size of files you can upload, set the following properties:

spring:
    servlet:
        multipart:
            max-file-size: 128KB
            max-request-size: 128KB

Localstack support

This library can be tested with Localstack (https://github.com/localstack/localstack). You only have to set the following properties in your application.yml:

localstack:
  enabled: false (by default)
  endpoint: http://localhost:4572
  region: us-east-1

In order to run easily Localstack, I have added docker-compose.yml file to the folder localstack. You have run the command docker-compose up to make it work.

I hardly recommend install AWS CLI in your local. It helps you to manage the buckets to run the tests with Localstack. Here you are the documentation to install the version 2: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html

To create a local bucket you must run this command aws2 --endpoint-url=http://localhost:4572 s3 mb s3://mytestbucket

To check out if the bucket has been created run this command aws2 --endpoint-url=http://localhost:4572 s3 ls

When you create a bucket, you have to add yourbucketname.localhost to your hosts local file mapped to 127.0.0.1.

Here we are the AWS CLI S3 command options: https://docs.aws.amazon.com/en_en/cli/latest/userguide/cli-services-s3-commands.html

How to use

You have to inject AmazonS3Service as dependency in your Spring component. The service provide these methods:

Java
public interface AmazonS3Service {
	
    UploadFileResponse uploadFile(UploadFileRequest request);
    
    Future<UploadFileResponse> uploadFileAsync(UploadFileRequest request);

    GetFileResponse getFile(GetFileRequest request);
    
    DeleteFileResponse deleteFile(DeleteFileRequest request);

}
Kotlin
interface AmazonS3Service {
    
    fun uploadFile(request: UploadFileRequest): UploadFileResponse
    
    fun uploadFileAsync(request: UploadFileRequest): Future<UploadFileResponse>

    fun getFile(request: GetFileRequest): GetFileResponse

    fun deleteFile(request: DeleteFileRequest): DeleteFileResponse

}

License

This project is licensed under the MIT License - see the LICENSE file for details

Improvements

v1.1.0

  • Improve configuration
  • Not component scan necessary
  • Refactor code
  • Chose bucket name dynamically
  • Beans to manage all request and responses
  • AWS S3 File access configuration (Private by default)

Next improvements

I'm very pleased to receive suggestions.

Versions

Version
1.1.0
1.0.0