1. Install MinIO
Deploy MinIO with Docker
Pull MinIO image:
docker pull minio/minio
This will get the latest MinIO image from Docker Hub.
Create a directory:
mkdir -p /home/minio/config mkdir -p /home/minio/data
These directories will be used to persist MinIO's data and configuration files
Create MinIO container and run:
docker run -p 9000:9000 -p 9090:9090 \ --net=host \ --name minio \ -d --restart=always \ -e "MINIO_ACCESS_KEY=minioadmin" \ -e "MINIO_SECRET_KEY=minioadmin" \ -v /home/minio/data:/data \ -v /home/minio/config:/root/.minio \ minio/minio server /data --console-address ":9090" -address ":9000"
- This will start the MinIO service so that it can be accessed through port 9000 and port 9090 of the host.
-
Log in to the MinIO console: After the installation is completed, access the MinIO console through the browser. The default address is
http://localhost:9000
, use the set access key and secret key to log in.
2. Spring Boot Integration MinIO Add Dependencies
existAdd the following dependencies to:
<dependency> <groupId></groupId> <artifactId>minio</artifactId> <version>8.5.2</version> </dependency>
Configure MinIO
existAdd MinIO configuration:
=http://localhost:9000 -key=minioadmin -key=minioadmin =test-bucket
Create MinIO configuration class
import ; import ; import ; import ; @Configuration public class MinioConfig { @Value("${}") private String host; @Value("${-key}") private String accessKey; @Value("${-key}") private String secretKey; @Bean public MinioClient minioClient() { return () .endpoint(host) .credentials(accessKey, secretKey) .build(); } }
Create a bucket
import ; import ; import ; @Service public class MinioService { @Autowired private MinioClient minioClient; public void createBucket(String bucketName) { if (!(b -> (bucketName))) { (m -> (bucketName)); } } }
File upload
import ; import ; import ; import ; import ; import ; @Service public class FileUploadService { @Autowired private MinioClient minioClient; public String uploadFile(MultipartFile file, String bucketName, String objectName) throws Exception { try (InputStream inputStream = ()) { PutObjectResponse response = ( () .bucket(bucketName) .object(objectName) .stream(inputStream, (), -1) .contentType(()) .build() ); return "http://localhost:9000/" + bucketName + "/" + objectName; } } }
File download
import ; import ; import ; import ; import ; import ; import ; import ; @RestController public class FileDownloadController { @Autowired private MinioClient minioClient; @GetMapping("/download") public void downloadFile(@RequestParam String bucketName, @RequestParam String objectName, HttpServletResponse response) { try { InputStream stream = ( () .bucket(bucketName) .object(objectName) .build() ); ("application/octet-stream"); ("Content-Disposition", "attachment; filename=" + objectName); (()); } catch (Exception e) { (); } } }
Through the above steps, you can successfully integrate and use MinIO for file storage and management in Spring Boot.
This is the end of this article about the detailed steps of Spring Boot integrating MinIO. For more related Spring Boot integrating MinIO content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!