1. 安裝MinIO
使用Docker部署MinIO
-
拉取MinIO鏡像:
docker pull minio/minio
這將從Docker Hub中獲取最新的MinIO鏡像。
創建目錄:
mkdir -p /home/minio/config
mkdir -p /home/minio/data
?這些目錄將用于持久化MinIO的數據和配置文件
創建MinIO容器并運行:?
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"
-
這將啟動MinIO服務,使其可以通過主機的9000端口和9090端口進行訪問。
-
登錄MinIO控制臺: 安裝完成后,通過瀏覽器訪問MinIO控制臺,默認地址為
http://localhost:9000
,使用設置的訪問密鑰和秘密密鑰進行登錄。
2. Spring Boot集成MinIO
添加依賴
在pom.xml
中添加以下依賴:
<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.5.2</version>
</dependency>
配置MinIO
在application.properties
中添加MinIO的配置:
minio.host=http://localhost:9000
minio.access-key=minioadmin
minio.secret-key=minioadmin
minio.bucket=test-bucket
創建MinIO配置類?
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MinioConfig {@Value("${minio.host}")private String host;@Value("${minio.access-key}")private String accessKey;@Value("${minio.secret-key}")private String secretKey;@Beanpublic MinioClient minioClient() {return MinioClient.builder().endpoint(host).credentials(accessKey, secretKey).build();}
}
創建存儲桶?
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class MinioService {@Autowiredprivate MinioClient minioClient;public void createBucket(String bucketName) {if (!minioClient.bucketExists(b -> b.bucket(bucketName))) {minioClient.makeBucket(m -> m.bucket(bucketName));}}
}
文件上傳?
import io.minio.MinioClient;
import io.minio.PutObjectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.io.InputStream;@Service
public class FileUploadService {@Autowiredprivate MinioClient minioClient;public String uploadFile(MultipartFile file, String bucketName, String objectName) throws Exception {try (InputStream inputStream = file.getInputStream()) {PutObjectResponse response = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, file.getSize(), -1).contentType(file.getContentType()).build());return "http://localhost:9000/" + bucketName + "/" + objectName;}}
}
文件下載?
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;@RestController
public class FileDownloadController {@Autowiredprivate MinioClient minioClient;@GetMapping("/download")public void downloadFile(@RequestParam String bucketName, @RequestParam String objectName, HttpServletResponse response) {try {InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());response.setContentType("application/octet-stream");response.setHeader("Content-Disposition", "attachment; filename=" + objectName);stream.transferTo(response.getOutputStream());} catch (Exception e) {e.printStackTrace();}}
}
通過以上步驟,你可以在Spring Boot中成功集成并使用MinIO進行文件存儲和管理。?
?
?
?
?
?
?
?
?
?