温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot如何整合minio

发布时间:2022-04-06 10:56:56 来源:亿速云 阅读:366 作者:iii 栏目:开发技术

这篇文章主要介绍“SpringBoot如何整合minio”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot如何整合minio”文章能帮助大家解决问题。

首先添加Minio的依赖

<dependency>         <groupId>io.minio</groupId>         <artifactId>minio</artifactId>         <version>3.0.10</version>     </dependency>

然后写一个controller类

这只是一个简单的demo,没有进行任何的封装,可以根据实际情况进行封装。

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

关于“SpringBoot如何整合minio”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI