此版本为2.0.0,采用spring框架,把MinioFileUtil组件化,配置更加灵活

1.0.0版本链接https://www.yuque.com/r/doc_versions/816772691

结构

  • config

    1. - MinioConfig
  • util

    1. - MinioFileUtil

代码

MinioConfig

  1. package com.jili.report.util.file.config;
  2. import io.minio.MinioClient;
  3. import lombok.Data;
  4. import lombok.SneakyThrows;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. /**
  9. * TODO
  10. *
  11. * @author baisongling
  12. * @version 1.0
  13. * @date 2021/6/15 下午5:32
  14. */
  15. @Data
  16. @Configuration
  17. @ConfigurationProperties(prefix = "minio")
  18. public class MinioConfig {
  19. private String minioUrl;
  20. private String minioAccessKey;
  21. private String minioSecretKey;
  22. @Bean
  23. @SneakyThrows(Exception.class)
  24. public MinioClient initMinioClient(){
  25. MinioClient minioClient = MinioClient.builder().endpoint(minioUrl).credentials(minioAccessKey,minioSecretKey).build();
  26. return minioClient;
  27. }
  28. }

配置Minio基础信息

MinioFileUtil

  1. package com.jili.report.util.file.util;
  2. import com.jili.report.util.file.config.MinioConfig;
  3. import io.minio.*;
  4. import io.minio.messages.Bucket;
  5. import lombok.SneakyThrows;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.http.HttpHeaders;
  8. import org.springframework.http.HttpStatus;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.InputStream;
  15. import java.net.URLEncoder;
  16. import java.util.List;
  17. /**
  18. * TODO
  19. *
  20. * @author liuyujie
  21. * @version 1.0
  22. * @date 2021/2/11 下午5:41
  23. */
  24. @Component
  25. public class MinioFileUtil {
  26. @Autowired
  27. private MinioConfig minioConfig;
  28. @Autowired
  29. private MinioClient minioClient;
  30. /**
  31. * 判断 bucket是否存在
  32. * @param bucketName
  33. * @return
  34. */
  35. @SneakyThrows(Exception.class)
  36. public boolean bucketExists(String bucketName) {
  37. return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
  38. }
  39. /**
  40. * 创建 bucket
  41. *
  42. * @param bucketName:
  43. * 桶名
  44. * @return: void
  45. */
  46. @SneakyThrows(Exception.class)
  47. private void createBucket(String bucketName) {
  48. boolean isExist = bucketExists(bucketName);
  49. if (!isExist) {
  50. minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
  51. String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::"+bucketName+"\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::"+bucketName+"/*\"]}]}";
  52. minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());
  53. }
  54. }
  55. /**
  56. * 获取全部bucket
  57. *
  58. * @param :
  59. * @return: java.util.List<io.minio.messages.Bucket>
  60. */
  61. @SneakyThrows(Exception.class)
  62. public List<Bucket> getAllBuckets() {
  63. return minioClient.listBuckets();
  64. }
  65. /**
  66. * 文件上传
  67. *
  68. * @param bucketName:
  69. * 桶名
  70. * @param fileName:
  71. * 文件名
  72. * @param filePath:
  73. * 文件路径
  74. * @return: void
  75. */
  76. @SneakyThrows(Exception.class)
  77. public String upload(String bucketName, String fileName, String filePath) {
  78. createBucket(bucketName);
  79. minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).object(fileName).filename(filePath).build());
  80. return getFileUrl(bucketName, fileName);
  81. }
  82. /**
  83. * 文件上传
  84. *
  85. * @param bucketName:
  86. * 桶名
  87. * @param fileName:
  88. * 文件名
  89. * @param stream:
  90. * 文件流
  91. * @return: java.lang.String : 文件url地址
  92. */
  93. @SneakyThrows(Exception.class)
  94. public String upload(String bucketName, String fileName, InputStream stream) {
  95. createBucket(bucketName);
  96. minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(stream, stream.available(), -1).contentType("application/octet-stream").build());
  97. // minioClient.putObject(bucketName,fileName,stream,"application/octet-stream");
  98. return getFileUrl(bucketName, fileName);
  99. }
  100. /**
  101. * 文件上传
  102. *
  103. * @param bucketName:
  104. * 桶名
  105. * @param file:
  106. * 文件
  107. * @return: 文件url地址
  108. */
  109. @SneakyThrows(Exception.class)
  110. public String upload(String bucketName, MultipartFile file) {
  111. createBucket(bucketName);
  112. final InputStream is = file.getInputStream();
  113. final String originalFilename = file.getOriginalFilename();
  114. String fileName = bucketName + "_" +
  115. System.currentTimeMillis() +
  116. originalFilename.substring(originalFilename.lastIndexOf("."));
  117. String url = upload(bucketName,fileName,is);
  118. is.close();
  119. return url;
  120. }
  121. /**
  122. * fileName作为参数传进来,解耦,便于自定义路径以及URI
  123. * @param file
  124. * @param bucketName
  125. * @param fileName
  126. * @return
  127. */
  128. @SneakyThrows(Exception.class)
  129. public String upload(MultipartFile file, String bucketName, String fileName) {
  130. createBucket(bucketName);
  131. final InputStream is = file.getInputStream();
  132. String url = upload(bucketName,fileName,is);
  133. is.close();
  134. return url;
  135. }
  136. /**
  137. * 删除文件
  138. *
  139. * @param bucketName:
  140. * 桶名
  141. * @param fileName:
  142. * 文件名
  143. * @return: void
  144. */
  145. @SneakyThrows(Exception.class)
  146. public void deleteFile(String bucketName, String fileName) {
  147. minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build());
  148. }
  149. /**
  150. * 获取文件
  151. * @param bucketName
  152. * @param fileName
  153. * @return
  154. */
  155. @SneakyThrows(Exception.class)
  156. private InputStream getObject(String bucketName, String fileName){
  157. return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
  158. }
  159. /**
  160. * 下载文件
  161. *
  162. * @param bucketName:
  163. * 桶名
  164. * @param fileName:
  165. * 文件名
  166. * @param response:
  167. * @return: void
  168. */
  169. @SneakyThrows(Exception.class)
  170. public ResponseEntity download(String bucketName, String fileName, HttpServletResponse response) {
  171. ResponseEntity<byte[]> responseEntity = null;
  172. InputStream stream = null;
  173. ByteArrayOutputStream output = null;
  174. try {
  175. stream = getObject(bucketName, fileName);
  176. if (stream == null) {
  177. System.out.println("文件不存在");
  178. }
  179. //用于转换byte
  180. output = new ByteArrayOutputStream();
  181. byte[] buffer = new byte[4096];
  182. int n = 0;
  183. while (-1 != (n = stream.read(buffer))) {
  184. output.write(buffer, 0, n);
  185. }
  186. byte[] bytes = output.toByteArray();
  187. //设置header
  188. HttpHeaders httpHeaders = new HttpHeaders();
  189. httpHeaders.add("Accept-Ranges", "bytes");
  190. httpHeaders.add("Content-Length", bytes.length + "");
  191. httpHeaders.add("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
  192. httpHeaders.add("Content-Type", "text/plain;charset=utf-8");
  193. responseEntity = new ResponseEntity<byte[]>(bytes, httpHeaders, HttpStatus.CREATED);
  194. } catch (Exception e) {
  195. e.printStackTrace();
  196. } finally {
  197. if (stream != null) {
  198. stream.close();
  199. }
  200. if (output != null) {
  201. output.close();
  202. }
  203. }
  204. return responseEntity;
  205. }
  206. /**
  207. * 获取minio文件的下载地址
  208. *
  209. * @param bucketName:
  210. * 桶名
  211. * @param fileName:
  212. * 完整文件名
  213. * @return: url
  214. */
  215. @SneakyThrows(Exception.class)
  216. public String getFileUrl(String bucketName, String fileName) {
  217. return minioConfig.getMinioUrl()+"/"+bucketName+"/"+fileName;
  218. }
  219. }

Minio文件管理实现方法

yml

  1. minio:
  2. minioUrl:
  3. minioAccessKey:
  4. minioSecretKey:

案例

  1. @PostMapping("/upload")
  2. public String upload(MultipartFile file){
  3. if (file.isEmpty() || file.getSize() == 0) {
  4. return "文件为空";
  5. }
  6. String fileName = file.getOriginalFilename();
  7. String newName = "img/"+ UUID.randomUUID().toString().replaceAll("-", "")+ fileName.substring(fileName.lastIndexOf("."));
  8. return minioFileUtil.upload(file,"test",newName);
  9. }