feat(storage): 集成MinIO对象存储支持
- 添加MinIO配置属性类MinioProperties - 实现MinIO文件上传服务MinioService - 在CommonController中增加MinIO上传逻辑分支 - 支持单文件和多文件的MinIO上传处理 - 保留原有本地文件上传作为备选方案- 添加MinIO Java SDK依赖到框架模块 - 移除重复的Spring Context依赖声明
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package com.ruoyi.framework.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "minio")
|
||||
public class MinioProperties {
|
||||
/** 是否启用MinIO上传 */
|
||||
private boolean enabled = true;
|
||||
/** MinIO服务端地址,例如 http://127.0.0.1:9000 */
|
||||
private String endpoint;
|
||||
/** 访问key */
|
||||
private String accessKey;
|
||||
/** 密钥 */
|
||||
private String secretKey;
|
||||
/** 桶名称 */
|
||||
private String bucket;
|
||||
/** 是否使用https */
|
||||
private boolean secure = false;
|
||||
|
||||
public boolean isEnabled() { return enabled; }
|
||||
public void setEnabled(boolean enabled) { this.enabled = enabled; }
|
||||
|
||||
public String getEndpoint() { return endpoint; }
|
||||
public void setEndpoint(String endpoint) { this.endpoint = endpoint; }
|
||||
|
||||
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 getBucket() { return bucket; }
|
||||
public void setBucket(String bucket) { this.bucket = bucket; }
|
||||
|
||||
public boolean isSecure() { return secure; }
|
||||
public void setSecure(boolean secure) { this.secure = secure; }
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.ruoyi.framework.service;
|
||||
|
||||
import com.ruoyi.framework.config.MinioProperties;
|
||||
import com.ruoyi.common.utils.file.FileUploadUtils;
|
||||
import io.minio.BucketExistsArgs;
|
||||
import io.minio.MakeBucketArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@Service
|
||||
public class MinioService {
|
||||
|
||||
private final MinioClient minioClient;
|
||||
private final MinioProperties properties;
|
||||
|
||||
@Autowired
|
||||
public MinioService(MinioProperties properties) {
|
||||
this.properties = properties;
|
||||
this.minioClient = MinioClient.builder()
|
||||
.endpoint(properties.getEndpoint())
|
||||
.credentials(properties.getAccessKey(), properties.getSecretKey())
|
||||
.build();
|
||||
}
|
||||
|
||||
public UploadResult upload(MultipartFile file) throws Exception {
|
||||
ensureBucket();
|
||||
String objectName = FileUploadUtils.extractFilename(file);
|
||||
try (InputStream is = file.getInputStream()) {
|
||||
minioClient.putObject(
|
||||
PutObjectArgs.builder()
|
||||
.bucket(properties.getBucket())
|
||||
.object(objectName)
|
||||
.stream(is, file.getSize(), -1)
|
||||
.contentType(file.getContentType())
|
||||
.build()
|
||||
);
|
||||
}
|
||||
String url = buildObjectUrl(objectName);
|
||||
return new UploadResult(objectName, url);
|
||||
}
|
||||
|
||||
public String buildObjectUrl(String objectName) {
|
||||
String endpoint = properties.getEndpoint();
|
||||
if (endpoint.endsWith("/")) {
|
||||
endpoint = endpoint.substring(0, endpoint.length() - 1);
|
||||
}
|
||||
return endpoint + "/" + properties.getBucket() + "/" + objectName;
|
||||
}
|
||||
|
||||
private void ensureBucket() throws Exception {
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder()
|
||||
.bucket(properties.getBucket())
|
||||
.build());
|
||||
if (!exists) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder()
|
||||
.bucket(properties.getBucket())
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
public static class UploadResult {
|
||||
private final String objectName;
|
||||
private final String url;
|
||||
|
||||
public UploadResult(String objectName, String url) {
|
||||
this.objectName = objectName;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getObjectName() { return objectName; }
|
||||
public String getUrl() { return url; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user