更新Minio
This commit is contained in:
parent
be168e54b7
commit
d2776a6b1e
2
.gitignore
vendored
2
.gitignore
vendored
@ -46,3 +46,5 @@ nbdist/
|
||||
!*/build/*.java
|
||||
!*/build/*.html
|
||||
!*/build/*.xml
|
||||
|
||||
effective-pom.xml
|
@ -25,11 +25,17 @@ spring:
|
||||
|
||||
# Minio配置
|
||||
minio:
|
||||
enable: false
|
||||
enable: true
|
||||
downLoadLimit: 1024
|
||||
primary: MASTER
|
||||
client:
|
||||
master:
|
||||
url: http://localhost:9000
|
||||
MASTER:
|
||||
url: http://192.168.3.13:9000
|
||||
accessKey:
|
||||
secretKey:
|
||||
defaultBuket:
|
||||
buketName: plaform
|
||||
SLAVE:
|
||||
url: http://192.168.3.13:9000
|
||||
accessKey:
|
||||
secretKey:
|
||||
buketName: plaform
|
||||
|
@ -13,7 +13,7 @@ ruoyi:
|
||||
# 验证码类型 math 数组计算 char 字符验证
|
||||
captchaType: math
|
||||
# 指定默认文件服务类型(值为disk代表使用磁盘作为文件操作服务,minio代表使用minio作为文件操作服务)
|
||||
fileServer: disk
|
||||
fileServer: minio
|
||||
|
||||
# 开发环境配置
|
||||
server:
|
||||
|
@ -33,6 +33,8 @@ public class MimeTypeUtils
|
||||
"doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
|
||||
// 压缩文件
|
||||
"rar", "zip", "gz", "bz2",
|
||||
// 音频格式
|
||||
"mp3", "wav",
|
||||
// 视频格式
|
||||
"mp4", "avi", "rmvb",
|
||||
// pdf
|
||||
|
@ -1,103 +0,0 @@
|
||||
package com.ruoyi.middleware.minio.config;
|
||||
|
||||
import io.minio.MinioClient;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties("minio.client")
|
||||
public class MinioClientConfig {
|
||||
|
||||
public MinioClientEntity getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public void setMaster(MinioClientEntity master) {
|
||||
this.master = master;
|
||||
}
|
||||
|
||||
public List<MinioClientEntity> getSlave() {
|
||||
return slave;
|
||||
}
|
||||
|
||||
public void setSlave(List<MinioClientEntity> slave) {
|
||||
this.slave = slave;
|
||||
}
|
||||
|
||||
private MinioClientEntity master;
|
||||
|
||||
private List<MinioClientEntity> slave = new ArrayList<>();
|
||||
|
||||
public static class MinioClientEntity {
|
||||
|
||||
private String url;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String name;
|
||||
private String defaultBuket;
|
||||
|
||||
private MinioClient client;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDefaultBuket() {
|
||||
return defaultBuket;
|
||||
}
|
||||
|
||||
public void setDefaultBuket(String defaultBuket) {
|
||||
this.defaultBuket = defaultBuket;
|
||||
}
|
||||
|
||||
public MinioClient getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
public void setClient(MinioClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public MinioClientEntity(String url, String accessKey, String secretKey, String name, String defaultBuket) {
|
||||
this.url = url;
|
||||
this.accessKey = accessKey;
|
||||
this.secretKey = secretKey;
|
||||
this.name = name;
|
||||
this.defaultBuket = defaultBuket;
|
||||
}
|
||||
|
||||
public MinioClientEntity() {
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.ruoyi.middleware.minio.config;
|
||||
import io.minio.MinioClient;
|
||||
|
||||
public class MinioClientProperties {
|
||||
|
||||
private String url;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String buketName;
|
||||
|
||||
private MinioClient client;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
|
||||
public String getBuketName() {
|
||||
return buketName;
|
||||
}
|
||||
|
||||
public void setBuketName(String buketName) {
|
||||
this.buketName = buketName;
|
||||
}
|
||||
|
||||
public MinioClient getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
public void setClient(MinioClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public MinioClientProperties(String url, String accessKey, String secretKey, String buketName) {
|
||||
this.url = url;
|
||||
this.accessKey = accessKey;
|
||||
this.secretKey = secretKey;
|
||||
this.buketName = buketName;
|
||||
}
|
||||
|
||||
public MinioClientProperties() {
|
||||
}
|
||||
}
|
@ -1,109 +1,106 @@
|
||||
package com.ruoyi.middleware.minio.config;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.apache.http.impl.io.EmptyInputStream;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.ruoyi.common.config.RuoYiConfig;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
import com.ruoyi.middleware.minio.domain.MinioBucket;
|
||||
|
||||
import io.minio.BucketExistsArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
@Configuration("MinioConfiguration")
|
||||
@ConditionalOnProperty(prefix = "minio", name = { "enable" }, havingValue = "true", matchIfMissing = false)
|
||||
public class MinioConfig {
|
||||
@ConfigurationProperties("minio")
|
||||
public class MinioConfig implements InitializingBean {
|
||||
|
||||
public int maxSize;
|
||||
public static int maxSize;
|
||||
private String prefix = "/minio";
|
||||
private Map<String, MinioClientProperties> client;
|
||||
private String primary;
|
||||
private Map<String, MinioBucket> targetMinioBucket = new HashMap<>();
|
||||
private MinioBucket masterBucket;
|
||||
|
||||
public static String prefix = "/minio";
|
||||
@Autowired
|
||||
private MinioClientConfig minioClientConfig;
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
client.forEach((name, props) -> targetMinioBucket.put(name, createMinioClient(name, props)));
|
||||
if (targetMinioBucket.get(primary) == null) {
|
||||
throw new RuntimeException("Primary client " + primary + " does not exist");
|
||||
}
|
||||
masterBucket = targetMinioBucket.get(primary);
|
||||
}
|
||||
|
||||
private Map<String, MinioClientConfig.MinioClientEntity> slaveClients = new ConcurrentHashMap<>();
|
||||
|
||||
private List<MinioClientConfig.MinioClientEntity> slaveClientsList = new CopyOnWriteArrayList<>();
|
||||
|
||||
private MinioClientConfig.MinioClientEntity masterClient;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
List<MinioClientConfig.MinioClientEntity> collect = minioClientConfig.getSlave().stream().map(item -> {
|
||||
setClient(item);
|
||||
isBuketExistOnAnonymous(item);
|
||||
return item;
|
||||
}).toList();
|
||||
collect.forEach(item -> {
|
||||
slaveClients.put(item.getName(), item);
|
||||
slaveClientsList.add(item);
|
||||
});
|
||||
MinioClientConfig.MinioClientEntity master = minioClientConfig.getMaster();
|
||||
setClient(master);
|
||||
isBuketExistOnAnonymous(master);
|
||||
masterClient = master;
|
||||
private MinioBucket createMinioClient(String name, MinioClientProperties props) {
|
||||
MinioClient client;
|
||||
if (StringUtils.isEmpty(props.getAccessKey())) {
|
||||
client = MinioClient.builder()
|
||||
.endpoint(props.getUrl())
|
||||
.build();
|
||||
} else {
|
||||
client = MinioClient.builder()
|
||||
.endpoint(props.getUrl())
|
||||
.credentials(props.getAccessKey(), props.getSecretKey())
|
||||
.build();
|
||||
}
|
||||
BucketExistsArgs bucketExistArgs = BucketExistsArgs.builder().bucket(props.getBuketName()).build();
|
||||
boolean b = false;
|
||||
try {
|
||||
b = client.bucketExists(bucketExistArgs);
|
||||
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
||||
.object(FileUtils.getRelativePath(RuoYiConfig.getProfile()) + "/")
|
||||
.stream(EmptyInputStream.nullInputStream(), 0, -1).bucket(props.getBuketName()).build();
|
||||
client.putObject(putObjectArgs);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
if (!b) {
|
||||
throw new RuntimeException("Bucket " + props.getBuketName() + " does not exist");
|
||||
}
|
||||
return new MinioBucket(client, props.getBuketName());
|
||||
}
|
||||
|
||||
public int getMaxSize() {
|
||||
return maxSize;
|
||||
}
|
||||
|
||||
public List<MinioClientConfig.MinioClientEntity> getSlaveClientsList() {
|
||||
return slaveClientsList;
|
||||
public MinioBucket getMasterBucket() {
|
||||
return this.masterBucket;
|
||||
}
|
||||
|
||||
public Map<String, MinioClientConfig.MinioClientEntity> getSlaveClients() {
|
||||
return this.slaveClients;
|
||||
public MinioBucket getBucket(String clent) {
|
||||
return targetMinioBucket.get(clent);
|
||||
}
|
||||
|
||||
public MinioClientConfig.MinioClientEntity getMasterClient() {
|
||||
return this.masterClient;
|
||||
public Map<String, MinioClientProperties> getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
private static void setClient(MinioClientConfig.MinioClientEntity entity){
|
||||
if (StringUtils.isEmpty(entity.getAccessKey())){
|
||||
MinioClient build = MinioClient.builder().endpoint(entity.getUrl()).build();
|
||||
entity.setClient(build);
|
||||
}else {
|
||||
MinioClient build = MinioClient.builder().endpoint(entity.getUrl())
|
||||
.credentials(entity.getAccessKey(), entity.getSecretKey()).build();
|
||||
entity.setClient(build);
|
||||
BucketExistsArgs bucketExistArgs = BucketExistsArgs.builder().bucket(entity.getDefaultBuket()).build();
|
||||
try {
|
||||
boolean b = entity.getClient().bucketExists(bucketExistArgs);
|
||||
if (!b){
|
||||
throw new RuntimeException("defaultBucket does not exist");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
entity.setClient(build);
|
||||
}
|
||||
public void setClient(Map<String, MinioClientProperties> client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
|
||||
private static void isBuketExistOnAnonymous(MinioClientConfig.MinioClientEntity entity) {
|
||||
try {
|
||||
String defaultBuket = entity.getDefaultBuket();
|
||||
if(StringUtils.isEmpty(defaultBuket)){
|
||||
throw new RuntimeException("defaultBuket without a default value ");
|
||||
}
|
||||
PutObjectArgs putObjectArgs= PutObjectArgs.builder().object(FileUtils.getRelativePath(RuoYiConfig.getProfile())+ "/")
|
||||
.stream(EmptyInputStream.nullInputStream(),0,-1).bucket(entity.getDefaultBuket()).build();
|
||||
entity.getClient().putObject(putObjectArgs);
|
||||
|
||||
}catch (Exception e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
public String getPrimary() {
|
||||
return primary;
|
||||
}
|
||||
|
||||
public void setPrimary(String primary) {
|
||||
this.primary = primary;
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public void setPrefix(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,12 @@ package com.ruoyi.middleware.minio.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.ruoyi.common.annotation.Anonymous;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
@ -20,28 +23,22 @@ import okhttp3.Headers;
|
||||
@RequestMapping("/minio")
|
||||
public class MinioController {
|
||||
|
||||
|
||||
@GetMapping("/{buketName}")
|
||||
@GetMapping("/{client}")
|
||||
@Anonymous
|
||||
public void downLoadFile(HttpServletRequest request, HttpServletResponse response,
|
||||
@PathVariable("buketName") String buketName,
|
||||
@RequestParam("fileName") String fileName,
|
||||
@RequestParam(value = "clientName", required = false) String clientName) throws Exception {
|
||||
@PathVariable("client") String client,
|
||||
@RequestParam("fileName") String fileName) throws Exception {
|
||||
MinioFileVO file = MinioUtil.getFile(client, fileName);
|
||||
Headers headers = file.getHeaders();
|
||||
String contentType = headers.get("content-Type");
|
||||
response.setContentType(contentType);
|
||||
FileUtils.writeBytes(file.getFileInputSteam(), response.getOutputStream());
|
||||
}
|
||||
|
||||
if (!StringUtils.isEmpty(clientName)) {
|
||||
MinioFileVO file = MinioUtil.SlaveClient.getFile(clientName, buketName, fileName);
|
||||
Headers headers = file.getHeaders();
|
||||
|
||||
String contentType = headers.get("content-Type");
|
||||
response.setContentType(contentType);
|
||||
FileUtils.writeBytes(file.getFileInputSteam(), response.getOutputStream());
|
||||
} else {
|
||||
MinioFileVO file = MinioUtil.getFile(buketName, fileName);
|
||||
|
||||
Headers headers = file.getHeaders();
|
||||
String contentType = headers.get("content-Type");
|
||||
response.setContentType(contentType);
|
||||
FileUtils.writeBytes(file.getFileInputSteam(), response.getOutputStream());
|
||||
}
|
||||
@PutMapping("/{client}")
|
||||
@Anonymous
|
||||
public String uploadFile(HttpServletRequest request, HttpServletResponse response,
|
||||
@PathVariable("client") String client, @RequestBody MultipartFile file) throws Exception {
|
||||
return MinioUtil.uploadFile(client, file);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,111 @@
|
||||
package com.ruoyi.middleware.minio.domain;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.ruoyi.middleware.minio.config.MinioConfig;
|
||||
import com.ruoyi.middleware.minio.exception.MinioClientErrorException;
|
||||
|
||||
import io.minio.GetObjectArgs;
|
||||
import io.minio.GetObjectResponse;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import com.ruoyi.common.exception.file.FileException;
|
||||
|
||||
public class MinioBucket {
|
||||
|
||||
private MinioClient client;
|
||||
private String buketName;
|
||||
|
||||
public MinioBucket() {
|
||||
}
|
||||
|
||||
public MinioBucket(MinioClient client, String buketName) {
|
||||
this.client = client;
|
||||
this.buketName = buketName;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return buketName;
|
||||
}
|
||||
|
||||
public MinioClient getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
public String getBuketName() {
|
||||
return buketName;
|
||||
}
|
||||
|
||||
public void setBuketName(String buketName) {
|
||||
this.buketName = buketName;
|
||||
}
|
||||
|
||||
public void setClient(MinioClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public void put(String fileName, MultipartFile file) throws Exception {
|
||||
put(fileName, file.getContentType(), file.getInputStream());
|
||||
}
|
||||
|
||||
public void put(String filePath, String contentType, InputStream inputStream) throws Exception {
|
||||
PutObjectArgs build = PutObjectArgs.builder().contentType(contentType)
|
||||
.stream(inputStream, inputStream.available(), -1)
|
||||
.bucket(this.buketName).object(filePath).build();
|
||||
put(build);
|
||||
}
|
||||
|
||||
public void put(PutObjectArgs putObjectArgs) throws Exception {
|
||||
try {
|
||||
this.client.putObject(putObjectArgs);
|
||||
} catch (Exception e) {
|
||||
throw new MinioClientErrorException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(String filePath) throws Exception {
|
||||
RemoveObjectArgs build = RemoveObjectArgs.builder().object(filePath).bucket(buketName).build();
|
||||
remove(build);
|
||||
}
|
||||
|
||||
public void remove(RemoveObjectArgs removeObjectArgs) throws Exception {
|
||||
this.client.removeObject(removeObjectArgs);
|
||||
}
|
||||
|
||||
public MinioFileVO get(GetObjectArgs getObjectArgs) throws Exception {
|
||||
GetObjectResponse inputStream = this.client.getObject(getObjectArgs);
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
byte[] bytes = new byte[MinioConfig.maxSize];
|
||||
int length = 0;
|
||||
while (true) {
|
||||
try {
|
||||
if (!((length = inputStream.read(bytes, 0, bytes.length)) > 0)) {
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new FileException("500", new String[] { e.getMessage() });
|
||||
}
|
||||
byteArrayOutputStream.write(bytes, 0, length);
|
||||
}
|
||||
return new MinioFileVO(inputStream, inputStream.object(), inputStream.headers(), inputStream.bucket(),
|
||||
inputStream.region());
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*
|
||||
* @param buketName Minio的桶名
|
||||
* @param filePath 文件路径
|
||||
* @return 返回封装的Minio下载文件对象
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public MinioFileVO get(String filePath) throws Exception {
|
||||
GetObjectArgs build = GetObjectArgs.builder().object(filePath).bucket(buketName).build();
|
||||
return get(build);
|
||||
}
|
||||
}
|
@ -1,18 +1,19 @@
|
||||
package com.ruoyi.middleware.minio.domain;
|
||||
|
||||
|
||||
import okhttp3.Headers;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class MinioFileVO {
|
||||
|
||||
|
||||
private InputStream fileInputSteam;
|
||||
private String object;
|
||||
private Headers headers;
|
||||
private String buket;
|
||||
private String region;
|
||||
|
||||
public MinioFileVO() {
|
||||
}
|
||||
|
||||
public InputStream getFileInputSteam() {
|
||||
return fileInputSteam;
|
||||
}
|
||||
@ -21,7 +22,6 @@ public class MinioFileVO {
|
||||
this.fileInputSteam = fileInputSteam;
|
||||
}
|
||||
|
||||
|
||||
public String getObject() {
|
||||
return object;
|
||||
}
|
||||
@ -62,7 +62,4 @@ public class MinioFileVO {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public MinioFileVO() {
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.ruoyi.middleware.minio.utils;
|
||||
package com.ruoyi.middleware.minio.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
@ -16,8 +16,7 @@ import com.ruoyi.common.utils.file.FileService;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
import com.ruoyi.middleware.minio.config.MinioConfig;
|
||||
import com.ruoyi.middleware.minio.domain.MinioFileVO;
|
||||
|
||||
import static com.ruoyi.common.utils.file.FileUtils.getPathFileName;
|
||||
import com.ruoyi.middleware.minio.utils.MinioUtil;
|
||||
|
||||
/**
|
||||
* Minio文件操作实现类
|
||||
@ -37,12 +36,12 @@ public class MinioFileService implements FileService {
|
||||
relativePath = filePath;
|
||||
}
|
||||
|
||||
return MinioUtil.uploadFile(minioConfig.getMasterClient().getDefaultBuket(), relativePath, file);
|
||||
return MinioUtil.uploadFile(minioConfig.getPrimary(), relativePath, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(MultipartFile file, String name) throws Exception {
|
||||
return MinioUtil.uploadFile(minioConfig.getMasterClient().getDefaultBuket(), name, file);
|
||||
return MinioUtil.uploadFile(minioConfig.getPrimary(), name, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -53,21 +52,20 @@ public class MinioFileService implements FileService {
|
||||
|
||||
@Override
|
||||
public String upload(String baseDir, String fileName, MultipartFile file) throws Exception {
|
||||
|
||||
return upload(baseDir + File.pathSeparator + fileName, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream downLoad(String fileUrl) throws Exception {
|
||||
String filePath = StringUtils.substringAfter(fileUrl, "?fileName=");
|
||||
MinioFileVO file = MinioUtil.getFile(minioConfig.getMasterClient().getDefaultBuket(), filePath);
|
||||
MinioFileVO file = MinioUtil.getFile(minioConfig.getPrimary(), filePath);
|
||||
return file.getFileInputSteam();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteFile(String fileUrl) throws Exception {
|
||||
String filePath = StringUtils.substringAfter(fileUrl, "?fileName=");
|
||||
MinioUtil.removeFile(minioConfig.getMasterClient().getDefaultBuket(), filePath);
|
||||
MinioUtil.removeFile(minioConfig.getPrimary(), filePath);
|
||||
FileOperateUtils.deleteFileAndMd5ByFilePath(filePath);
|
||||
return true;
|
||||
}
|
@ -1,29 +1,15 @@
|
||||
package com.ruoyi.middleware.minio.utils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.ruoyi.common.exception.file.FileException;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import com.ruoyi.common.utils.uuid.UUID;
|
||||
import com.ruoyi.middleware.minio.config.MinioClientConfig;
|
||||
import com.ruoyi.middleware.minio.config.MinioConfig;
|
||||
import com.ruoyi.middleware.minio.domain.MinioFileVO;
|
||||
import com.ruoyi.middleware.minio.exception.MinioClientErrorException;
|
||||
|
||||
import io.minio.GetObjectArgs;
|
||||
import io.minio.GetObjectResponse;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import io.minio.RemoveObjectsArgs;
|
||||
import io.minio.Result;
|
||||
import io.minio.messages.DeleteError;
|
||||
|
||||
/**
|
||||
* Minio工具
|
||||
*
|
||||
@ -34,7 +20,7 @@ public class MinioUtil {
|
||||
|
||||
private static MinioConfig getMinioConfig() {
|
||||
if (minioConfig == null) {
|
||||
synchronized (MinioFileService.class) {
|
||||
synchronized (MinioUtil.class) {
|
||||
if (minioConfig == null) {
|
||||
minioConfig = SpringUtils.getBean(MinioConfig.class);
|
||||
}
|
||||
@ -46,73 +32,17 @@ public class MinioUtil {
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param buketName Minio的桶名
|
||||
* @param filePath 上传的文件路径
|
||||
* @param contentType 上传文件类型
|
||||
* @param inputStream 上传文件的输入流
|
||||
* @return 返回上传成功的文路径
|
||||
* @param buketName Minio的桶名
|
||||
* @param file 上传的文件
|
||||
* @return 返回上传成功的文件名
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*
|
||||
*/
|
||||
public static String uploadFile(String buketName, String filePath,
|
||||
String contentType, InputStream inputStream)
|
||||
throws Exception {
|
||||
PutObjectArgs build = PutObjectArgs.builder().contentType(contentType)
|
||||
.stream(inputStream, inputStream.available(), -1)
|
||||
.bucket(buketName).object(filePath).build();
|
||||
return uploadFile(build);
|
||||
public static String uploadFile(String client, MultipartFile file) throws Exception {
|
||||
String fileName = DateUtils.dateTimeNow() + UUID.fastUUID().toString().substring(0, 5) + "."
|
||||
+ FileUtils.getExtension(file);
|
||||
return uploadFile(client, fileName, file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param putObjectArgs Minio上传参数
|
||||
* @return 返回上传成功的文件路径
|
||||
*/
|
||||
public static String uploadFile(PutObjectArgs putObjectArgs) throws Exception {
|
||||
try {
|
||||
MinioClientConfig.MinioClientEntity masterClient = getMinioConfig().getMasterClient();
|
||||
masterClient.getClient().putObject(putObjectArgs);
|
||||
StringBuilder url = new StringBuilder();
|
||||
url.append(MinioConfig.prefix).append("/").append(masterClient.getDefaultBuket())
|
||||
// .append("/").append(filePath)
|
||||
.append("?").append("fileName=").append(putObjectArgs.object());
|
||||
return url.toString();
|
||||
} catch (Exception e) {
|
||||
throw new MinioClientErrorException(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传(从节点递归,直到上传成功)
|
||||
*
|
||||
* @param index 开始递归的从节点索引
|
||||
* @param putObjectArgs Minio上传文件参数
|
||||
* @return 返回上传成功的文件路径
|
||||
*/
|
||||
// private static String uploadFileIterator(int index, PutObjectArgs putObjectArgs) {
|
||||
// List<MinioClientConfig.MinioClientEntity> slaveClientsList = getMinioConfig().getSlaveClientsList();
|
||||
// if (index >= slaveClientsList.size()) {
|
||||
// throw new MinioClientNotFundException();
|
||||
// }
|
||||
// try {
|
||||
// MinioClientConfig.MinioClientEntity minioClientEntity = slaveClientsList.get(index);
|
||||
// PutObjectArgs build = PutObjectArgs.builder().contentType(putObjectArgs.contentType())
|
||||
// .object(putObjectArgs.object())
|
||||
// .stream(putObjectArgs.stream(), putObjectArgs.stream().available(), -1)
|
||||
// .bucket(minioClientEntity.getDefaultBuket()).build();
|
||||
// minioClientEntity.getClient().putObject(build);
|
||||
// StringBuilder url = new StringBuilder();
|
||||
// url.append(MinioConfig.prefix).append("/").append(minioClientEntity.getDefaultBuket())
|
||||
// .append("?").append("fileName=").append(putObjectArgs.object())
|
||||
// .append("&").append("clientName=").append(minioClientEntity.getName());
|
||||
// return url.toString();
|
||||
// } catch (Exception e) {
|
||||
// return uploadFileIterator(index + 1, putObjectArgs);
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
@ -122,169 +52,71 @@ public class MinioUtil {
|
||||
* @return 返回上传成功的文件名
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static String uploadFile(String buketName, String fileName, MultipartFile file) throws Exception {
|
||||
return uploadFile(buketName, fileName, file.getContentType(), file.getInputStream());
|
||||
public static String uploadFile(String client, String fileName, MultipartFile file) throws Exception {
|
||||
getMinioConfig().getBucket(client).put(fileName, file);
|
||||
return getURL(client, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param buketName Minio的桶名
|
||||
* @param file 上传的文件
|
||||
* @return 返回上传成功的文件名
|
||||
* @throws IOException 比如读写文件出错时
|
||||
* @param filePath 文件路径
|
||||
* @return 返回上传成功的文件路径
|
||||
*/
|
||||
public static String uploadFile(String buketName, MultipartFile file) throws Exception {
|
||||
String fileName = DateUtils.dateTimeNow() + UUID.fastUUID().toString().substring(0, 5) + "."
|
||||
+ FileUtils.getExtension(file);
|
||||
|
||||
return uploadFile(buketName, fileName, file.getContentType(), file.getInputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param removeObjectArgs Minio删除文件的参数对象
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static void removeFile(RemoveObjectArgs removeObjectArgs) throws Exception {
|
||||
getMinioConfig().getMasterClient().getClient().removeObject(removeObjectArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param buketName Minio的桶名
|
||||
* @param filePath 上传文件路径
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static void removeFile(String buketName, String filePath) throws Exception {
|
||||
RemoveObjectArgs build = RemoveObjectArgs.builder().object(filePath).bucket(buketName).build();
|
||||
removeFile(build);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件批量删除
|
||||
*
|
||||
* @param removeObjectsArgs Minio批量删除文件参数对象
|
||||
* @return 删除结果
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static Iterable<Result<DeleteError>> removeFiles(RemoveObjectsArgs removeObjectsArgs) {
|
||||
return getMinioConfig().getMasterClient().getClient().removeObjects(removeObjectsArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*
|
||||
* @param getObjectArgs Minio获取文件参数对象
|
||||
* @return 返回封装的Minio下载结果对象
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static MinioFileVO getFile(GetObjectArgs getObjectArgs) throws Exception {
|
||||
GetObjectResponse inputStream = getMinioConfig().getMasterClient().getClient().getObject(getObjectArgs);
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
byte[] bytes = new byte[getMinioConfig().maxSize];
|
||||
int length = 0;
|
||||
while (true) {
|
||||
try {
|
||||
if (!((length = inputStream.read(bytes, 0, bytes.length)) > 0)) {
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new FileException("500", new String[] { e.getMessage() });
|
||||
}
|
||||
byteArrayOutputStream.write(bytes, 0, length);
|
||||
public static String getURL(String client, String filePath) throws Exception {
|
||||
try {
|
||||
StringBuilder url = new StringBuilder();
|
||||
url.append(getMinioConfig().getPrefix()).append("/").append(client)
|
||||
.append("?").append("fileName=").append(filePath);
|
||||
return url.toString();
|
||||
} catch (Exception e) {
|
||||
throw new MinioClientErrorException(e.getMessage());
|
||||
}
|
||||
return new MinioFileVO(inputStream, inputStream.object(), inputStream.headers(), inputStream.bucket(),
|
||||
inputStream.region());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param filePath 上传文件路径
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static void removeFile(String filePath) throws Exception {
|
||||
getMinioConfig().getMasterBucket().remove(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param client 连接名
|
||||
* @param filePath 上传文件路径
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static void removeFile(String client, String filePath) throws Exception {
|
||||
getMinioConfig().getBucket(client).remove(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*
|
||||
* @param buketName Minio的桶名
|
||||
* @param filePath 文件路径
|
||||
* @param filePath 文件路径
|
||||
* @return 返回封装的Minio下载文件对象
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static MinioFileVO getFile(String buketName, String filePath) throws Exception {
|
||||
GetObjectArgs build = GetObjectArgs.builder().object(filePath).bucket(buketName).build();
|
||||
return getFile(build);
|
||||
public static MinioFileVO getFile(String filePath) throws Exception {
|
||||
return getMinioConfig().getMasterBucket().get(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从节点对应操作工具类
|
||||
* 文件下载
|
||||
*
|
||||
* @param client 连接名
|
||||
* @param filePath 文件路径
|
||||
* @return 返回封装的Minio下载文件对象
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static class SlaveClient {
|
||||
public static String uploadFile(String clientName, String buketName, String fileName,
|
||||
String contentType, InputStream inputStream)
|
||||
throws Exception {
|
||||
PutObjectArgs build = PutObjectArgs.builder().contentType(contentType)
|
||||
.stream(inputStream, inputStream.available(), -1)
|
||||
.bucket(buketName).object(fileName).build();
|
||||
return uploadFile(clientName, build);
|
||||
}
|
||||
|
||||
public static String uploadFile(String clientName, PutObjectArgs putObjectArgs) throws Exception {
|
||||
MinioClientConfig.MinioClientEntity minioClientEntity = getMinioConfig().getSlaveClients().get(clientName);
|
||||
minioClientEntity.getClient().putObject(putObjectArgs);
|
||||
StringBuilder url = new StringBuilder();
|
||||
url.append(MinioConfig.prefix).append("/").append(minioClientEntity.getDefaultBuket())
|
||||
.append("?").append("fileName=").append(putObjectArgs.object())
|
||||
.append("&").append("clientName=").append(minioClientEntity.getName());
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
public static String uploadFile(String clientName, String buketName, String fileName, MultipartFile file)
|
||||
throws Exception {
|
||||
|
||||
return uploadFile(clientName, buketName, fileName, file.getContentType(), file.getInputStream());
|
||||
}
|
||||
|
||||
public static String uploadFile(String clientName, String buketName, MultipartFile file) throws Exception {
|
||||
String fileName = DateUtils.dateTimeNow() + UUID.fastUUID().toString().substring(0, 5);
|
||||
return uploadFile(clientName, buketName, fileName, file.getContentType(), file.getInputStream());
|
||||
}
|
||||
|
||||
public static void removeFile(String clientName, RemoveObjectArgs removeObjectArgs) throws Exception {
|
||||
getMinioConfig().getSlaveClients().get(clientName).getClient().removeObject(removeObjectArgs);
|
||||
}
|
||||
|
||||
public static void removeFile(String clientName, String buketName, String fileName) throws Exception {
|
||||
RemoveObjectArgs build = RemoveObjectArgs.builder().object(fileName).bucket(buketName).build();
|
||||
removeFile(clientName, build);
|
||||
}
|
||||
|
||||
public static Iterable<Result<DeleteError>> removeFiles(RemoveObjectsArgs removeObjectsArgs) {
|
||||
return getMinioConfig().getMasterClient().getClient().removeObjects(removeObjectsArgs);
|
||||
}
|
||||
|
||||
public static MinioFileVO getFile(String clientName, GetObjectArgs getObjectArgs) throws Exception {
|
||||
GetObjectResponse inputStream = getMinioConfig().getSlaveClients().get(clientName).getClient()
|
||||
.getObject(getObjectArgs);
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
byte[] bytes = new byte[getMinioConfig().maxSize];
|
||||
int length = 0;
|
||||
while (true) {
|
||||
try {
|
||||
if (!((length = inputStream.read(bytes, 0, bytes.length)) > 0)) {
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new FileException("500", new String[] { e.getMessage() });
|
||||
}
|
||||
byteArrayOutputStream.write(bytes, 0, length);
|
||||
}
|
||||
return new MinioFileVO(inputStream, inputStream.object(), inputStream.headers(), inputStream.bucket(),
|
||||
inputStream.region());
|
||||
}
|
||||
|
||||
public static MinioFileVO getFile(String clientName, String buketName, String fileName) throws Exception {
|
||||
GetObjectArgs build = GetObjectArgs.builder().object(fileName).bucket(buketName).build();
|
||||
return getFile(clientName, build);
|
||||
}
|
||||
|
||||
public static MinioFileVO getFile(String client, String filePath) throws Exception {
|
||||
return getMinioConfig().getBucket(client).get(filePath);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user