This project provides a comprehensive file storage solution for Spring Boot applications, enabling secure file uploads, downloads, and management for various use cases.
- ✅ Flexible storage service with MinIO integration
- ✅ Path constants system to maintain organizational structure
- ✅ Secure token-based file access
- ✅ Controllers for serving images and files securely
- ✅ Specialized services for product and user files
- ✅ Easy integration with existing services
- Java 21
- Spring Boot 3.x
- MinIO server (or compatible S3 storage)
- MySQL/PostgreSQL/H2 database
-
Configure your database
Update
application.yml
with your database credentials:spring: datasource: url: jdbc:h2:mem:testdb # For development # url: jdbc:mysql://localhost:3306/yourdb # For MySQL username: your_username password: your_password
-
Configure MinIO
Update MinIO credentials in
application.yml
:minio: endpoint: http://your-minio-server:9000 accessKey: your-access-key secretKey: your-secret-key bucket: name: your-main-bucket
-
Configure security token
Generate a secure random key for file access tokens:
app: image: token: secret: your-secure-random-key-at-least-32-bytes-long expiration: 300 base-url: https://yourdomain.com
-
Run the application
./gradlew bootRun # OR ./mvnw spring-boot:run
com.example.storage/
├── config/
│ └── MinioConfig.java
├── constants/
│ └── StoragePath.java
├── controller/
│ ├── FileController.java
│ ├── ProductFileController.java
│ └── UserFileController.java
├── model/
│ └── FileMetadata.java
└── service/
├── StorageService.java
├── StorageTokenService.java
├── ProductStorageService.java
└── UserStorageService.java
@Autowired
private ProductStorageService productStorageService;
// Store a product image
FileMetadata metadata = productStorageService.storeProductImage(imageFile, productId);
// Get a secure URL for the product image
String secureUrl = productStorageService.getSecureProductImageUrl(productId, metadata.getFilename());
@Autowired
private UserStorageService userStorageService;
// Store a user's profile picture
FileMetadata metadata = userStorageService.storeProfilePicture(imageFile, userId);
// Get a secure URL for the profile picture
String secureUrl = userStorageService.getSecureProfilePictureUrl(userId);
@Autowired
private StorageService storageService;
// Store a file at a custom path
FileMetadata metadata = storageService.storeFile(file, "custom/path/filename.jpg");
// Retrieve a file
InputStream fileStream = storageService.getFile("custom/path/filename.jpg");
// Delete a file
boolean deleted = storageService.deleteFile("custom/path/filename.jpg");
Files are accessed through secure, time-limited tokens to prevent unauthorized access. All file URLs generated by the system include a token parameter that validates the request.
Example secure URL:
https://yourdomain.com/api/files/products/1/thumbnail?token=eyJhbGciOiJIUzI1NiJ9...
Configure maximum file sizes in application.yml
:
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
Configure how long file access tokens remain valid:
app:
image:
token:
expiration: 300 # 5 minutes, in seconds
GET /api/files/**?token=<token>
- Retrieve any file using its path and a valid token
POST /api/products/{productId}/files/images
- Upload a product imagePOST /api/products/{productId}/files/thumbnail
- Upload a product thumbnail
POST /api/users/{userId}/files/profile-picture
- Upload a user profile picturePOST /api/users/{userId}/files/nid/front
- Upload an NID front imagePOST /api/users/{userId}/files/nid/back
- Upload an NID back image
The storage service easily integrates with your existing services. See examples in:
ProductService.java
- Shows integration with product managementUserService.java
- Shows integration with user management
- Spring Boot Data JPA
- Spring Boot Web
- MinIO Client (io.minio:minio)
- JSON Web Token (io.jsonwebtoken:jjwt)
- Apache Commons IO
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.