Home > Java > javaTutorial > How SpringBoot integrates minio

How SpringBoot integrates minio

WBOY
Release: 2023-05-11 08:49:17
forward
1927 people have browsed it

First add the dependencies of Minio

<dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>3.0.10</version>
    </dependency>
Copy after login

Then write a controller class

This is just a simple demo without any encapsulation and can be encapsulated according to the actual situation.

package com.file.server.controller;
import io.minio.MinioClient;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
@RestController
public class MinioController {
   private static String url = "http://127.0.0.1:9000";  //minio服务的IP端口
   private static String accessKey = "W2ZWITFFDWFM5TWS3WI9";  
   private static String secretKey = "dNx++XsRJpjmWVQHWv8djMCFJ0A3YXbEr4qfKHR+";
   
    //上传文件到minio服务
 @PostMapping("upload")
 public String upload(@RequestParam("fileName") MultipartFile file )  {
   try {
       MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
       InputStream is= file.getInputStream(); //得到文件流
       String fileName = file.getOriginalFilename(); //文件名
       String contentType = file.getContentType();  //类型
       minioClient.putObject("file",fileName,is,contentType); //把文件放置Minio桶(文件夹)
       return  "上传成功";
     }catch (Exception e){
         return "上传失败";
     }
    }
    //下载minio服务的文件
    @GetMapping("download")
    public String download(HttpServletResponse response){
        try {
          MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
          InputStream fileInputStream = minioClient.getObject("file", "test.jpg");
          response.setHeader("Content-Disposition", "attachment;filename=" + "test.jpg");
          response.setContentType("application/force-download");
          response.setCharacterEncoding("UTF-8");
          IOUtils.copy(fileInputStream,response.getOutputStream());
          return "下载完成";
        }catch (Exception e){
            return "下载失败";
        }
    }
    //获取minio文件的下载地址
    @GetMapping("url")
    public  String  getUrl(){
        try {
            MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
            String url = minioClient.presignedGetObject("file", "test.jpg");
            return url;
        }catch (Exception e){
            return "获取失败";
        }
    }
}
Copy after login

The above is the detailed content of How SpringBoot integrates minio. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template