文章目录
- 配置
- 1. 部署MinIO服务
- 2. 整合SpringBoot
- 功能实现
- 1. 文件上传
- 2. 文件下载
- 总结
配置
1. 部署MinIO服务
这里以docker为例:
- 安装minio命令
表示MinIO 的 API 服务默认运行在 9000 端口,并且映射到容器的9000端口docker run -p 9000:9000 -p 9001:9001 \ --name minio \ -v /path/to/data:/data \ -e "MINIO_ROOT_USER=minioadmin" \ -e "MINIO_ROOT_PASSWORD=minioadmin" \ minio/minio server /data --console-address ":9001"
MinIO 的控制台(Web UI)默认运行在 9001 端口。
注意:服务器部署记得开放9000和9001端口。 - 登录MinIO的ui控制台“ip+端口号”
得到如下界面,输入配置的用户名和密码,也就是docker run命令中的-e "MINIO_ROOT_USER=minioadmin" \ -e "MINIO_ROOT_PASSWORD=minioadmin" \
2. 整合SpringBoot
- maven依赖:
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.5.14</version> </dependency>
- application.yaml
minio: endpoint: http://你部署minio服务的ip地址:9000 # MinIO 服务器地址 access-key: minioadmin # 访问密钥 secret-key: minioadmin # 秘密密钥 bucket: test # 默认存储桶名称
- 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.endpoint}") private String endpoint; @Value("${minio.access-key}") private String accessKey; @Value("${minio.secret-key}") private String secretKey; @Bean public MinioClient minioClient() { return MinioClient.builder() .endpoint(endpoint) .credentials(accessKey, secretKey) .build(); } }
功能实现
以为文件上传、下载为例:
1. 文件上传
-
controller层
@RestController @RequestMapping("/file/") public class FileController { @Autowired private FileService fileService; /** * 文件上传 * @param file * @param fileName */ @PostMapping("upload") public Result uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName){ String msg = fileService.uploadFile(file, fileName); return Result.success(msg); }
-
service层
public interface FileService { String uploadFile(MultipartFile file, String fileName); }
@Service public class FileServiceImpl implements FileService { @Autowired private MinioClient minioClient; @Value("${minio.bucket}") private String bucketName; /** * 文件上传 * @param file 需要上传的文件 * @param fileName 文件在 minio 中存储的名称 */ @Override public String uploadFile(MultipartFile file, String fileName) { try { //MultipartFile 转换为 InputStream,以便 MinIO 客户端可以读取文件内容 InputStream inputStream = file.getInputStream(); //将文件上传到 MinIO 服务器 minioClient.putObject(PutObjectArgs.builder() .bucket(bucketName)//文件存储的存储桶名称 .object(fileName) .stream(inputStream, file.getSize(), -1)//文件大小, partSize设置为 -1,MinIO 会使用默认的分块大小(通常是 5MB)进行上传。 .contentType(file.getContentType())//文件的 MIME 类型 .build()); }catch (Exception e){ return "文件上传失败"; } return "文件上传成功"; } }
-
测试结果:
在ui界面中可以查看到上传的文件:
2. 文件下载
-
controller层:
@RestController @RequestMapping("/file/") public class FileController { @Autowired private FileService fileService; /** * 文件下载 * @param fileName * @return */ @GetMapping("download/{fileName}") public Result downloadFile(@PathVariable String fileName, HttpServletResponse response) throws IOException { InputStream inputStream = fileService.downloadFile(fileName); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; fileName" + fileName); IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); return Result.success(); } }
-
service层:
public interface FileService { InputStream downloadFile(String fileName); }
@Service public class FileServiceImpl implements FileService { @Autowired private MinioClient minioClient; @Value("${minio.bucket}") private String bucketName; @Override public InputStream downloadFile(String fileName) { //从 MinIO 对象存储中读取的数据 InputStream inputStream = null; try { inputStream = minioClient.getObject( GetObjectArgs.builder() .object(fileName)//文件名称 .bucket(bucketName)//文件所在存储桶的位置 .build()); } catch (Exception e) { return null; } return inputStream; }
-
测试结果:
便于测试,我这里下载方法是显示下载文件的内容,结果正确。
总结
与MQ类似,需要先部署服务,也有相应的图形化界面便于查看。写好配置文件,使用MinIO提供好的方法类即可。
以上为个人学习分享,如有问题,欢迎指出:)