Storing and managing audio files is a common feature in modern apps, from podcasts and voice notes to music uploads and audio logs. One of the most efficient ways to handle these files is to store them in object storage like
MinIO, which is lightweight, fast, and fully compatible with AWS S3.
In this guide, you’ll learn how to
upload audio files (like MP3, WAV, or AAC) to MinIO using Spring Boot. We’ll create a simple REST API that handles audio uploads securely and efficiently.
What You'll Learn
- How to configure and connect MinIO in Spring Boot
- How to create a REST API endpoint for uploading audio files
- How to validate audio file types
- How to store audio files in MinIO buckets
Tip: This guide assumes your MinIO server is already running and accessible from your Spring Boot app.
Step 1: Add the MinIO SDK to Your Project
Start by adding the MinIO Java SDK to your Maven project:
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.3</version> <!-- Use the latest version -->
</dependency>
If you’re using Gradle, add the equivalent to your
build.gradle
.
Step 2: Set Up MinIO Configuration
Add your MinIO server details in
application.properties
:
minio.url=http://localhost:9000
minio.accessKey=minioadmin
minio.secretKey=minioadmin
minio.bucketName=audios
These properties will be injected into your MinIO client configuration.
Step 3: Create the MinIO Client Configuration
Create a config class that initializes the MinIO client bean:
@Configuration
public class MinioConfig {
@Value("${minio.url}")
private String url;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(url)
.credentials(accessKey, secretKey)
.build();
}
}
Step 4: Build the Audio Upload Service
This service handles checking, saving, and uploading audio files to MinIO:
@Service
public class AudioUploadService {
@Value("${minio.bucketName}")
private String bucketName;
private final MinioClient minioClient;
public AudioUploadService(MinioClient minioClient) {
this.minioClient = minioClient;
}
public String uploadAudio(MultipartFile file) throws Exception {
String contentType = file.getContentType();
if (contentType == null || !contentType.startsWith("audio/")) {
throw new IllegalArgumentException("Only audio files are allowed.");
}
// Check if bucket exists, create if not
boolean found = minioClient.bucketExists(BucketExistsArgs.builder()
.bucket(bucketName)
.build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
}
// Generate unique file name
String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
// Upload to MinIO
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.stream(file.getInputStream(), file.getSize(), -1)
.contentType(contentType)
.build());
return fileName;
}
}
Step 5: Create the Upload Endpoint
Now let’s expose a REST API to handle the audio file upload:
@RestController
@RequestMapping("/api/audios")
public class AudioUploadController {
private final AudioUploadService audioUploadService;
public AudioUploadController(AudioUploadService audioUploadService) {
this.audioUploadService = audioUploadService;
}
@PostMapping("/upload")
public ResponseEntity<String> uploadAudio(@RequestParam("file") MultipartFile file) {
try {
String fileName = audioUploadService.uploadAudio(file);
return ResponseEntity.ok("Audio uploaded successfully: " + fileName);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body("Invalid file type: " + e.getMessage());
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Upload failed: " + e.getMessage());
}
}
}
Step 6: Test the API
Use
Postman,
curl, or a frontend app to upload an audio file.
Example with curl
:
curl -X POST http://localhost:8080/api/audios/upload \
-F "file=@sample-audio.mp3"
The API will return a message with the unique filename used in MinIO storage.
Best Practices
- Limit upload size to prevent abuse or server overload
- Allow only specific audio formats (e.g., MP3, WAV, AAC)
- Store uploaded file metadata (e.g., duration, user ID) in your database
- Secure your MinIO server in production environments
- Generate and return file URLs if files need to be accessed publicly
image quote pre code