我是靠谱客的博主 典雅薯片,这篇文章主要介绍SpringBoot整合minioMinioPropertiesMinioUtilsFileTypeUtilsyml配置docker创建minio容器,现在分享给大家,希望可以做个参考。

MinioProperties

复制代码
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
package com.yujing.user.utils.minio; import io.minio.MinioClient; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * desc: minio参数 * * @author: 邢阳 * @mail: xydeveloper@126.com * @create 2020-06-24 15:33 */ @Data @Configuration @ConfigurationProperties(prefix = "minio") public class MinioProperties { @ApiModelProperty(value = "URL,域名,IPv4或者IPv6地址") private String endpoint; @ApiModelProperty(value = "端口号") private Integer port; @ApiModelProperty(value = "accessKey类似于用户ID,用于唯一标识你的账户") private String accessKey; @ApiModelProperty(value = "secretKey是你账户的密码") private String secretKey; @ApiModelProperty(value = "如果是true,则用的是https而不是http,默认值是true") private boolean secure; @ApiModelProperty(value = "默认存储桶") private String bucketName; @ApiModelProperty(value = "图片的最大大小") private long imageSize; @ApiModelProperty(value = "其他文件的最大大小") private long fileSize; @ApiModelProperty(value = "访问路径超时时间") private int timeOut; @ApiModelProperty(value = "非法存储桶名称") private String illegalBucketName; /** * 初始化 MinioClient */ @Bean public MinioClient minioClient() { return MinioClient.builder().credentials(accessKey, secretKey).endpoint(endpoint, port, secure).build(); } }

MinioUtils

复制代码
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
package com.yujing.user.utils.minio; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.concurrent.TimeUnit; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.ObjectId; import cn.hutool.core.text.StrPool; import cn.hutool.core.util.URLUtil; import com.yujing.user.common.ExceptionEnum; import com.yujing.user.common.MyRuntimeException; import com.yujing.user.entity.vo.MinioFileVo; import io.minio.*; import io.minio.http.Method; import io.minio.messages.DeleteObject; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import io.minio.messages.Bucket; import io.minio.messages.Item; import lombok.SneakyThrows; import javax.annotation.PostConstruct; /** * desc: minio工具类 * * @author: 邢阳 * @mail: xydeveloper@126.com * @create 2020-06-24 15:39 */ @Component public class MinioUtils { @Autowired private MinioClient minioClient; @Autowired public MinioProperties minioProperties; /** * 占位符 */ private static final String BUCKET_PARAM = "${bucket}"; /** * bucket权限-读写 */ private static final String READ_WRITE = "{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetBucketLocation","s3:ListBucket","s3:ListBucketMultipartUploads"],"Resource":["arn:aws:s3:::" + BUCKET_PARAM + ""]},{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:DeleteObject","s3:GetObject","s3:ListMultipartUploadParts","s3:PutObject","s3:AbortMultipartUpload"],"Resource":["arn:aws:s3:::" + BUCKET_PARAM + "/*"]}]}"; /** * 系统启动创建默认存储桶 */ @PostConstruct public void init() { String bucketName = minioProperties.getBucketName(); if (BooleanUtils.negate(bucketExists(bucketName))) { makeBucket(bucketName); } } /** * 检查存储桶是否存在 * * @param bucketName 存储桶名称 * @return */ @SneakyThrows public boolean bucketExists(String bucketName) { if (StringUtils.equals(minioProperties.getIllegalBucketName(), bucketName)) { throw new MyRuntimeException(ExceptionEnum.ILLEGAL_BUCKET_NAME); } return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); } /** * 创建存储桶 * * @param bucketName 存储桶名称 */ @SneakyThrows public boolean makeBucket(String bucketName) { if (BooleanUtils.negate(bucketExists(bucketName))) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); minioClient.setBucketPolicy(SetBucketPolicyArgs.builder() .bucket(bucketName) .config(READ_WRITE.replace(BUCKET_PARAM, bucketName)) .build()); return Boolean.TRUE; } else { return Boolean.FALSE; } } /** * 列出所有存储桶名称 * * @return */ @SneakyThrows public List<String> listBucketNames() { List<String> bucketListName = new ArrayList<>(); listBuckets().forEach(bucket -> bucketListName.add(bucket.name())); return bucketListName; } /** * 列出所有存储桶 * * @return */ @SneakyThrows public List<Bucket> listBuckets() { return minioClient.listBuckets(); } /** * 删除存储桶 * * @param bucketName 存储桶名称 * @return */ @SneakyThrows public boolean removeBucket(String bucketName) { if (bucketExists(bucketName)) { Iterable<Result<Item>> myObjects = listObjects(bucketName); for (Result<Item> result : myObjects) { Item item = result.get(); // 有对象文件,则删除失败 if (item.size() > 0) { return Boolean.FALSE; } } // 删除存储桶,注意,只有存储桶为空时才能删除成功。 minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); return BooleanUtils.negate(bucketExists(bucketName)); } return Boolean.FALSE; } /** * 列出存储桶中的所有对象名称 * * @param bucketName 存储桶名称 * @return */ @SneakyThrows public List<String> listObjectNames(String bucketName) { List<String> listObjectNames = new ArrayList<>(); if (bucketExists(bucketName)) { for (Result<Item> result : listObjects(bucketName)) { listObjectNames.add(result.get().objectName()); } } else { listObjectNames.add(ExceptionEnum.BUCKET_IS_EXIST.getMsg()); } return listObjectNames; } /** * 列出存储桶中的所有对象 * * @param bucketName 存储桶名称 * @return */ @SneakyThrows public Iterable<Result<Item>> listObjects(String bucketName) { return bucketExists(bucketName) ? minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build()) : null; } /** * 文件上传 * * @param bucketName * @param multipartFile */ @SneakyThrows public MinioFileVo upload(String bucketName, MultipartFile multipartFile) { String objectName = ObjectId.next(); if (BooleanUtils.negate(bucketExists(bucketName))) { makeBucket(bucketName); } minioClient.putObject(PutObjectArgs.builder().bucket(bucketName) .object(objectName).stream(new ByteArrayInputStream(multipartFile.getBytes()), -1, minioProperties.getFileSize()) .contentType(FileTypeUtils.getFileType(multipartFile)).build()); MinioFileVo minioFileVo = new MinioFileVo() .setFileUrl(getObjectUrl(bucketName, objectName)) .setObjectName(StringUtils.join(bucketName, StrPool.DASHED, objectName)); return minioFileVo; } /** * 文件访问路径 * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 * @return */ @SneakyThrows private String getObjectUrl(String bucketName, String objectName) { return bucketExists(bucketName) ? minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(Method.GET).bucket(bucketName).object(objectName).expiry(minioProperties.getTimeOut(), TimeUnit.MINUTES).build()) : null; } /** * 文件访问路径 * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 * @return */ @SneakyThrows public String getFileUrl(String bucketName, String objectName) { String fileUrl = getObjectUrl(bucketName,objectName); URLUtil.toUrlForHttp(fileUrl).openConnection().getInputStream(); return fileUrl; } /** * 删除一个对象 * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 */ @SneakyThrows public boolean delete(String bucketName, String objectName) { Boolean flag = bucketExists(bucketName); if (flag) { minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build()); } return flag; } /** * 以流的形式获取一个文件对象 * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 * @return */ @SneakyThrows public InputStream getObject(String bucketName, String objectName) { if (bucketExists(bucketName)) { StatObjectResponse statObject = statObject(bucketName, objectName); if (statObject != null && statObject.size() > 0) { return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build()); } } return null; } /** * 获取对象的元数据 * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 * @return */ @SneakyThrows public StatObjectResponse statObject(String bucketName, String objectName) { return bucketExists(bucketName) ? minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()) : null; } /** * 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表 * * @param bucketName 存储桶名称 * @param objectNames 含有要删除的多个object名称的迭代器对象 * @return */ @SneakyThrows public boolean deleteList(String bucketName, List<String> objectNames) { if (bucketExists(bucketName)) { List<DeleteObject> objects = new LinkedList<>(); for (int i = 0; i < objectNames.size(); i++) { objects.add(new DeleteObject(objectNames.get(i))); } if (CollUtil.isNotEmpty(minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(objects).build()))) { return Boolean.FALSE; } } return Boolean.TRUE; } /** * 以流的形式获取一个文件对象(断点下载) * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 * @param offset 起始字节的位置 * @param length 要读取的长度 (可选,如果无值则代表读到文件结尾) * @return */ @SneakyThrows public InputStream getObject(String bucketName, String objectName, long offset, Long length) { if (bucketExists(bucketName)) { StatObjectResponse statObject = statObject(bucketName, objectName); if (statObject != null && statObject.size() > 0) { return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).offset(offset).length(length).build()); } } return null; } /** * 通过InputStream上传对象 * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 * @param inputStream 要上传的流 * @param contentType 要上传的文件类型 MimeTypeUtils.IMAGE_JPEG_VALUE * @return */ @SneakyThrows public boolean upload(String bucketName, String objectName, InputStream inputStream, String contentType) { if (bucketExists(bucketName)) { minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, -1, minioProperties.getFileSize()).contentType(contentType).build()); StatObjectResponse statObject = statObject(bucketName, objectName); if (statObject != null && statObject.size() > 0) { return true; } } return Boolean.FALSE; } }

FileTypeUtils

复制代码
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
package com.yujing.user.utils.minio; import cn.hutool.core.io.FileTypeUtil; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; /** * desc: 文件格式工具类 * * @author: 邢阳 * @mail: xydeveloper@126.com * @create 2018-07-25 22:26 */ public class FileTypeUtils { private final static String IMAGE_TYPE = "image/"; private final static String AUDIO_TYPE = "audio/"; private final static String VIDEO_TYPE = "video/"; private final static String APPLICATION_TYPE = "application/"; private final static String TXT_TYPE = "text/"; public static String getFileType(MultipartFile multipartFile) { try { String type = FileTypeUtil.getType(multipartFile.getInputStream()); if (type.equalsIgnoreCase("JPG") || type.equalsIgnoreCase("JPEG") || type.equalsIgnoreCase("GIF") || type.equalsIgnoreCase("PNG") || type.equalsIgnoreCase("BMP") || type.equalsIgnoreCase("PCX") || type.equalsIgnoreCase("TGA") || type.equalsIgnoreCase("PSD") || type.equalsIgnoreCase("TIFF")) { return IMAGE_TYPE + type; } if (type.equalsIgnoreCase("mp3") || type.equalsIgnoreCase("OGG") || type.equalsIgnoreCase("WAV") || type.equalsIgnoreCase("REAL") || type.equalsIgnoreCase("APE") || type.equalsIgnoreCase("MODULE") || type.equalsIgnoreCase("MIDI") || type.equalsIgnoreCase("VQF") || type.equalsIgnoreCase("CD")) { return AUDIO_TYPE + type; } if (type.equalsIgnoreCase("mp4") || type.equalsIgnoreCase("avi") || type.equalsIgnoreCase("MPEG-1") || type.equalsIgnoreCase("RM") || type.equalsIgnoreCase("ASF") || type.equalsIgnoreCase("WMV") || type.equalsIgnoreCase("qlv") || type.equalsIgnoreCase("MPEG-2") || type.equalsIgnoreCase("MPEG4") || type.equalsIgnoreCase("mov") || type.equalsIgnoreCase("3gp")) { return VIDEO_TYPE + type; } if (type.equalsIgnoreCase("doc") || type.equalsIgnoreCase("docx") || type.equalsIgnoreCase("ppt") || type.equalsIgnoreCase("pptx") || type.equalsIgnoreCase("xls") || type.equalsIgnoreCase("xlsx") || type.equalsIgnoreCase("zip") || type.equalsIgnoreCase("jar")) { return APPLICATION_TYPE + type; } if (type.equalsIgnoreCase("txt")) { return TXT_TYPE + type; } } catch (IOException e) { e.printStackTrace(); } return null; } }

yml配置

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
#minio配置 minio: endpoint: http://10.168.2.110:9002/ port: 9002 access-key: minio secret-key: minio@123 secure: false bucket-name: yujing image-size: 10485760 file-size: 1073741824 time-out: 2 illegal-bucket-name: minio

docker创建minio容器

复制代码
1
2
3
4
5
6
7
8
9
10
docker run -p 9002:9000 --name minio -d --restart=always --privileged=true -e TZ="Asia/Shanghai" -e MINIO_ROOT_USER=minio -e MINIO_ROOT_PASSWORD=minio@123 -v /usr/local/develop/minio//data:/data -v /usr/local/develop/minio/config:/root/.minio minio/minio:RELEASE.2021-06-17T00-10-46Z server /data

最后

以上就是典雅薯片最近收集整理的关于SpringBoot整合minioMinioPropertiesMinioUtilsFileTypeUtilsyml配置docker创建minio容器的全部内容,更多相关SpringBoot整合minioMinioPropertiesMinioUtilsFileTypeUtilsyml配置docker创建minio容器内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部