‘liusuyi’
2023-05-30 44ef24375d57b7bf38369aa8de83605e800460d9
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
package com.ruoyi.device.hiksdk.util.minioUtil;
 
/**
 * @ClassName: MinioUtil
 * @Description: minio 工具类,提供上传、下载方法
 * @Author: Administrator
 * @Date: 2023年01月28日 13:28
 * @Version: 1.0
 **/
 
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.InputStream;
 
 
@Slf4j(topic = "minio")
@Component
@RequiredArgsConstructor
public class MinioUtil {
 
    private final MinioClient minioClient;
 
    /**
     * 判断bulk桶是否存在
     *
     * @param bulkName
     * @return
     */
    private boolean isBuckExist(String bulkName) {
        try {
            return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bulkName).build());
        } catch (Exception e) {
            log.error("bulkname is not exist,error " + e.getMessage());
            return false;
        }
    }
 
    /**
     * 上传对象-通过本地路径
     *
     * @param bulkName
     * @param objectName
     * @param localFilePathName
     * @return
     */
    public boolean uploadObject(String bulkName, String objectName, String localFilePathName) {
        try {
            if (!isBuckExist(bulkName)) {
                log.debug(bulkName + "不存在");
                 return false;
            }
            File file = new File(localFilePathName);
            if (!file.exists()) {
                log.debug("文件不存在");
                 return false;
            }
            ObjectWriteResponse objectWriteResponse = minioClient.uploadObject(UploadObjectArgs.builder().bucket(bulkName).object(objectName).filename(localFilePathName).build());
            return true;
        } catch (Exception e) {
            log.error("minio upload object file error " + e.getMessage());
            return false;
        }
    }
 
    /**
     * 下载对象
     *
     * @param bulkName
     * @param objectName
     * @param localFilePathName
     * @return
     */
    public boolean downloadObject(String bulkName, String objectName, String localFilePathName) {
        if (isBuckExist(bulkName)) {
            try {
                minioClient.downloadObject(DownloadObjectArgs.builder().bucket(bulkName).object(objectName).filename(localFilePathName).build());
                return true;
            } catch (Exception e) {
                log.error("minio download object file error " + e.getMessage());
                return false;
            }
 
        } else {
            return false;
        }
    }
 
    /**
     * 删除对象
     *
     * @param bulkName
     * @param objectName
     * @return
     */
    public boolean deleteObject(String bulkName, String objectName) {
        if (isBuckExist(bulkName)) {
            try {
                minioClient.removeObject(RemoveObjectArgs.builder().bucket(bulkName).object(objectName).build());
                return true;
            } catch (Exception e) {
                log.error("minio delete object file error " + e.getMessage());
                return false;
            }
        } else {
            return false;
        }
    }
 
 
    /**
     * 生成一个GET请求的分享链接。
     * 失效时间默认是7天。
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param expires    失效时间(以秒为单位),默认是7天,不得大于七天
     * @return
     */
    public String presignedGetObject(String bucketName, String objectName, Integer expires) {
        boolean bucketExists = isBuckExist(bucketName);
        String url = "";
        if (bucketExists) {
            try {
                if (expires == null) {
                    expires = 604800;
                }
                GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder()
                        .method(Method.GET)
                        .bucket(bucketName)
                        .object(objectName)
//                        .expiry(expires)
                        .build();
                url = minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs);
                // log.info("*******url:{}", url);
            } catch (Exception e) {
                log.info("presigned get object fail:{}", e);
            }
        }
        return url;
    }
 
    /**
     * 上传⽂件
     *
     * @param bucketName  bucket名称
     * @param objectName  ⽂件名称
     * @param stream      ⽂件流
     * @param size        ⼤⼩
     * @param contextType 类型
     * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
     */
    public boolean putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) {
 
        try {
            ObjectWriteResponse objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(stream, size, -1).contentType(contextType).build());
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return false;
        }
    }
 
 
    /**
     *@描述 获取桶中所有对象
     *@参数 [bucketName]
     *@返回值 java.lang.Iterable<io.minio.Result<io.minio.messages.Item>>
     *@创建人 刘苏义
     *@创建时间 2023/2/6 10:32
     *@修改人和其它信息
     */
    public Iterable<Result<Item>> getObjectsByBucket(String bucketName){
 
        Iterable<Result<Item>> listObjects = minioClient.listObjects(ListObjectsArgs.builder()
                .bucket(bucketName)
                .recursive(true)
                .build());
        return listObjects;
    }
}