MinIoUploadService.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package com.fdkankan.fusion.common.util;
  2. import com.aliyun.oss.OSSClient;
  3. import com.aliyun.oss.model.GetObjectRequest;
  4. import com.fdkankan.fusion.config.MinIOConfig;
  5. import com.fdkankan.fusion.response.FileInfoVo;
  6. import io.minio.*;
  7. import io.minio.messages.Item;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.util.ObjectUtils;
  11. import javax.annotation.PostConstruct;
  12. import javax.annotation.Resource;
  13. import java.io.InputStream;
  14. import java.time.ZonedDateTime;
  15. import java.util.Date;
  16. import java.util.List;
  17. @Slf4j
  18. @Service
  19. public class MinIoUploadService {
  20. @Resource
  21. private MinIOConfig minIOConfig;
  22. private MinioClient minioClient;
  23. private String endpoint;
  24. private String bucketName;
  25. private String accessKey;
  26. private String secretKey;
  27. private Integer imgSize;
  28. private Integer fileSize;
  29. private final String SEPARATOR = "/";
  30. @PostConstruct
  31. public void init() {
  32. this.endpoint = minIOConfig.getEndpoint();
  33. this.bucketName = minIOConfig.getBucketName();
  34. this.accessKey = minIOConfig.getAccessKey();
  35. this.secretKey = minIOConfig.getSecretKey();
  36. createMinioClient();
  37. }
  38. /**
  39. * 创建基于Java端的MinioClient
  40. */
  41. public void createMinioClient() {
  42. try {
  43. if (null == minioClient) {
  44. log.info("开始创建 MinioClient...");
  45. minioClient = MinioClient
  46. .builder()
  47. .endpoint(endpoint)
  48. .credentials(accessKey, secretKey)
  49. .build();
  50. createBucket(bucketName);
  51. log.info("创建完毕 MinioClient...");
  52. }
  53. } catch (Exception e) {
  54. log.error("MinIO服务器异常:{}", e);
  55. }
  56. }
  57. /**
  58. * 启动SpringBoot容器的时候初始化Bucket
  59. * 如果没有Bucket则创建
  60. *
  61. * @throws Exception
  62. */
  63. private void createBucket(String bucketName) throws Exception {
  64. if (!bucketExists(bucketName)) {
  65. minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
  66. }
  67. }
  68. /**
  69. * 判断Bucket是否存在,true:存在,false:不存在
  70. *
  71. * @return
  72. * @throws Exception
  73. */
  74. public boolean bucketExists(String bucketName) throws Exception {
  75. return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
  76. }
  77. /**
  78. * 获得Bucket的策略
  79. *
  80. * @param bucketName
  81. * @return
  82. * @throws Exception
  83. */
  84. public String getBucketPolicy(String bucketName) throws Exception {
  85. String bucketPolicy = minioClient
  86. .getBucketPolicy(
  87. GetBucketPolicyArgs
  88. .builder()
  89. .bucket(bucketName)
  90. .build()
  91. );
  92. return bucketPolicy;
  93. }
  94. /**
  95. * 判断文件是否存在
  96. *
  97. * @param bucketName 存储桶
  98. * @param objectName 文件名
  99. * @return
  100. */
  101. public boolean isObjectExist(String bucketName, String objectName) {
  102. boolean exist = true;
  103. try {
  104. minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
  105. } catch (Exception e) {
  106. exist = false;
  107. }
  108. return exist;
  109. }
  110. public boolean isObjectExist(String objectName) {
  111. return isObjectExist(bucketName,objectName);
  112. }
  113. /**
  114. * 判断文件夹是否存在
  115. *
  116. * @param bucketName 存储桶
  117. * @param objectName 文件夹名称
  118. * @return
  119. */
  120. public boolean isFolderExist(String bucketName, String objectName) {
  121. boolean exist = false;
  122. try {
  123. Iterable<Result<Item>> results = minioClient.listObjects(
  124. ListObjectsArgs.builder().bucket(bucketName).prefix(objectName).recursive(false).build());
  125. for (Result<Item> result : results) {
  126. Item item = result.get();
  127. if (item.isDir() && objectName.equals(item.objectName())) {
  128. exist = true;
  129. }
  130. }
  131. } catch (Exception e) {
  132. exist = false;
  133. }
  134. return exist;
  135. }
  136. public boolean isFolderExist( String objectName) {
  137. return isFolderExist(bucketName,objectName);
  138. }
  139. /**
  140. * 获取路径下文件列表
  141. *
  142. * @param bucketName 存储桶
  143. * @param prefix 文件名称
  144. * @param recursive 是否递归查找,false:模拟文件夹结构查找
  145. * @return 二进制流
  146. */
  147. public Iterable<Result<Item>> listObjects(String bucketName, String prefix,
  148. boolean recursive) {
  149. return minioClient.listObjects(
  150. ListObjectsArgs.builder()
  151. .bucket(bucketName)
  152. .prefix(prefix)
  153. .recursive(recursive)
  154. .build());
  155. }
  156. /**
  157. * 获取文件流
  158. *
  159. * @param bucketName 存储桶
  160. * @param objectName 文件名
  161. * @return 二进制流
  162. */
  163. public InputStream getObject(String bucketName, String objectName) throws Exception {
  164. return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
  165. }
  166. public InputStream getObject( String objectName) {
  167. try {
  168. return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
  169. }catch (Exception e){
  170. log.info("minio获取文件失败:{}",objectName,e);
  171. }
  172. return null;
  173. }
  174. /**
  175. * 获取文件信息, 如果抛出异常则说明文件不存在
  176. *
  177. * @param bucketName 存储桶
  178. * @param objectName 文件名称
  179. */
  180. public FileInfoVo getFileStatusInfo(String bucketName, String objectName) throws Exception {
  181. StatObjectResponse statObjectResponse = minioClient.statObject(
  182. StatObjectArgs.builder()
  183. .bucket(bucketName)
  184. .object(objectName)
  185. .build());
  186. String md5 = statObjectResponse.etag().toUpperCase();
  187. ZonedDateTime zonedDateTime = statObjectResponse.lastModified();
  188. Date LastMo = Date.from(zonedDateTime.toInstant());
  189. Long size = statObjectResponse.size();
  190. InputStream inputStream = getObject(bucketName, objectName);
  191. String sha1 = MD5Checksum.getSHA1(inputStream);
  192. return new FileInfoVo(md5,sha1.toUpperCase(),LastMo.getTime(),size);
  193. }
  194. public FileInfoVo getFileStatusInfo( String objectName) {
  195. try {
  196. return getFileStatusInfo(bucketName,objectName);
  197. }catch (Exception e){
  198. log.info("获取文件失败或文件不存在:{},{}",bucketName,objectName,e);
  199. }
  200. return null;
  201. }
  202. public Long getSize(String filePath){
  203. Long total = 0L;
  204. try {
  205. Iterable<Result<Item>> results = listObjects(bucketName, filePath, true);
  206. if (ObjectUtils.isEmpty(results)) {
  207. return 0L;
  208. }
  209. for (Result<Item> result : results) {
  210. Item item = result.get();
  211. long size = item.size();
  212. total += size;
  213. }
  214. }catch (Exception e){
  215. log.info("oss-getFileInfo-error:{}",e);
  216. }
  217. return total;
  218. }
  219. }