The simplest example of SpringBoot integration minio (with integrated swagger)

Good blogs I saw during my study :

https://blog.csdn.net/weixin_45415885/article/details/102870687

https://www.cnblogs.com/q149072205/p/12851667.html

1. Download

Windows download address: https://min.io/download#/windows For
other system downloads, you can also click this link to download after selecting the system.

Two, installation

1. Create a new directory to store the minio files. The file name I created is C:\minioData. CMD enters the location where the minio.exe just downloaded is placed, and executes minio.exe server C:\minioData (because I am running on a virtual machine) , There is only one partition, so I put it on the C drive, I don’t recommend it on the C drive for data storage) The
Insert picture description here
red font is the initial login account and password, Browser Access: for the access address
2. Create a new bucket to store resources
Insert picture description here

Three, file upload

Key code:

@RestController
public class MinioController {
    
    
    private static String url = "http://10.0.19.27:9000"; 
    private static String accessKey = "minioadmin";
    private static String secretKey = "minioadmin";

    //上传文件到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("test",fileName,is,contentType); 
            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("test", "9990.jpg");
            response.setHeader("Content-Disposition", "attachment;filename=" + "9990.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("test", "9990.jpg");
            return url;
        }catch (Exception e){
    
    
            return "失败";
        }
    }
}

All the codes have been uploaded to Code Cloud , and all criticisms and corrections are welcome.

(Supplement: The address to access swagger is http://127.0.0.1:8080/swagger-ui.html )

Guess you like

Origin blog.csdn.net/weixin_42656358/article/details/107529555