A tiny Spring Boot library that auto‑configures the AWS S3 SDK v2 and gives you:
- An
S3Client - An
S3Presigner - A small
S3MServiceto generate pre‑signed upload/download URLs - Optional HTTP endpoints to request pre‑signed URLs from your frontend
- Auto‑configured beans (no boilerplate):
S3Client,S3Presigner,S3MService. - Fail‑fast startup: validates access to the configured bucket.
- Simple service API for generating pre‑signed URLs.
- Optional REST endpoints at
/api/s3m(opt‑in via config).
Add the dependency to your pom.xml. Check that GitHub repository is available,
instructions here
<dependency>
<groupId>it.trinex</groupId>
<artifactId>s3m</artifactId>
<version>0.0.4</version>
</dependency>Configure your AWS credentials, endpoint, and bucket under the s3m prefix.
application.yml:
s3m:
accessKeyId: YOUR_ACCESS_KEY_ID
secretAccessKey: YOUR_SECRET_ACCESS_KEY
s3:
bucketName: your-bucket
endpoint: https://minio.or.r2.endpoint
# Optional: region used for signing. Defaults to us-east-1 if omitted.
# For Cloudflare R2 use: auto
region: us-east-1
# Optional: turn on built‑in endpoints to upload and download files
autoendpoint: false # default is falseNotes:
- If you see "Unable to load region from any of the providers", set
s3m.s3.regionor rely on the defaultus-east-1. - For Cloudflare R2 set region to
auto. - Startup validation: the auto‑configuration performs a
HeadBucketcall during startup. If the bucket is not accessible with the provided credentials/region, the application will fail to start with a clear error.
There are two ways to use the library: programmatically via S3MService, or via the optional HTTP endpoints.
Inject S3MService wherever you need to generate pre‑signed URLs:
import it.trinex.s3m.S3MService;
import org.springframework.stereotype.Service;
@Service
public class MyUploadService {
private final S3MService s3m;
public MyUploadService(S3MService s3m) {
this.s3m = s3m;
}
public String createUploadUrl(String key) {
// URL valid for 5 minutes;
return s3m.generateUploadUrl(key, 5);
}
public String createDownloadUrl(String key) {
// URL valid for 10 minutes;
return s3m.generateDownloadUrl(key, 10);
}
}S3MService methods:
generateUploadUrl(key, expireMinutes)generateDownloadUrl(key, expireMinutes)
Enable the controller by adding:
s3m:
autoendpoint: trueAvailable endpoints under /api/s3m:
GET /api/s3m/upload?key=myfile.png&expireMinutes=1GET /api/s3m/download?key=myfile.png&expireMinutes=1
Both return a simple JSON body:
{
"url": "https://...",
"key": "/direct/myfile.png"
}Example cURL:
curl "http://localhost:8080/api/s3m/upload?key=avatar.png&expireMinutes=5"Use the returned url to PUT the bytes directly to S3 from your client.
- Startup fails with
Unable to access S3 bucket:- Verify
s3m.s3.bucketNameands3m.s3.endpoint. - Verify
s3m.accessKeyIdands3m.secretAccessKeyhave permissions (e.g.,s3:PutObject,s3:GetObject,s3:ListBucket). - Check for VPC endpoint or network restrictions affecting S3.
- Verify
- URL immediately expires or is rejected:
- Ensure your server/client clocks are reasonably in sync.
- Increase
expireMinutes.
This library is released into the public domain under The Unlicense.
- License file: LICENSE (included in this repository)
- SPDX identifier: Unlicense
- More info: https://unlicense.org/