0


minio安装配置教程及整合springboot(史上最强保姆级教程---minio入门)

minio安装配置教程及整合springboot

1、进入minio官网

https://www.minio.org.cn/
点击下载(Download),选择自己需要的版本即可

2、选择放置minio文件路径

在此路径下,
新建文件夹minioData(与minio.exe处于同一路径)
在minio.exe文件夹的路径处输入cmd进入命令行界面(该exe文件不能双击运行)
输入命令:minio.exe server minioData全路径
例如:minio.exe server E:\software\minioData
后面路径为创建的minioData文件夹的路径
(如果上面命令执行的时候,命令框在疯狂跳动,就关闭该命令框,以管理员方式开启cmd窗口,进入自己的minio文件夹,执行上面的命令即可)

3、根据命令行提示访问minio面板

有多个url可以访问,但是其中固定的是127.0.0.1:9000
第一次访问,会进入登录页面,登录账户和密码均为minioadmin,
进入点击Create Bucket,bucket Name 建议设定为项目的名字,再点击Create Bucket即可,注意如果需要后期通过访问url预览,需要在新建的bucket Name点击Manage,点击AccessPolicy后面的icon(图标),将访问权限改为Public即可,在项目运行中必须开启minio服务,否则无法使用minion。

4、minio配置(yaml文件版)

  1. spring:
  2. # 配置文件上传大小限制(minio文件上传)
  3. servlet:
  4. multipart:
  5. max-file-size: 200MB
  6. max-request-size: 200MB
  7. minio:
  8. endpoint: http://127.0.0.1:9000
  9. accessKey: minioadmin
  10. secretKey: minioadmin
  11. bucketName: community-web

如果前面有spring配置,只需要将spring的配置minio文件限制复制贴贴到spring下即可。

5、编写minio的配置文件MinIoClientConfig

  1. package com.example.config;
  2. import io.minio.MinioClient;
  3. import lombok.Data;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.stereotype.Component;
  7. @Data
  8. @Component
  9. public class MinIoClientConfig {
  10. @Value("${minio.endpoint}")
  11. private String endpoint;
  12. @Value("${minio.accessKey}")
  13. private String accessKey;
  14. @Value("${minio.secretKey}")
  15. private String secretKey;
  16. /**
  17. * 注入minio 客户端
  18. *
  19. * @return
  20. */
  21. @Bean
  22. public MinioClient minioClient() {
  23. return MinioClient.builder()
  24. .endpoint(endpoint)
  25. .credentials(accessKey, secretKey)
  26. .build();
  27. }
  28. }

6、新建minio工具类需要的实体类ObjectItem

  1. package com.example.sys.entity;
  2. import lombok.Data;
  3. @Data
  4. public class ObjectItem {
  5. private String objectName;
  6. private Long size;
  7. }

7、编写minio的工具类MinioUtils

  1. package com.example.utils;
  2. import com.example.sys.entity.ObjectItem;
  3. import io.minio.*;
  4. import io.minio.messages.DeleteError;
  5. import io.minio.messages.DeleteObject;
  6. import io.minio.messages.Item;
  7. import org.apache.tomcat.util.http.fileupload.IOUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.http.HttpHeaders;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.http.MediaType;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.UnsupportedEncodingException;
  20. import java.net.URLEncoder;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.stream.Collectors;
  25. /**
  26. * @description: minio工具类
  27. * @version:1.0
  28. */
  29. @Component
  30. public class MinioUtils {
  31. @Autowired
  32. private MinioClient minioClient;
  33. @Value("${minio.bucketName}")
  34. private String bucketName;
  35. /**
  36. * description: 判断bucket是否存在,不存在则创建
  37. *
  38. * @return: void
  39. */
  40. public void existBucket(String name) {
  41. try {
  42. boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());
  43. if (!exists) {
  44. minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build());
  45. }
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. /**
  51. * 创建存储bucket
  52. *
  53. * @param bucketName 存储bucket名称
  54. * @return Boolean
  55. */
  56. public Boolean makeBucket(String bucketName) {
  57. try {
  58. minioClient.makeBucket(MakeBucketArgs.builder()
  59. .bucket(bucketName)
  60. .build());
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. return false;
  64. }
  65. return true;
  66. }
  67. /**
  68. * 删除存储bucket
  69. *
  70. * @param bucketName 存储bucket名称
  71. * @return Boolean
  72. */
  73. public Boolean removeBucket(String bucketName) {
  74. try {
  75. minioClient.removeBucket(RemoveBucketArgs.builder()
  76. .bucket(bucketName)
  77. .build());
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * description: 上传文件
  86. *
  87. * @param multipartFile
  88. * @return: java.lang.String
  89. */
  90. public List<String> upload(MultipartFile[] multipartFile) {
  91. List<String> names = new ArrayList<>(multipartFile.length);
  92. for (MultipartFile file : multipartFile) {
  93. String fileName = file.getOriginalFilename();
  94. String[] split = fileName.split("\\.");
  95. if (split.length > 1) {
  96. fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
  97. } else {
  98. fileName = fileName + System.currentTimeMillis();
  99. }
  100. InputStream in = null;
  101. try {
  102. in = file.getInputStream();
  103. minioClient.putObject(PutObjectArgs.builder()
  104. .bucket(bucketName)
  105. .object(fileName)
  106. .stream(in, in.available(), -1)
  107. .contentType(file.getContentType())
  108. .build()
  109. );
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. } finally {
  113. if (in != null) {
  114. try {
  115. in.close();
  116. } catch (IOException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. }
  121. names.add(fileName);
  122. }
  123. return names;
  124. }
  125. /**
  126. * description: 下载文件
  127. *
  128. * @param fileName
  129. * @return: org.springframework.http.ResponseEntity<byte [ ]>
  130. */
  131. public ResponseEntity<byte[]> download(String fileName) {
  132. ResponseEntity<byte[]> responseEntity = null;
  133. InputStream in = null;
  134. ByteArrayOutputStream out = null;
  135. try {
  136. in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
  137. out = new ByteArrayOutputStream();
  138. IOUtils.copy(in, out);
  139. //封装返回值
  140. byte[] bytes = out.toByteArray();
  141. HttpHeaders headers = new HttpHeaders();
  142. try {
  143. headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  144. } catch (UnsupportedEncodingException e) {
  145. e.printStackTrace();
  146. }
  147. headers.setContentLength(bytes.length);
  148. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  149. headers.setAccessControlExposeHeaders(Arrays.asList("*"));
  150. responseEntity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. } finally {
  154. try {
  155. if (in != null) {
  156. try {
  157. in.close();
  158. } catch (IOException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. if (out != null) {
  163. out.close();
  164. }
  165. } catch (IOException e) {
  166. e.printStackTrace();
  167. }
  168. }
  169. return responseEntity;
  170. }
  171. /**
  172. * 查看文件对象
  173. *
  174. * @param bucketName 存储bucket名称
  175. * @return 存储bucket内文件对象信息
  176. */
  177. public List<ObjectItem> listObjects(String bucketName) {
  178. Iterable<Result<Item>> results = minioClient.listObjects(
  179. ListObjectsArgs.builder().bucket(bucketName).build());
  180. List<ObjectItem> objectItems = new ArrayList<>();
  181. try {
  182. for (Result<Item> result : results) {
  183. Item item = result.get();
  184. ObjectItem objectItem = new ObjectItem();
  185. objectItem.setObjectName(item.objectName());
  186. objectItem.setSize(item.size());
  187. objectItems.add(objectItem);
  188. }
  189. } catch (Exception e) {
  190. e.printStackTrace();
  191. return null;
  192. }
  193. return objectItems;
  194. }
  195. /**
  196. * 批量删除文件对象
  197. *
  198. * @param bucketName 存储bucket名称
  199. * @param objects 对象名称集合
  200. */
  201. public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
  202. List<DeleteObject> dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
  203. Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
  204. return results;
  205. }
  206. }

8、编写minio的Controller层MinioController

  1. package com.example.sys.controller;
  2. import com.example.utils.MinioUtils;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import java.util.List;
  10. @RestController
  11. @Slf4j
  12. public class MinioController {
  13. @Autowired
  14. private MinioUtils minioUtils;
  15. @Value("${minio.endpoint}")
  16. private String address;
  17. @Value("${minio.bucketName}")
  18. private String bucketName;
  19. @PostMapping("/upload")
  20. public Object upload(MultipartFile file) {
  21. List<String> upload = minioUtils.upload(new MultipartFile[]{file});
  22. return address + "/" + bucketName + "/" + upload.get(0);
  23. }
  24. }

9.通过接口测试工具测试

1)新建一个请求(Request)
2)请求方式修改为post
3)请求的url,项目访问路径/upload
例如:http://127.0.0.1:8080/upload
4)选择Body
5)选择Form-Data
6)填写key和value
第一个键值
key—>file
参数类型为file
value—>点击Select Files按钮,选择文件(建议图片)
第二个键值
key—>bucketName
参数类型:选择String
value—>个人设置的桶(bucketName)的名称
最后点击send,发送即可
通过响应的url,复制到浏览器,打开即可,一般打开即可预览到图片,有些电脑会直接下载图片。

标签: spring boot spring java

本文转载自: https://blog.csdn.net/qq_51073233/article/details/127673489
版权归原作者 月熙_ 所有, 如有侵权,请联系我们删除。

“minio安装配置教程及整合springboot(史上最强保姆级教程---minio入门)”的评论:

还没有评论