When you're building a Spring Boot application that handles media content, streaming video files is a common requirement. But what if those videos are stored in MinIO? And what if you want to stream them directly to the browser or client—without forcing the user to download the file?
Good news: it’s not only possible, but also quite simple with Spring Boot and the MinIO Java SDK.
In this article, we’ll show you how to stream video content directly from MinIO in a Spring Boot application. This method is ideal for scenarios like media previews, learning platforms, or any web app where users need to view video content directly in their browser.
What is MinIO?
MinIO is an open-source object storage solution that’s S3-compatible, lightweight, and well-suited for cloud-native environments. It’s widely used in both development and production setups where storing and accessing large files—like videos—is a regular task.
What You'll Learn
- How to set up MinIO connection in Spring Boot
- How to read and stream video files using MinIO SDK
- How to send video responses using Spring Boot controllers
- How to handle video content types correctly
Step 1: Add the MinIO SDK to Your Project
If you're using Maven, add the following dependency:
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.3</version>
</dependency>
Step 2: MinIO Configuration
Define your MinIO connection settings in
application.properties
:
minio.url=http://localhost:9000
minio.accessKey=minioadmin
minio.secretKey=minioadmin123
minio.bucketName=videos
Next, create a config class to initialize the MinIO client:
@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 3: Service to Fetch Video from MinIO
Now let’s write a service to get the video file as a stream:
@Service
public class MinioService {
@Value("${minio.bucketName}")
private String bucketName;
@Autowired
private MinioClient minioClient;
public InputStream getVideo(String fileName) throws Exception {
return minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.build()
);
}
}
Step 4: Controller to Stream Video
Here’s the controller that streams the video directly to the browser:
@RestController
@RequestMapping("/videos")
public class VideoController {
@Autowired
private MinioService minioService;
@GetMapping("/{filename}")
public ResponseEntity<byte[]> streamVideo(@PathVariable String filename) {
try (InputStream inputStream = minioService.getVideo(filename)) {
byte[] videoBytes = inputStream.readAllBytes();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf("video/mp4")); // adjust for file type
headers.setContentLength(videoBytes.length);
return new ResponseEntity<>(videoBytes, headers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
Note: Make sure your video files are in a browser-compatible format like .mp4
.
Step 5: Try It Out in a Browser or HTML5 Player
Once your Spring Boot app is running, you can test video streaming by navigating to:
http://localhost:8080/videos/sample.mp4
Or embed the video in an HTML5 player:
<video width="640" height="360" controls>
<source src="http://localhost:8080/videos/sample.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
The video should start playing immediately—no download required.
Tips for Better Streaming Performance
- Avoid loading large files into memory all at once (use streaming if needed)
- Enable range requests for better video controls (seek, pause, etc.)
- Use proper content types (
video/mp4
, video/webm
, etc.)
- Consider caching frequently streamed videos
Support Video Seeking (Range Requests)
If you want to support advanced video controls like seeking, you’ll need to handle
Range
headers. This requires more logic than our basic example but significantly improves the user experience for video playback.
image quote pre code