8000 GitHub - gitproject09/storageService: Comprehensive file storage solution for Spring Boot
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

gitproject09/storageService

Repository files navigation

Storage Service Solution

This project provides a comprehensive file storage solution for Spring Boot applications, enabling secure file uploads, downloads, and management for various use cases.

Features

  • ✅ 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

Prerequisites

  • Java 21
  • Spring Boot 3.x
  • MinIO server (or compatible S3 storage)
  • MySQL/PostgreSQL/H2 database

Quick Start

  1. 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
  2. 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
  3. 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
  4. Run the application

    ./gradlew bootRun
    # OR
    ./mvnw spring-boot:run

Project Structure

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

Usage Examples

Uploading a Product Image

@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());

Uploading a User Profile Picture

@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);

Working with Raw Storage

@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");

Security

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...

Configuration Options

File Size Limits

Configure maximum file sizes in application.yml:

spring:
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 10MB

Token Expiration

Configure how long file access tokens remain valid:

app:
  image:
    token:
      expiration: 300  # 5 minutes, in seconds

API Endpoints

Generic File API

  • GET /api/files/**?token=<token> - Retrieve any file using its path and a valid token

Product File API

  • POST /api/products/{productId}/files/images - Upload a product image
  • POST /api/products/{productId}/files/thumbnail - Upload a product thumbnail

User File API

  • POST /api/users/{userId}/files/profile-picture - Upload a user profile picture
  • POST /api/users/{userId}/files/nid/front - Upload an NID front image
  • POST /api/users/{userId}/files/nid/back - Upload an NID back image

Integration with Existing Services

The storage service easily integrates with your existing services. See examples in:

  • ProductService.java - Shows integration with product management
  • UserService.java - Shows integration with user management

Dependencies

  • Spring Boot Data JPA
  • Spring Boot Web
  • MinIO Client (io.minio:minio)
  • JSON Web Token (io.jsonwebtoken:jjwt)
  • Apache Commons IO

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

About

Comprehensive file storage solution for Spring Boot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0