SpringBoot整合minio最简单样例(附带整合swagger)

学习过程中看到的好的博客

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

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

一、下载

Windows下载地址:https://min.io/download#/windows
其他系统下载也可以点击此链接,进行系统选择后下载。

二、安装

1、新建一个目录存放minio文件,我建的文件名叫 C:\minioData,CMD进入刚刚下载的minio.exe放置得位置,执行minio.exe server C:\minioData(因为我是在虚拟机上运行,只做了一个分区,所以放在C盘,个人不建议放在C盘做数据存储)
在这里插入图片描述
红色字体为初始登录账号和密码,Browser Access: 为访问地址
2、新建一个桶,用来存储资源
在这里插入图片描述

三、文件上传

关键代码:

@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 "失败";
        }
    }
}

全部代码已上传至码云,欢迎各位批评指正。

(补充:访问 swagger 地址为http://127.0.0.1:8080/swagger-ui.html)

猜你喜欢

转载自blog.csdn.net/weixin_42656358/article/details/107529555
今日推荐