- Replies:
- 0
- Words:
- 6535

minio server /data
http://localhost:9000
in your browser.audio-files
) to store your audio content.Medium
pom.xml
:
<dependency>
<groupId>io.minio</groupId>
<artifactId>>minio</artifactId>
<version>8.3.4</version>
</dependency>
application.properties
or application.yml
, add:minio.url=http://localhost:9000
minio.access-key=minioadmin
minio.secret-key=minioadmin
minio.bucket-name=audio-files
@Configuration
public class MinioConfig {
@Value("${minio.url}")
private String minioUrl;
@Value("${minio.access-key}")
private String accessKey;
@Value("${minio.secret-key}")
private String secretKey;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(minioUrl)
.credentials(accessKey, secretKey)
.build();
}
}
@RestController
@RequestMapping("/audio")
public class AudioStreamController {
@Autowired
private MinioClient minioClient;
@Value("${minio.bucket-name}")
private String bucketName;
@GetMapping("/stream/{filename}")
public void streamAudio(@PathVariable String filename, HttpServletResponse response) {
try {
InputStream inputStream = minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(filename)
.build()
);
response.setContentType("audio/mpeg");
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
inputStream.close();
} catch (Exception e) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
}
}
@GetMapping("/stream/{filename}")
public ResponseEntity<Resource> streamAudio(@PathVariable String filename, @RequestHeader HttpHeaders headers) {
try {
InputStream inputStream = minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(filename)
.build()
);
byte[] bytes = IOUtils.toByteArray(inputStream);
ByteArrayResource resource = new ByteArrayResource(bytes);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.setContentLength(bytes.length);
return new ResponseEntity<>(resource, responseHeaders, HttpStatus.PARTIAL_CONTENT);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
<audio>
tag to play the streamed audio:
<audio controls>
<source src="http://localhost:8080/audio/stream/sample.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
This setup allows users to play audio files directly from your application without downloading them, providing a seamless listening experience.
I was looking for this, thanks a lot!
easy to understand
Great! Thanks for your explanation!
Thanks for the comment! Feel free to share more thoughts if you have any—this is a space for exchanging ideas and discussing Java.
Great explanation! @Autowired really does make dependency injection in Spring so much simpler. I love how it keeps our code cleaner and more flexible, especially by avoiding manual instantiation and m
Absolutely! autowired helps a lot on spring framework. We don't need creating new code for same services
image quote pre code