Production-style example for uploading files to S3 using Spring Boot 3, with:
- unit tests
- controller tests
- integration test using Testcontainers + LocalStack
- Docker Compose for local manual testing
- Java 21
- Spring Boot 3.3.x
- AWS SDK v2
- JUnit 5 + Mockito
- Testcontainers + LocalStack
- Docker Compose
mvn testmvn verifydocker compose up --buildcurl -X POST http://localhost:8080/api/files/upload \
-F "file=@/absolute/path/to/sample.txt"Environment variables:
APP_S3_BUCKETAPP_AWS_REGIONAPP_S3_ENDPOINTAPP_AWS_ACCESS_KEYAPP_AWS_SECRET_KEYAPP_S3_PATH_STYLEAPP_S3_AUTO_CREATE_BUCKET
- In AWS, keep
APP_S3_ENDPOINTempty and do not set static credentials if the app runs with IAM role / default credential chain. - For LocalStack, use endpoint
http://localhost:4566orhttp://localstack:4566inside Docker Compose. - Bucket auto-creation is enabled only when
APP_S3_AUTO_CREATE_BUCKET=true.
curl -X POST http://localhost:8080/api/files/upload
-F "file=@/Users/vinodkumar/Documents/Technologies/AWS/springboot-s3-localstack/transactions.csv"