# aloha-minio-spring-boot-starter **Repository Path**: zhdain/minio ## Basic Information - **Project Name**: aloha-minio-spring-boot-starter - **Description**: 对minio的操作封装 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2023-04-05 - **Last Updated**: 2023-07-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 1. 依赖 将 aloha-minio-spring-boot-starter 安装到本地的maven仓库 在项目中导入如下依赖 ```xml org.springframework.boot spring-boot-starter-web com.aloha aloha-minio-spring-boot-starter 1.0-SNAPSHOT ``` ## 2. 配置文件 application.yaml ```yaml aloha: minio: bucket: leadnews # 这个bucket需要提前在Minio上创建 endpoint: http://ubuntu.server:9000 access-key: VKJFZ69ZD4ZWLO3UZSZL secret-key: KIibwuZshOGuF+8oUaPXX3o6kZMN8kymLe8yZIZJ ``` ## 3. 使用 在 controller 中注入 **MinioService** ### 3.1 上传小文件 ```java @ApiOperation("上传小文件") @PostMapping("/upload/small") public MinioResult upload(@RequestParam("file") MultipartFile file) throws IOException { // generatePath()是生成 yyyy/MM/dd/格式字符串的一个方法 String objectName = generatePath() + file.getOriginalFilename(); // 直接将从前端接收到的文件转成字节数组 return minioService.uploadFile(file.getBytes(), objectName); } ``` 演示: ![image-20230405165608535](img/image-20230405165608535.png) ![image-20230405165720536](img/image-20230405165720536.png) ### 3.2 上传大文件 ```java @ApiOperation("上传大文件") @PutMapping("/upload/big") public MinioResult uploadBigFile() { // 本机的文件位置 String localPath = "G:\\学习资料\\侯捷视频课\\1 - C++面向对象高级编程(上)\\1.C++编程简介.mp4"; return minioService.uploadFile(localPath, generatePath() + "C++编程简介.mp4"); } ``` 演示: ![image-20230405170448190](img/image-20230405170448190.png) ![image-20230405170603882](img/image-20230405170603882.png) 查看控制台,可以看见之前上传的文件 ![image-20230405171914179](img/image-20230405171914179.png) ### 3.3 其余功能 其余功能不再演示,有两点需要说明 + 使用删除整个**目录**的操作,传入的参数必须是 **xx1/xx2/xx3/**这种格式,并且 **xx3/目录下存在文件** + 使用直传文件时,前端会拿到一个 policy,然后使用 **PUT**请求直接向 policy发送数据就行,但不要使用**FormData** 将完整的 controller的代码赋在这里: ```java /** * @author DaiZhiHeng * @description 文件上传控制器 * @date 2023/4/5 15:19 */ @Api(tags = "测试minio") @RestController public class MinioController { @Resource private MinioService minioService; @ApiOperation("上传小文件") @PostMapping("/upload/small") public MinioResult upload(@RequestParam("file") MultipartFile file) throws IOException { String objectName = generatePath() + file.getOriginalFilename(); return minioService.uploadFile(file.getBytes(), objectName); } @ApiOperation("删除指定文件") @DeleteMapping("/delete") public boolean delete(@RequestParam("filePath") String filepath) { // filepath: 2020/01/01/xx.jpg return minioService.delete(filepath); } @ApiOperation("上传大文件") @PutMapping("/upload/big") public MinioResult uploadBigFile() { // 本机的文件位置 String localPath = "G:\\学习资料\\侯捷视频课\\1 - C++面向对象高级编程(上)\\1.C++编程简介.mp4"; return minioService.uploadFile(localPath, generatePath() + "C++编程简介.mp4"); } @ApiOperation("判断文件是否存在") @GetMapping("/exist") public boolean exist(@RequestParam("filePath") String filepath) { // filepath: 2020/01/01/xx.jpg return minioService.exists(filepath); } @ApiOperation("下载") @GetMapping("/download") public String download(@RequestParam("filePath") String filepath) { return minioService.download(new File("f:\\1.mp4"), filepath).getAbsolutePath(); } @ApiOperation("删除整个目录") @DeleteMapping("/deleteDir") public boolean deleteDir(@RequestParam("dir") String dir) { // dir: 2020/01/01/(最后的一个 / 不能少) try { minioService.deleteDir(dir); return true; } catch (Exception e) { e.printStackTrace(); } return false; } @ApiOperation("直传文件") @GetMapping("/policy") public String policy(@RequestParam("filename") String filename) { return minioService.policy(30, TimeUnit.MINUTES, generatePath() + filename); } private String generatePath() { return new SimpleDateFormat("yyyy/MM/dd/").format(new Date()); } } ```