1. package com.soyuan.mh.srs.util;
    2. import com.google.common.base.Stopwatch;
    3. import com.google.common.util.concurrent.ThreadFactoryBuilder;
    4. import com.soyuan.mh.srs.config.child.MinIoConfig;
    5. import io.minio.*;
    6. import io.minio.errors.*;
    7. import java.io.*;
    8. import java.security.InvalidKeyException;
    9. import java.security.NoSuchAlgorithmException;
    10. import java.time.Duration;
    11. import java.util.ArrayList;
    12. import java.util.List;
    13. import java.util.concurrent.*;
    14. import java.util.function.Consumer;
    15. /**
    16. * @author tangwx@soyuan.com.cn
    17. * @date 2021/4/2 上午11:46
    18. */
    19. public class MinIoUtil {
    20. /**
    21. * 用于上传视频
    22. */
    23. private static final ExecutorService es = Executors.newFixedThreadPool(5,
    24. new ThreadFactoryBuilder().setNameFormat("mh.srs.upload-minio-%d").build());
    25. private MinioClient client;
    26. private String bucketName;
    27. public MinIoUtil(MinIoConfig config) {
    28. this(config.getEndPoint(), config.getAccessKey(), config.getSecretKey(),config.getBucketName());
    29. }
    30. public MinIoUtil(String endPoint, String accessKey, String secretKey,String bucketName) {
    31. this.bucketName=bucketName;
    32. this.client = MinioClient.builder()
    33. .endpoint(endPoint)
    34. .credentials(accessKey, secretKey)
    35. .build();
    36. }
    37. /**
    38. * 初始化创建桶和子文件夹
    39. */
    40. public void init() {
    41. createBucket();
    42. //makeDir("image/");
    43. //makeDir("video/");
    44. }
    45. private boolean createBucket() {
    46. try {
    47. boolean isExist = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    48. if (isExist) {
    49. return false;
    50. }else{
    51. client.makeBucket(MakeBucketArgs.builder()
    52. .bucket(bucketName)
    53. .objectLock(false)
    54. .build());
    55. return true;
    56. }
    57. } catch (ErrorResponseException e) {
    58. e.printStackTrace();
    59. } catch (InsufficientDataException e) {
    60. e.printStackTrace();
    61. } catch (InternalException e) {
    62. e.printStackTrace();
    63. } catch (InvalidBucketNameException e) {
    64. e.printStackTrace();
    65. } catch (InvalidKeyException e) {
    66. e.printStackTrace();
    67. } catch (InvalidResponseException e) {
    68. e.printStackTrace();
    69. } catch (IOException e) {
    70. e.printStackTrace();
    71. } catch (NoSuchAlgorithmException e) {
    72. e.printStackTrace();
    73. } catch (ServerException e) {
    74. e.printStackTrace();
    75. } catch (XmlParserException e) {
    76. e.printStackTrace();
    77. } catch (RegionConflictException e) {
    78. e.printStackTrace();
    79. }
    80. return false;
    81. }
    82. public boolean deleteBucket(String bucketName){
    83. try {
    84. client.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    85. return true;
    86. } catch (ErrorResponseException e) {
    87. e.printStackTrace();
    88. } catch (InsufficientDataException e) {
    89. e.printStackTrace();
    90. } catch (InternalException e) {
    91. e.printStackTrace();
    92. } catch (InvalidBucketNameException e) {
    93. e.printStackTrace();
    94. } catch (InvalidKeyException e) {
    95. e.printStackTrace();
    96. } catch (InvalidResponseException e) {
    97. e.printStackTrace();
    98. } catch (IOException e) {
    99. e.printStackTrace();
    100. } catch (NoSuchAlgorithmException e) {
    101. e.printStackTrace();
    102. } catch (ServerException e) {
    103. e.printStackTrace();
    104. } catch (XmlParserException e) {
    105. e.printStackTrace();
    106. }
    107. return false;
    108. }
    109. public boolean download(String bucketName, String objectName, String output) {
    110. final InputStream is = getObject(bucketName, objectName);
    111. if (is != null) {
    112. try {
    113. OutputStream os = new FileOutputStream(new File(output));
    114. byte[] arr = new byte[1024];
    115. int len;
    116. while ((len=is.read(arr))!=-1){
    117. os.write(arr,0,len);
    118. }
    119. os.close();
    120. is.close();
    121. } catch (FileNotFoundException e) {
    122. e.printStackTrace();
    123. } catch (IOException e) {
    124. e.printStackTrace();
    125. }
    126. return true;
    127. }
    128. return false;
    129. }
    130. public InputStream getObject(String bucketName,String objectName) {
    131. try {
    132. return client.getObject(GetObjectArgs.builder()
    133. .bucket(bucketName)
    134. .object(objectName)
    135. .build());
    136. } catch (ErrorResponseException e) {
    137. e.printStackTrace();
    138. } catch (InsufficientDataException e) {
    139. e.printStackTrace();
    140. } catch (InternalException e) {
    141. e.printStackTrace();
    142. } catch (InvalidBucketNameException e) {
    143. e.printStackTrace();
    144. } catch (InvalidKeyException e) {
    145. e.printStackTrace();
    146. } catch (InvalidResponseException e) {
    147. e.printStackTrace();
    148. } catch (IOException e) {
    149. e.printStackTrace();
    150. } catch (NoSuchAlgorithmException e) {
    151. e.printStackTrace();
    152. } catch (ServerException e) {
    153. e.printStackTrace();
    154. } catch (XmlParserException e) {
    155. e.printStackTrace();
    156. }
    157. return null;
    158. }
    159. /**
    160. * 上传jpg图片
    161. * @param imageName 图片名称
    162. * @param inputPath 图片路径
    163. * @return
    164. */
    165. public boolean putImage(String imageName, String inputPath) {
    166. try {
    167. InputStream ins = new FileInputStream(inputPath);
    168. return putObject(bucketName, imageName, ContentType.JPEG, ins);
    169. } catch (FileNotFoundException e) {
    170. e.printStackTrace();
    171. }
    172. return false;
    173. }
    174. /**
    175. * 异步上传MP4
    176. * @param videoName 视频名称
    177. * @param inputPath 视频路径
    178. * @param consumer 回调接口
    179. * @return
    180. */
    181. public void putVideoAsy(final String videoName, final String inputPath, Consumer<Boolean> consumer){
    182. es.submit(() -> {
    183. try {
    184. InputStream ins = new FileInputStream(inputPath);
    185. boolean res = putObject(bucketName, videoName, ContentType.MP4, ins);
    186. consumer.accept(res);
    187. } catch (FileNotFoundException e) {
    188. e.printStackTrace();
    189. }
    190. });
    191. }
    192. /**
    193. * 创建目录
    194. * @param dir
    195. */
    196. public void makeDir(String dir){
    197. try {
    198. client.putObject(PutObjectArgs.builder()
    199. .bucket(bucketName)
    200. .object(dir)
    201. .stream(new ByteArrayInputStream(new byte[] {}), 0, -1)
    202. .build());
    203. } catch (ErrorResponseException e) {
    204. e.printStackTrace();
    205. } catch (InsufficientDataException e) {
    206. e.printStackTrace();
    207. } catch (InternalException e) {
    208. e.printStackTrace();
    209. } catch (InvalidBucketNameException e) {
    210. e.printStackTrace();
    211. } catch (InvalidKeyException e) {
    212. e.printStackTrace();
    213. } catch (InvalidResponseException e) {
    214. e.printStackTrace();
    215. } catch (IOException e) {
    216. e.printStackTrace();
    217. } catch (NoSuchAlgorithmException e) {
    218. e.printStackTrace();
    219. } catch (ServerException e) {
    220. e.printStackTrace();
    221. } catch (XmlParserException e) {
    222. e.printStackTrace();
    223. }
    224. }
    225. private boolean putObject(String bucket,String objName,ContentType contentType,InputStream ins) {
    226. try {
    227. client.putObject(PutObjectArgs.builder()
    228. .bucket(bucket)
    229. .object(objName)
    230. .stream(ins, ins.available(), -1)
    231. .contentType(contentType.type)
    232. .build());
    233. return true;
    234. } catch (ErrorResponseException e) {
    235. e.printStackTrace();
    236. } catch (InsufficientDataException e) {
    237. e.printStackTrace();
    238. } catch (InternalException e) {
    239. e.printStackTrace();
    240. } catch (InvalidBucketNameException e) {
    241. e.printStackTrace();
    242. } catch (InvalidKeyException e) {
    243. e.printStackTrace();
    244. } catch (InvalidResponseException e) {
    245. e.printStackTrace();
    246. } catch (IOException e) {
    247. e.printStackTrace();
    248. } catch (NoSuchAlgorithmException e) {
    249. e.printStackTrace();
    250. } catch (ServerException e) {
    251. e.printStackTrace();
    252. } catch (XmlParserException e) {
    253. e.printStackTrace();
    254. }
    255. return false;
    256. }
    257. public enum ContentType {
    258. DEFAULT("application/octet-stream"),
    259. JPEG("image/jpeg"),
    260. PNG("image/png"),
    261. MP4("video/mp4"),
    262. CSV("text/csv");
    263. private String type;
    264. ContentType(String type) {
    265. this.type = type;
    266. }
    267. }
    268. }