若依框架(RuoYi)是一个基于Spring Boot和Spring Cloud的开源快速开发平台,其主要特点是简化了企业级应用开发的流程。MinIO则是一款高性能的对象存储系统,兼容Amazon S3 API,适合存储大量非结构化数据,如图片、视频、日志文件等。将MinIO整合到若依框架中,可以为应用程序提供高效、可靠的对象存储解决方案。
以下是如何在若依框架中整合MinIO实现对象存储的详细步骤:
./minio server /data
或者使用Docker:
docker run -p 9000:9000 -p 9001:9001 --name minio \
-e "MINIO_ROOT_USER=admin" \
-e "MINIO_ROOT_PASSWORD=123456" \
-v /data:/data \
minio/minio server /data --console-address ":9001"
在若依项目的pom.xml
文件中添加MinIO客户端依赖:
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.3</version>
</dependency>
在若依框架的配置文件application.yml
中添加MinIO的相关配置:
minio:
endpoint: http://localhost:9000
accessKey: admin
secretKey: 123456
bucketName: test-bucket
编写一个工具类用于封装MinIO的操作逻辑,例如上传文件、下载文件、删除文件等。
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.InputStream;
@Component
public class MinioUtil {
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Value("${minio.bucketName}")
private String bucketName;
@Autowired
private MinioClient minioClient;
/**
* 初始化MinIO客户端
*/
public MinioUtil() {
try {
this.minioClient = new MinioClient(endpoint, accessKey, secretKey);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 上传文件到MinIO
*
* @param objectName 文件名
* @param inputStream 文件流
* @throws Exception
*/
public void uploadFile(String objectName, InputStream inputStream) throws Exception {
if (!minioClient.bucketExists(bucketName)) {
minioClient.makeBucket(bucketName);
}
minioClient.putObject(bucketName, objectName, inputStream, "application/octet-stream");
}
/**
* 下载文件
*
* @param objectName 文件名
* @return 文件流
* @throws Exception
*/
public InputStream downloadFile(String objectName) throws Exception {
return minioClient.getObject(bucketName, objectName);
}
/**
* 删除文件
*
* @param objectName 文件名
* @throws Exception
*/
public void deleteFile(String objectName) throws Exception {
minioClient.removeObject(bucketName, objectName);
}
}
在若依框架的控制器中调用MinioUtil
提供的方法,实现文件上传、下载、删除等功能。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
@RestController
@RequestMapping("/minio")
public class MinioController {
@Autowired
private MinioUtil minioUtil;
/**
* 文件上传接口
*
* @param file 文件
* @return 返回结果
*/
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) {
try {
InputStream inputStream = file.getInputStream();
minioUtil.uploadFile(file.getOriginalFilename(), inputStream);
return "文件上传成功";
} catch (Exception e) {
e.printStackTrace();
return "文件上传失败";
}
}
/**
* 文件下载接口
*
* @param fileName 文件名
* @param response 响应
*/
@GetMapping("/download")
public void downloadFile(@RequestParam String fileName, HttpServletResponse response) {
try {
InputStream inputStream = minioUtil.downloadFile(fileName);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件删除接口
*
* @param fileName 文件名
* @return 返回结果
*/
@DeleteMapping("/delete")
public String deleteFile(@RequestParam String fileName) {
try {
minioUtil.deleteFile(fileName);
return "文件删除成功";
} catch (Exception e) {
e.printStackTrace();
return "文件删除失败";
}
}
}
启动项目后,可以通过Postman或其他工具测试文件上传、下载、删除的功能。以下是测试步骤:
/minio/upload
,附带文件参数。/minio/download?fileName=test.txt
。/minio/delete?fileName=test.txt
。