SpringBoot整合minio文件服务器,附上Docker部署方式

·  阅读 428
SpringBoot整合minio文件服务器,附上Docker部署方式

今天闲来无事研究了一下minio这个文件服务器,并在自己的服务器上搭建了minio的环境并且整合springboot写出了一个小demo,直接放代码

1. docker部署minio

  • docker pull minio/minio: 默认下载的最新的版本
  • docker run -d
    -p 9000:9000
    -p 9001:9001
    --name minio1
    -v /home/minio/data:/data
    -e "MINIO_ROOT_USER=AKIAIOSFODNN7EXAMPLE"
    -e "MINIO_ROOT_PASSWORD=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
    minio/minio server /data --console-address ":9001"

最新版的minio需要配置两个端口,一个是api的,一个是控制台的那个.-v挂载数据卷 -e环境变量配置的是用户名和密码 最后minio/minio server /data(minio/minio 是镜像名字、 /data:数据存储位置)

部署完成后可以在ip:9001访问控制台,账户密码就是-e里边的内容

2. SpringBoot整合minio

  • 导入依赖

    <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
       </dependency>
       <dependency>
           <groupId>com.jlefebure</groupId>
           <artifactId>spring-boot-starter-minio</artifactId>
           <version>1.4</version>
       </dependency>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
       </dependency>
       <dependency>
           <groupId>cn.hutool</groupId>
           <artifactId>hutool-all</artifactId>
           <version>5.6.5</version>
       </dependency>
    </dependencies>
    复制代码
  • 配置文件application.yml

    server:
      port: 10088
      spring:
          minio:
            url: http://10.0.40.61:9000/
            access-key: AKIAIOSFODNN7EXAMPLE
            secret-key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
            bucket: apengcxjt
            create-bucket: true
    复制代码
  • 引导类

         /**
          * @author 阿鹏
          * @date 😴2021/9/10 14:45
          */
          @SpringBootApplication
          public class MinioApplication {
                public static void main(String[] args) {
                    SpringApplication.run(MinioApplication.class, args);
          }
        }
    复制代码
  • controller接口

       /**
        * @author 阿鹏
        * @date 😴2021/9/10 14:58
        */
       @RestController
       @RequestMapping("minio/test")
       @Slf4j
       public class MinioController {
    
           @Autowired
           private MinioService minioService;
           @Autowired
           private MinioConfigurationProperties properties;
           @Autowired
           private MinioClient minioClient;
    
           /**
            * 列出所有文件
            */
           @GetMapping("list")
           public List<Item> getFiles() {
               return minioService.list();
           }
    
           /**
            * 根据文件名称下载文件
            */
           @GetMapping("files/{fileName}")
           public void getFileByFileName(@PathVariable("fileName") String fileName,
                                                 HttpServletResponse response) throws MinioException, IOException {
               InputStream is = minioService.get(Paths.get(fileName));
               InputStreamSource source = new InputStreamResource(is);
               // 添加响应头,以附件形式
               fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
               response.addHeader("Content-disposition", "attachment;filename=" + fileName);
               response.setContentType(URLConnection.guessContentTypeFromName(fileName));
    
               IOUtils.copy(is, response.getOutputStream());
               response.flushBuffer();
           }
    
           @PostMapping("upload")
           public void uploadFile(MultipartFile file)  {
               String originalFilename = file.getOriginalFilename();
               originalFilename = "pic" + File.separator + System.currentTimeMillis() +
                       originalFilename.substring(originalFilename.lastIndexOf("."));
               Path path = Paths.get(originalFilename);
    
               try {
                   minioService.upload(path, file.getInputStream(), file.getContentType());
                   String url = minioClient.getObjectUrl(properties.getBucket(), originalFilename);
                   System.out.println("上传完成");
                   System.out.println("url = " + url);
               } catch (Exception e) {
                   e.printStackTrace();
                   log.error("文件上传失败");
               }
           }
    
       }
    复制代码
分类:
后端
标签:
分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改