当前位置:   article > 正文

若依前后端分离框架(Ruoyi-Vue)集成Minio实现上传和下载_若依minio

若依minio

若依提供的上传和下载组件有些开发者想将其更换成Minio, 我集成后在此文做个记录。

1、简单了解Minion

Minio是一款开源的对象存储服务器,它与云存储服务Amazon S3兼容,能够提供高性能、高可用性、分布式的文件存储服务。Minio适合用于存储、备份、存档、大数据的分布式存储等场景。Minio使用Go语言编写,具有轻量级、可扩展、易于部署的特点。并且可以通过API和命令行进行管理和访问。搭建Minio服务器的方法网上很多,这里就不作阐述,直接从整合开始。

2、在ruoyi-admin模块pom.xml引入Minio的依赖
  1. <dependency>
  2. <groupId>io.minio</groupId>
  3. <artifactId>minio</artifactId>
  4. <version>7.1.0</version>
  5. </dependency>
3、在yml里增加Minio的账号信息配置
  1. #Minio
  2. minio:
  3. url: 服务器地址
  4. access-key: 账号
  5. secret-key: 密码
  6. bucket-name: 桶名字
4、读取配置,初始化Minio客户端
  1. /**
  2. * 创建Minio客户端
  3. */
  4. @Configuration
  5. public class MinioConfig {
  6. @Value("${minio.accessKey}")
  7. private String accessKey;
  8. @Value("${minio.secretKey}")
  9. private String secretKey;
  10. @Value("${minio.url}")
  11. private String url;
  12. /**
  13. * 注入客户端
  14. */
  15. @Bean
  16. public MinioClient minioClient() {
  17. return MinioClient.builder()
  18. .credentials("accessKey", "secretKey")
  19. .endpoint(url)
  20. .build();
  21. }
  22. }
5、引入Minio工具类
  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. @Component
  5. public class MinioTemplate {
  6. @Value("${minio.bucketName}")
  7. public String bucketName;
  8. @Autowired
  9. private MinioClient minioClient;
  10. /**
  11. * 判断桶是否存在,如果存在返回true,如果不存在返回false
  12. *
  13. * @param bucketName
  14. * @return
  15. */
  16. @SneakyThrows
  17. public Boolean existBucket(String bucketName) {
  18. boolean exist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
  19. if (!exist) {
  20. return false;
  21. }
  22. return true;
  23. }
  24. /**
  25. * 创建桶
  26. *
  27. * @param bucketName
  28. * @return
  29. */
  30. @SneakyThrows
  31. public void makeBucket(String bucketName) {
  32. minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
  33. }
  34. /**
  35. * 上传对象
  36. *
  37. * @param objectName
  38. * @param file
  39. * @return
  40. * @throws Exception
  41. */
  42. @SneakyThrows
  43. public void putObject(String objectName, MultipartFile file) {
  44. //判断 桶是否存在
  45. boolean flag = existBucket(bucketName);
  46. if (flag) {
  47. PutObjectArgs args = PutObjectArgs.builder()
  48. .bucket(bucketName)
  49. .object(objectName)
  50. .contentType(file.getContentType())
  51. .stream(file.getInputStream(), file.getSize(), -1)
  52. .build();
  53. minioClient.putObject(args);
  54. }
  55. }
  56. /**
  57. * 删除对象
  58. *
  59. * @param objectName
  60. * @return
  61. * @throws Exception
  62. */
  63. @SneakyThrows
  64. public boolean removeObject(String objectName) {
  65. boolean flag = existBucket(bucketName);
  66. if (flag) {
  67. RemoveObjectArgs args = RemoveObjectArgs.builder()
  68. .bucket(bucketName)
  69. .object(objectName)
  70. .build();
  71. minioClient.removeObject(args);
  72. return true;
  73. }
  74. return false;
  75. }
  76. /**
  77. * 获取对象信息
  78. *
  79. * @param objectName
  80. * @return
  81. * @throws Exception
  82. */
  83. @SneakyThrows
  84. public ObjectStat getMessage(String objectName) {
  85. boolean flag = existBucket(bucketName);
  86. if (flag) {
  87. ObjectStat statObject = minioClient.statObject(StatObjectArgs.builder()
  88. .bucket(bucketName)
  89. .object(objectName)
  90. .build());
  91. return statObject;
  92. }
  93. return null;
  94. }
  95. /**
  96. * 返回的文件路径,不会过期.
  97. *
  98. * @param objectName
  99. * @return
  100. */
  101. @SneakyThrows
  102. public String getObjectUrl(String objectName) {
  103. Boolean flag = existBucket(bucketName);
  104. String url = "";
  105. if (flag) {
  106. url = minioClient.getObjectUrl(bucketName, objectName);
  107. }
  108. return url;
  109. }
  110. /**
  111. * 下载
  112. *
  113. * @param filename
  114. * @param response
  115. */
  116. public void getObject(String filename, HttpServletResponse response) {
  117. InputStream in = null;
  118. OutputStream out = null;
  119. try {
  120. in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(filename).build());
  121. int length = 0;
  122. byte[] buffer = new byte[1024];
  123. out = response.getOutputStream();
  124. response.reset();
  125. response.addHeader("Content-Disposition",
  126. " attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
  127. response.setContentType("application/octet-stream");
  128. while ((length = in.read(buffer)) > 0) {
  129. out.write(buffer, 0, length);
  130. }
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. } finally {
  134. if (in != null) {
  135. try {
  136. in.close();
  137. } catch (Exception e) {
  138. throw new RuntimeException(e);
  139. }
  140. }
  141. if (out != null) {
  142. try {
  143. out.close();
  144. } catch (IOException e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. }
  149. }
  150. }
6、进行测试
  1. @PostMapping("/minio/upload")
  2. public AjaxResult minioUploadFile(@RequestPart MultipartFile file) throws Exception {
  3. try {
  4. System.out.println("文件正在上传");
  5. String fileName = file.getOriginalFilename();
  6. String uuid = UUID.randomUUID().toString();
  7. String imgType = fileName.substring(fileName.lastIndexOf("."));
  8. String finalFileName = "images/"+uuid + imgType;
  9. minioTemplate.putObject(finalFileName,file);
  10. String path = "/test/" + finalFileName;
  11. AjaxResult ajax = AjaxResult.success();
  12. ajax.put("path", path);
  13. return ajax;
  14. } catch (Exception e) {
  15. return AjaxResult.error(e.getMessage());
  16. }
  17. }

其它测试根据需求自己写

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/594990
推荐阅读
  

闽ICP备14008679号