2021与蓝度共同重构项目,服务端
liuhaonan
2022-04-08 df67e2b6894130662f0a3b42f4b965aedb90869e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package com.sandu.ximon.admin.minio.utils;
 
import com.alibaba.fastjson.JSONObject;
import com.sandu.ximon.admin.config.VnnoxConstant;
import com.sandu.ximon.admin.minio.config.MinIoPolicy;
import com.sandu.ximon.admin.minio.entity.MinIoConstant;
import com.sandu.ximon.admin.utils.LogUtils;
import com.sandu.ximon.admin.utils.SpringContextUtils;
 
 
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import io.minio.messages.DeleteError;
import io.minio.messages.Item;
import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
import lombok.SneakyThrows;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
 
/**
 * 调用本工具类请保证bucketName不为空,不可在根目录添加文件
 */
@Component("MinIoUtil")
public class MinIoUtil {
 
    @Autowired
    private MinioClient minioClient;
 
    //  URL过期时间
    private static final Integer DEFAULT_EXPIRY_TIME = 60 * 60 * 24;
 
 
    public static MinIoUtil getMinIOUtil() {
        return (MinIoUtil) SpringContextUtils.getBean("MinIoUtil");
    }
 
 
    /**
     * 获取存储桶策略
     *
     * @param bucketName 存储桶名称
     * @return json
     */
    private JSONObject getBucketPolicy(String bucketName)
            throws IOException, InvalidKeyException, InvalidResponseException, BucketPolicyTooLargeException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, io.minio.errors.InternalException {
        String bucketPolicy = minioClient
                .getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build());
        return JSONObject.parseObject(bucketPolicy);
    }
 
    /**
     * 设置开放*的桶策略
     *
     * @param bucketName
     * @return
     */
    public void setBucketPolicy(String bucketName) throws Exception {
        minioClient.setBucketPolicy(
                SetBucketPolicyArgs.builder().bucket(bucketName).config(MinIoPolicy.generateMinioPolicy(bucketName)).build()
        );
    }
 
 
    /**
     * 判断 bucket是否存在
     *
     * @param bucketName: 桶名
     * @return: boolean
     */
    @SneakyThrows(Exception.class)
    public boolean bucketExists(String bucketName) {
        return minioClient.bucketExists(bucketName);
    }
 
    public boolean objectExists(String bucketName, String objectName) {
        try {
            getObjectInfo(bucketName, objectName);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
 
    /**
     * 创建 bucket
     *
     * @param bucketName: 桶名
     * @return: void
     */
    @SneakyThrows(Exception.class)
    public void createBucket(String bucketName) {
        boolean isExist = minioClient.bucketExists((BucketExistsArgs) ((io.minio.BucketExistsArgs.Builder) BucketExistsArgs.builder().bucket(bucketName)).build());
        if (!isExist) {
            minioClient.makeBucket((MakeBucketArgs) ((io.minio.MakeBucketArgs.Builder) MakeBucketArgs.builder().bucket(bucketName)).build());
        }
    }
 
    /**
     * 获取全部bucket
     */
    @SneakyThrows(Exception.class)
    public List<Bucket> getAllBuckets() {
        return minioClient.listBuckets();
    }
 
    /**
     * 列出存储桶中的所有对象名称
     *
     * @param bucketName 存储桶名称
     * @return
     */
    @SneakyThrows
    public List<String> listObjectNames(String bucketName) {
        List<String> listObjectNames = new ArrayList<>();
        boolean flag = bucketExists(bucketName);
        if (flag) {
            Iterable<Result<Item>> myObjects = listObjects(bucketName);
            for (Result<Item> result : myObjects) {
                Item item = result.get();
                listObjectNames.add(item.objectName());
            }
        }
        return listObjectNames;
    }
 
    /**
     * 列出存储桶中的所有对象
     *
     * @param bucketName 存储桶名称
     * @return
     */
    @SneakyThrows
    public Iterable<Result<Item>> listObjects(String bucketName) {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            return minioClient.listObjects(bucketName);
        }
        return null;
    }
 
 
    /**
     * 文件上传
     *
     * @param bucketName: 桶名
     * @param fileName:   文件名
     * @param filePath:   文件路径
     * @return: void
     */
    @SneakyThrows(Exception.class)
    public void uploadToMinIo(String bucketName, String fileName, String filePath) {
        if (!bucketExists(bucketName)) {
            createBucket(bucketName);
 
            LogUtils.print("创建桶:" + bucketName);
 
        }
//        log.log(new LogRecordEntity(
//                "服务器文件上传:(bucket)"+bucketName +", (fileName)"+fileName+",(filePath)"+filePath,
//                LogType.MINIO_SERVER,
//                getNUserId(),
//                this.getClass()
//        ));
        try {
            //  判断文件是否存在
            if (!objectExists(bucketName, fileName)) {
                minioClient.putObject(bucketName, fileName, filePath, null);
            } else {
//                log.log(new LogRecordEntity(
//                        "服务器文件已存在,请勿重复上传:(bucket)"+bucketName +", (fileName)"+fileName,
//                        LogType.MINIO_SERVER,
//                        getNUserId(),
//                        this.getClass()
//                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 文件上传
     *
     * @param bucketName: 桶名
     * @param fileName:   文件名
     * @param stream:     文件流
     * @return: 文件url地址
     */
    public String upload(String bucketName, String fileName, InputStream stream) {
        if (!bucketExists(bucketName)) {
            createBucket(bucketName);
 
            LogUtils.print("创建桶:" + bucketName);
 
        }
 
        try {
            //  判断文件是否存在
            if (!objectExists(bucketName, fileName)) {
                minioClient.putObject(bucketName, fileName, stream, new PutObjectOptions(stream.available(), -1));
            } else {
 
                return null;
            }
        } catch (Exception e) {
            return null;
        }
        return getFileUrl(bucketName, fileName);
    }
 
    /**
     * 列出所有存储桶名称
     *
     * @return
     */
    @SneakyThrows
    public List<String> listBucketNames() {
        List<Bucket> bucketList = getAllBuckets();
        List<String> bucketListName = new ArrayList<>();
        for (Bucket bucket : bucketList) {
            bucketListName.add(bucket.name());
        }
        return bucketListName;
    }
 
 
    /**
     * 文件上传
     *
     * @param bucketName: 桶名
     * @param file:       文件
     * @return: 文件url地址
     */
    @SneakyThrows(Exception.class)
    public String upload(String bucketName, MultipartFile file) {
 
        if (!bucketExists(bucketName)) {
            createBucket(bucketName);
            LogUtils.print("创建桶:" + bucketName);
            if (bucketName.indexOf(VnnoxConstant.MINIO_PREFIX) != -1) {
                setBucketPolicy(bucketName);
            }
        }
 
        final InputStream is = file.getInputStream();
        final String fileName = file.getOriginalFilename();
        Long start = System.currentTimeMillis();
        minioClient.putObject(bucketName, fileName, is, new PutObjectOptions(is.available(), -1));
        is.close();
        LogUtils.error("上传总时间:" + (System.currentTimeMillis() - start) + "ms");
        return getFileUrl(bucketName, fileName);
    }
 
    /**
     * 删除文件
     *
     * @param bucketName: 桶名
     * @param fileName:   文件名
     * @return: void
     * @date : 2020/8/16 20:53
     */
    @SneakyThrows(Exception.class)
    public boolean deleteFile(String bucketName, String fileName) {
        //  判断文件是否存在
        if (objectExists(bucketName, fileName)) {
//            log.log(new LogRecordEntity(
//                    "服务器文件删除:(bucket)"+bucketName +", (fileName)"+fileName,
//                    LogType.MINIO_SERVER,
//                    getNUserId(),
//                    this.getClass()
//            ));
            minioClient.removeObject(bucketName, fileName);
            if (!objectExists(bucketName, fileName)) {
                return true;
            } else {
                return false;
            }
        } else {
//            log.log(new LogRecordEntity(
//                    "服务器文件不存在,无法删除:(bucket)"+bucketName +", (fileName)"+fileName,
//                    LogType.MINIO_SERVER,
//                    getNUserId(),
//                    this.getClass()
//            ));
            return false;
        }
 
    }
 
    /**
     * 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
     *
     * @param bucketName  存储桶名称
     * @param objectNames 含有要删除的多个object名称的迭代器对象
     * @return
     */
    @SneakyThrows
    public List<String> removeObject(String bucketName, List<String> objectNames) {
        List<String> deleteErrorNames = new ArrayList<>();
        boolean flag = bucketExists(bucketName);
        if (flag) {
            Iterable<Result<DeleteError>> results = minioClient.removeObjects(bucketName, objectNames);
            for (Result<DeleteError> result : results) {
                DeleteError error = result.get();
                deleteErrorNames.add(error.objectName());
            }
        }
//        log.log(new LogRecordEntity(
//                "服务器文件批量删除:(bucket)"+bucketName +", (fileName)"+ JSON.toJSONString(objectNames)+", 删除失败文件:"+JSON.toJSONString(deleteErrorNames),
//                LogType.MINIO_SERVER,
//                getNUserId(),
//                this.getClass()
//        ));
        return deleteErrorNames;
    }
 
 
    /**
     * 下载文件
     *
     * @param bucketName: 桶名
     * @param fileName:   文件名
     * @param response:
     * @return: void
     * @date : 2020/8/17 0:34
     */
    @SneakyThrows(Exception.class)
    public HttpServletResponse download(String bucketName, String fileName, HttpServletResponse response) {
        // 获取对象的元数据
        final ObjectStat stat = minioClient.statObject(bucketName, fileName);
        response.setContentType(stat.contentType());
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        InputStream is = minioClient.getObject(bucketName, fileName);
        IOUtils.copy(is, response.getOutputStream());
        is.close();
        return response;
    }
 
    /**
     * 获取MinIo文件的永久下载地址
     *
     * @param bucketName: 桶名
     * @param fileName:   文件名
     * @return:
     */
    @SneakyThrows(Exception.class)
    public String getFileUrl(String bucketName, String fileName) {
//        log.log(new LogRecordEntity(
//                "服务器文件获取永久下载地址:(bucketName)"+bucketName+", (fileName)"+fileName,
//                LogType.MINIO_SERVER,
//                getNUserId(),
//                this.getClass()
//        ));
        return minioClient.presignedGetObject(bucketName, fileName);
    }
 
 
    /**
     * 生成一个给HTTP GET请求用的presigned URL。
     * 浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param expires    失效时间(以秒为单位
     * @return
     */
    @SneakyThrows
    public String presignedGetObject(String bucketName, String objectName, Integer expires) {
        boolean flag = bucketExists(bucketName);
        String url = "";
        if (flag) {
            if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
                throw new InvalidExpiresRangeException(expires,
                        "expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
            }
            url = minioClient.presignedGetObject(bucketName, objectName, expires);
        }
//        log.log(new LogRecordEntity(
//                "服务器文件获取文件定时下载地址(get:"+expires+"s):(bucketName)"+bucketName+", (fileName)"+objectName,
//                LogType.MINIO_SERVER,
//                getNUserId(),
//                this.getClass()
//        ));
        return url;
    }
 
    /**
     * 生成一个给HTTP PUT请求用的presigned URL。
     * 浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param expires    失效时间(以秒为单位
     * @return
     */
    @SneakyThrows
    public String presignedPutObject(String bucketName, String objectName, Integer expires) {
        boolean flag = bucketExists(bucketName);
        String url = "";
        if (flag) {
            if (expires < 10 || expires > DEFAULT_EXPIRY_TIME) {
                throw new InvalidExpiresRangeException(expires,
                        "expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
            }
            url = minioClient.presignedPutObject(bucketName, objectName, expires);
        }
//        log.log(new LogRecordEntity(
//                "服务器文件获取文件定时下载地址(put:"+expires+"s):(bucketName)"+bucketName+", (fileName)"+objectName,
//                LogType.MINIO_SERVER,
//                getNUserId(),
//                this.getClass()
//        ));
        return url;
    }
 
    /**
     * 根据文件前置查询文件
     *
     * @param bucketName bucket名称
     * @param prefix     前缀
     * @param recursive  是否递归查询
     * @return MinioItem 列表
     */
    @SneakyThrows
    public List getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
        List<Item> list = new ArrayList<>();
        Iterable<Result<Item>> objectsIterator = minioClient.listObjects(bucketName, prefix, recursive);
        if (objectsIterator != null) {
            Iterator<Result<Item>> iterator = objectsIterator.iterator();
            if (iterator != null) {
                while (iterator.hasNext()) {
                    Result<Item> result = iterator.next();
                    Item item = result.get();
                    list.add(item);
                }
            }
        }
 
        return list;
    }
 
    /**
     * 获取文件
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @return 二进制流
     */
    @SneakyThrows
    public InputStream getObject(String bucketName, String objectName) {
        return minioClient.getObject(bucketName, objectName);
    }
 
 
    /**
     * 获取文件信息, 如果抛出异常则说明文件不存在
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
     */
    public ObjectStat getObjectInfo(String bucketName, String objectName) throws Exception {
        ObjectStat objectStat = minioClient.statObject(bucketName, objectName);
        return objectStat;
    }
 
    /**
     * 获取文件绝对路径
     *
     * @param bucketName
     * @param objectName
     * @return
     */
    public String getAbsolutePath(String bucketName, String objectName) {
        try {
            getObjectInfo(bucketName, objectName);
        } catch (Exception e) {
            return null;
        }
        return MinIoConstant.getAbsolutePath(bucketName, objectName);
    }
 
}