FileServerOptUtil.java
4.07 KB
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
package com.pashanhoo.common.minio;
import io.minio.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
@Component
public class FileServerOptUtil {
private Logger log = LoggerFactory.getLogger(FileServerOptUtil.class);
@Autowired
MinioClient minioClient;
/**
* 获取桶中某个对象的输入流
* @param bucketName
* @param path
* @return
*/
public InputStream getObjectInputStream(String bucketName, String path){
log.info("从桶:" + bucketName + ",获取:" + path + "对象流!");
InputStream stream = null;
try {
stream = minioClient.getObject(
GetObjectArgs.builder().bucket(bucketName).object(path).build());
} catch (Exception e) {
log.error("从桶:" + bucketName + ",获取:" + path + "对象流错误!");
e.printStackTrace();
}
return stream;
}
/**
*
* @param bucketName
* @param dirPath 如:path/to/
* @return
*/
public boolean mkdir(String bucketName,String dirPath){
log.info("在桶:" + bucketName + ",中创建:" + dirPath);
boolean result = false;
try {
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(dirPath).stream(
new ByteArrayInputStream(new byte[] {}), 0, -1)
.build());
result = true;
} catch (Exception e) {
log.error("在桶:" + bucketName + ",中创建:" + dirPath + "错误!");
e.printStackTrace();
}
return result;
}
/**
* 往对象存储服务目标桶的目标位置上传文件
* @param bucketName 桶
* @param toPath 目标位置如:test/2.txt
* @param fromPath 本地文件位置
* @return
*/
public boolean uploadObject(String bucketName,String toPath,String fromPath ){
log.info("从本地:" + fromPath + "往桶:" + bucketName + ",中上传文件:" + toPath);
boolean result = false;
try {
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(bucketName)
.object(toPath)
.filename(fromPath)
.build());
result = true;
} catch (Exception e) {
log.error("从本地:" + fromPath + "往桶:" + bucketName + ",中上传文件:" + toPath + "错误!");
e.printStackTrace();
}
return result;
}
/**
* 从桶中下载文件到本地文件
* @param bucketName
* @param fromPath
* @param toPath
* @return
*/
public boolean downloadFile(String bucketName,String fromPath,String toPath){
log.info("从桶:" + bucketName + "中将" + fromPath + "文件下载到:" + toPath);
boolean result = false;
try {
minioClient.downloadObject(
DownloadObjectArgs.builder()
.bucket(bucketName)
.object(fromPath)
.filename(toPath)
.build());
result = true;
} catch (Exception e) {
log.error("从桶:" + bucketName + "中将" + fromPath + "文件下载到:" + toPath + "错误!");
e.printStackTrace();
}
return result;
}
public static String extractFilename(MultipartFile file){
if (file.isEmpty()) {
throw new RuntimeException("文件不存在!");
}
return DateUtils.dateToStr(new Date()) + "/" + System.currentTimeMillis() + "_" + file.getOriginalFilename() ;
}
}