在Spring Boot项目中整合MinIO,可以按照以下步骤进行操作:
1. 添加MinIO依赖
<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.5.9</version></dependency>
2. 配置MinIO连接
minio:endpoint: http://127.0.0.1:9000 #MinIO资源地址accessKey: minioadmin #访问keysecretKey: minioadmin #访问密钥bucketName: test #存储桶名称
3. 创建MinIO配置类
@Data
@Configuration
public class MinioConfig {@Value("${minio.endpoint}")private String endpoint;@Value("${minio.accessKey}")private String accessKey;@Value("${minio.secretKey}")private String secretKey;@Value("${minio.bucketName}")private String bucketName;@Beanpublic MinioClient minioClient() {return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();}
}
4. 创建MinIO工具类
@Component
public class MinioUtil {@Autowiredprivate MinioClient minioClient;@Autowiredprivate MinioConfig minioConfig;/*** 创建存储bucket** @param bucketName 存储bucket名称* @return Boolean*/public boolean makeBucket(String bucketName) {try {minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());return true;} catch (Exception e) {e.printStackTrace();}return false;}/*** 删除存储bucket** @param bucketName 存储bucket名称* @return Boolean*/public boolean removeBucket(String bucketName) {try {minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());return true;} catch (Exception e) {e.printStackTrace();}return false;}/*** 查看存储bucket是否存在** @param bucketName* @return*/public boolean bucketExists(String bucketName) {try {return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());} catch (Exception e) {e.printStackTrace();}return false;}/*** 文件上传** @param objectName 对象名称* @param file 文件* @return*/public String uploadFile(String objectName, MultipartFile file) {try {InputStream stream = file.getInputStream();PutObjectArgs putObjectArgs = PutObjectArgs.builder().bucket(minioConfig.getBucketName()).object(objectName).contentType(file.getContentType()).stream(file.getInputStream(), file.getSize(), -1).build();minioClient.putObject(putObjectArgs);stream.close();return "%s/%s/%s".formatted(minioConfig.getEndpoint(), minioConfig.getBucketName(), objectName);} catch (Exception e) {e.printStackTrace();}return "";}/*** 获取对象url** @param objectName 对象名称* @return*/public String getObjectUrl(String objectName) {try {return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(minioConfig.getBucketName()).object(objectName).build());} catch (Exception e) {e.printStackTrace();}return null;}}
5. 控制器操作
@RestController
@RequestMapping("/api/file")
public class FileController {@Autowiredprivate MinioUtil minioUtil;/*** 创建存储bucket** @param bucketName* @return*/@PostMapping("makeBucket")public Object makeBucket(String bucketName) {var success = minioUtil.makeBucket(bucketName);Map<String, Object> map = new HashMap<>();map.put("success", success);return map;}/*** 删除存储bucket** @param bucketName* @return*/@PostMapping("removeBucket")public Object removeBucket(String bucketName) {var success = minioUtil.removeBucket(bucketName);Map<String, Object> map = new HashMap<>();map.put("success", success);return map;}/*** 查看存储bucket是否存在** @param bucketName* @return*/@GetMapping("bucketExists")public Object bucketExists(String bucketName) {var success = minioUtil.bucketExists(bucketName);Map<String, Object> map = new HashMap<>();map.put("success", success);return map;}/*** 文件上传** @param file* @return*/@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public Object uploadFile(@RequestParam("file") MultipartFile file) {String fileName = file.getOriginalFilename();var fileUrl = minioUtil.uploadFile(fileName, file);Map<String, Object> map = new HashMap<>();map.put("success", true);map.put("data", fileUrl);return map;}/*** 获取对象url** @param objectName* @return*/@GetMapping("getObjectUrl")public Object getObjectUrl(String objectName) {var fileUrl = minioUtil.getObjectUrl(objectName);Map<String, Object> map = new HashMap<>();map.put("success", true);map.put("data", fileUrl);return map;}
}