Minio-JAVA SDK 使用2.0
1,确保搭建好的MINIO能进得去
有个一问,为什么Minio没提供封装好的东西供我们调用,需要自己封装一次。有点麻烦。那就动手吧!
2、引入POM相关依赖
<minio.version>6.0.12</minio.version> <guava.version>20.0</guava.version> <!-- MinIO文件系统 --> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>${minio.version}</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency>
3、MinioConfig.yml
# MinIO文件系统 minio: url: http://xx.xx.xx.xxx:9000 access: minioadmin secret: miniostorage bucket: pdms
4、初始化Minio
package com.example.config; import io.minio.MinioClient; import io.minio.errors.MinioException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.io.ByteArrayInputStream; import java.util.HashMap; import java.util.concurrent.ThreadPoolExecutor; /** * Minio文件系统初始化 * * @author wliduo[i@dolyw.com] * @date 2020/2/5 14:36 */ @Configuration public class MinioConfig { /** * logger */ private final static Logger logger = LoggerFactory.getLogger(MinioConfig.class); @Value("${minio.url}") private String url; @Value("${minio.access}") private String access; @Value("${minio.secret}") private String secret; @Value("${minio.bucket}") private String bucket; /** * Minio文件系统配置 * * @param * @return io.minio.MinioClient * @throws * @author wliduo[i@dolyw.com] * @date 2020/2/5 16:10 */ @Bean(name = "minioClient") public MinioClient minioClient() throws Exception { logger.info("---------- Minio文件系统初始化加载 ----------"); MinioClient minioClient = new MinioClient(url, access, secret); // 判断Bucket是否存在 boolean isExist = minioClient.bucketExists(bucket); if(isExist) { logger.info("---------- Minio文件系统Bucket已存在 ----------"); } else { // 不存在创建一个新的Bucket minioClient.makeBucket(bucket); logger.info("---------- Minio文件系统Bucket已创建 ----------"); } logger.info("---------- Minio文件系统初始化完成 ----------"); return minioClient; } }
5、存取工具类,用法了属于是,MinioUtil.java
package com.example.util; import io.minio.MinioClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; /** * MinioUtil工具类 * * @author wliduo[i@dolyw.com] * @date 2020/2/5 17:53 */ @Component public class MinioUtil { /** * logger */ private final static Logger logger = LoggerFactory.getLogger(MinioUtil.class); @Value("${minio.bucket}") private String bucket; /** * minioClient */ private final MinioClient minioClient; @Autowired public MinioUtil(MinioClient minioClient) { this.minioClient = minioClient; } /** * 上传文件 * * @param file * @return java.lang.String * @throws * @author wliduo[i@dolyw.com] * @date 2020/2/6 16:03 */ public String uploadFile(MultipartFile file) { try(ByteArrayInputStream bais = new ByteArrayInputStream(file.getBytes())) { return uploadFile(bais, file.getOriginalFilename()); } catch (IOException e) { logger.error("{}文件上传失败", file.getOriginalFilename()); return ""; } } /** * 上传文件 * * @param stream * @param fileName * @return java.lang.String * @throws * @author wliduo[i@dolyw.com] * @date 2020/2/6 16:04 */ public String uploadFile(InputStream stream, String fileName) { // 获取当前日期,设置文件夹 LocalDateTime localDateTime = LocalDateTime.now(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd/"); // 上传文件路径 StringBuilder objectName = new StringBuilder(localDateTime.format(dateTimeFormatter)); objectName.append('[').append(System.currentTimeMillis()).append(']').append(fileName); try { // 上传文件 minioClient.putObject(bucket, objectName.toString(), stream, new HashMap(16)); return objectName.toString(); } catch (Exception e) { logger.error("{}文件上传失败", objectName); return ""; } } /** * 获取文件 * * @param objectName * @return java.io.InputStream * @throws * @author wliduo[i@dolyw.com] * @date 2020/2/6 16:05 */ public InputStream getFile(String objectName) { try { // 文件是否存在 minioClient.statObject(bucket, objectName); // 获取文件 return minioClient.getObject(bucket, objectName); } catch (Exception e) { logger.error("{}文件获取失败", objectName); return null; } } /** * 直接下载文件 * * @param req * @param res * @param fid * @param fileName * @return void * @throws * @author wliduo[i@dolyw.com] * @date 2020/3/5 9:45 */ public void lookUploadFile(HttpServletRequest req, HttpServletResponse res, String fid, String fileName) { try (InputStream in = minioUtil.getFile(fid); OutputStream output = res.getOutputStream()) { // 得到输入流 if (in == null) { try (PrintWriter printWriter = res.getWriter()) { printWriter.append("404 - File Not Exist"); } catch (IOException e) { logger.error("数据异常: {}", e); } return; } res.reset(); // res.setContentType(getMimeType(fileName)); // https://gitee.com/dolyw/codes/2h1r6avwxumegjs89ztyn86 res.addHeader("content-Disposition", "inline;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8")); byte[] b = new byte[4096]; int i = 0; while ((i = in.read(b)) > 0) { output.write(b, 0, i); } } catch (MalformedURLException me) { logger.error("数据异常: {}", me); } catch (IOException e) { logger.error("数据异常: {}", e); } } /** * 获取外链 * * @param objectName * @return java.lang.String * @throws * @author wliduo[i@dolyw.com] * @date 2020/2/6 16:04 */ public String getFileUrl(String objectName) { try { return minioClient.presignedGetObject(bucket, objectName, 60 * 60 * 24); } catch (Exception e) { logger.error("{}文件获取失败", objectName); return ""; } } }
老版本和新版本的用法可能有差异,上一章用的minio3.0的,写法好像和这不一样可以研究下。
引用这位大佬的:
https://blog.csdn.net/wang926454/article/details/106410669/
zzzzzzzzzzzz