Minio整合Springboot保姆级使用教程

Minio整合Springboot保姆级使用教程

使用方法

使用minio-spring-boot-starter 完事

POM 文件加入 依赖


<dependency>
    <groupId>plus.ojbk</groupId>
    <artifactId>minio-spring-boot-starter</artifactId>
    <version>1.0.3</version>
</dependency>

<!-- 测试用例页面使用 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <optional>true</optional>
</dependency>

yml 配置


file:
  minio:
#    enable: true
    endpoint: http://172.16.1.240:9000
    bucket: test
    access-key: test
    secret-key: test123456789



spring:
  servlet:
    multipart:
      #指定上传文件允许的最大大小。 默认为 1MB
      max-file-size: 100MB
      #指定多部分/表单数据请求允许的最大大小。 默认值为 10MB。
      max-request-size: 100MB

编写一个controller


import io.minio.ObjectWriteResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import plus.ojbk.minio.core.MinioTemplate;

/**
 * @author wxm
 * @version 1.0
 * @since 2021/6/7 10:03
 */
@Slf4j
@RestController
public class TestController {
    @Autowired
    private MinioTemplate minioTemplate;

    @GetMapping("/index")
    public Object index() {
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }

    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile multipartFile) {
        ObjectWriteResponse res = minioTemplate.putObject(multipartFile);
        String path = res.object();
        // String bucket = res.bucket();
        log.info("File Path = {}", path);
        // TODO 你具体的业务 比如存储 这个path 到数据库等
        return "ok";
    }

    @GetMapping("/delete")
    public String delete(@RequestParam("path") String object) {
        minioTemplate.deleteObject(object);
        return "ok";
    }


    @GetMapping("/get")
    public String get(@RequestParam("path") String object) {
        //默认 1 小时
        String url = minioTemplate.getObject(object);
        //自定义时间
        //String url = minioTemplate.getObject(name, 10, TimeUnit.MINUTES);
        log.info("Get File Url = {}", url);
        return url;
    }

    @GetMapping("/download")
    public void download(@RequestParam("path") String object, HttpServletResponse response) throws Exception {
        InputStream in = minioTemplate.getObjectInputStream(object);
        OutputStream out = response.getOutputStream();
        byte[] buf = new byte[1024];
        int len = 0;
        response.reset();
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(MinioUtils.getFileName(object), "UTF-8"));
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
}


编写一个 测试文件上传的页面index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我是主页</title>
</head>
<body>
<p>测试上传</p>
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传">
</form>


</body>
</html>


关于minio 服务的安装

可参考 服务安装

1 个赞

我明哥封装的 :v:

更新版本 1.0.1 两小时以后可从中央仓库下载
主要完善MinioTemplate 部分功能
修复doc文档无法下载问题

1 个赞

我是抄K 哥你的 :rofl: