• java集成minio文件系统


    MinIO 是高性能的对象存储,是为海量数据存储、人工智能、大数据分析而设计的,它完全兼容Amazon S3接口,单个对象最大可达5TB,适合存储海量图片、视频、日志文件、备份数据和容器/虚拟机镜像等。MinIO主要采用Golang语言实现,,客户端与存储服务器之间采用http/https通信协议。 

    上文讲解了minio如何部署

    linux部署minio文件系统_一朵纯洁的小白花的博客-CSDN博客MinIO 是高性能的对象存储,是为海量数据存储、、大数据分析而的,它完全兼容Amazon接口,单个对象最大可达5TB,适合存储海量图片、视频、日志文件、备份数据和容器/虚拟机镜像等。MinIO主要采语言实现,,客户端与存储服务器之间采用通信协议。以下为单机部署。......https://blog.csdn.net/TurboAnho/article/details/126289205

    本文演示如何集成程序

    一、配置yml文件

    1. #yml配置文件增加如下配置
    2. minio:
    3. endpoint: http://ip:9000
    4. fileUploadUrl: http://ip:9000
    5. accessKey: minioadmin (用户名)
    6. secretKey: minioadmin (密码)
    7. bucketName: file (文件组:桶名)

    二、引入pom(8与7的方法不适用,以下使用8.0.3)

    1. io.minio
    2. minio
    3. 8.0.3

    三、系统加载minio配置

    1. @Data
    2. @Configuration
    3. @ConfigurationProperties(prefix = "minio")
    4. public class MinioConfig {
    5. private String endpoint;
    6. private String accessKey;
    7. private String secretKey;
    8. private String bucketName;
    9. @Bean
    10. public MinioClient minioClient() {
    11. MinioClient minioClient = MinioClient.builder()
    12. .endpoint(endpoint)
    13. .credentials(accessKey, secretKey)
    14. .build();
    15. return minioClient;
    16. }
    17. }

    四、minio工具类(上传文件,返回访问链接)

    1. package org;
    2. import cn.hutool.core.io.FastByteArrayOutputStream;
    3. import com.alibaba.fastjson.JSONObject;
    4. import io.minio.*;
    5. import io.minio.http.Method;
    6. import io.minio.messages.Bucket;
    7. import io.minio.messages.DeleteError;
    8. import io.minio.messages.DeleteObject;
    9. import io.minio.messages.Item;
    10. import org.springframework.stereotype.Component;
    11. import org.springframework.web.multipart.MultipartFile;
    12. import javax.annotation.Resource;
    13. import javax.servlet.ServletOutputStream;
    14. import javax.servlet.http.HttpServletResponse;
    15. import java.io.InputStream;
    16. import java.util.ArrayList;
    17. import java.util.HashMap;
    18. import java.util.List;
    19. import java.util.UUID;
    20. import java.util.stream.Collectors;
    21. /**
    22. * 功能描述 文件服务器工具类
    23. *
    24. * @author:
    25. * @date:
    26. */
    27. @Component
    28. public class MinioUtil {
    29. @Resource
    30. private MinioClient minioClient;
    31. /**
    32. * 查看存储bucket是否存在
    33. * @return boolean
    34. */
    35. public Boolean bucketExists(String bucketName) {
    36. Boolean found;
    37. try {
    38. found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    39. } catch (Exception e) {
    40. //e.printStackTrace();
    41. return false;
    42. }
    43. return found;
    44. }
    45. /**
    46. * 创建存储bucket
    47. * @return Boolean
    48. */
    49. public Boolean makeBucket(String bucketName) {
    50. try {
    51. minioClient.makeBucket(MakeBucketArgs.builder()
    52. .bucket(bucketName)
    53. .build());
    54. } catch (Exception e) {
    55. e.printStackTrace();
    56. return false;
    57. }
    58. return true;
    59. }
    60. /**
    61. * 删除存储bucket
    62. * @return Boolean
    63. */
    64. public Boolean removeBucket(String bucketName) {
    65. try {
    66. minioClient.removeBucket(RemoveBucketArgs.builder()
    67. .bucket(bucketName)
    68. .build());
    69. } catch (Exception e) {
    70. e.printStackTrace();
    71. return false;
    72. }
    73. return true;
    74. }
    75. /**
    76. * 获取全部bucket
    77. */
    78. public List getAllBuckets() {
    79. try {
    80. return minioClient.listBuckets();
    81. } catch (Exception e) {
    82. e.printStackTrace();
    83. }
    84. return null;
    85. }
    86. /**
    87. * 文件上传
    88. * @param file 文件
    89. * @return Boolean
    90. */
    91. public Boolean upload(String bucketName, String fileName, MultipartFile file, InputStream inputStream) {
    92. try {
    93. PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName).object(fileName)
    94. .stream(inputStream,file.getSize(),-1).contentType(file.getContentType()).build();
    95. //文件名称相同会覆盖
    96. minioClient.putObject(objectArgs);
    97. } catch (Exception e) {
    98. e.printStackTrace();
    99. return false;
    100. }
    101. return true;
    102. }
    103. /**
    104. * 预览图片
    105. * @param fileName
    106. * @return
    107. */
    108. public String preview(String fileName,String bucketName){
    109. // 查看文件地址
    110. GetPresignedObjectUrlArgs build = new GetPresignedObjectUrlArgs().builder().bucket(bucketName).object(fileName).method(Method.GET).build();
    111. try {
    112. String url = minioClient.getPresignedObjectUrl(build);
    113. return url;
    114. } catch (Exception e) {
    115. e.printStackTrace();
    116. }
    117. return null;
    118. }
    119. /**
    120. * 文件下载
    121. * @param fileName 文件名称
    122. * @param res response
    123. * @return Boolean
    124. */
    125. public void download(String fileName,String bucketName, HttpServletResponse res) {
    126. GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
    127. .object(fileName).build();
    128. try (GetObjectResponse response = minioClient.getObject(objectArgs)){
    129. byte[] buf = new byte[1024];
    130. int len;
    131. try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()){
    132. while ((len=response.read(buf))!=-1){
    133. os.write(buf,0,len);
    134. }
    135. os.flush();
    136. byte[] bytes = os.toByteArray();
    137. res.setCharacterEncoding("utf-8");
    138. //设置强制下载不打开
    139. //res.setContentType("application/force-download");
    140. res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
    141. try (ServletOutputStream stream = res.getOutputStream()){
    142. stream.write(bytes);
    143. stream.flush();
    144. }
    145. }
    146. } catch (Exception e) {
    147. e.printStackTrace();
    148. }
    149. }
    150. /**
    151. * 查看文件对象
    152. * @return 存储bucket内文件对象信息
    153. */
    154. public List listObjects(String bucketName) {
    155. Iterable> results = minioClient.listObjects(
    156. ListObjectsArgs.builder().bucket(bucketName).build());
    157. List items = new ArrayList<>();
    158. try {
    159. for (Result result : results) {
    160. items.add(result.get());
    161. }
    162. } catch (Exception e) {
    163. e.printStackTrace();
    164. return null;
    165. }
    166. return items;
    167. }
    168. /**
    169. * 删除
    170. * @param fileName
    171. * @return
    172. * @throws Exception
    173. */
    174. public boolean remove(String fileName,String bucketName){
    175. try {
    176. minioClient.removeObject( RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build());
    177. }catch (Exception e){
    178. return false;
    179. }
    180. return true;
    181. }
    182. /**
    183. * 批量删除文件对象(没测试)
    184. * @param objects 对象名称集合
    185. */
    186. public Iterable> removeObjects(List objects, String bucketName) {
    187. List dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
    188. Iterable> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
    189. return results;
    190. }
    191. //上传文件,返回路径
    192. public Object upload(HashMap paramMap) {
    193. MultipartFile file=(MultipartFile)paramMap.get("file");
    194. if (file.isEmpty() || file.getSize() == 0) {
    195. return "文件为空";
    196. }
    197. try {
    198. if (!this.bucketExists("file")) {
    199. this.makeBucket("file");
    200. }
    201. InputStream inputStream = file.getInputStream();
    202. String fileName = file.getOriginalFilename();
    203. String newName = UUID.randomUUID().toString().replaceAll("-", "")
    204. + fileName.substring(fileName.lastIndexOf("."));
    205. this.upload("file", newName,file, inputStream);
    206. inputStream.close();
    207. String fileUrl = this.preview(newName,"file");
    208. String fileUrlNew=fileUrl.substring(0, fileUrl.indexOf("?"));
    209. JSONObject jsonObject=new JSONObject();
    210. jsonObject.put("url",fileUrlNew);
    211. return jsonObject.toJSONString();
    212. } catch (Exception e) {
    213. e.printStackTrace();
    214. return "上传失败";
    215. }
    216. }
    217. }
  • 相关阅读:
    MyBatis--逆向工程
    算法通关村-----原来贪心如此简单
    docker-compose安装nginx
    GCC、g++、gcc的关系
    841软件工程专业综合相关代码题总结(2)—— 栈、队列相关
    MySQL库的操作『增删改查 ‖ 编码问题 ‖ 备份与恢复』
    Vue 响应式原理
    多地接连进入汛期,如何做好积水监测站的管理?
    AsyncLocal<T>在链路追踪中的应用
    【web渗透思路】敏感信息泄露(网站+用户+服务器)
  • 原文地址:https://blog.csdn.net/TurboAnho/article/details/126289726