mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-11-12 12:13:50 +08:00
update 使用 策略+工厂 重写OSS模块
This commit is contained in:
@@ -2,8 +2,18 @@ package com.ruoyi.oss.service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 云存储服务接口
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
public interface ICloudStorageService {
|
||||
|
||||
/**
|
||||
* 获取服务商类型
|
||||
*/
|
||||
String getServiceType();
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*
|
||||
|
||||
@@ -1,36 +1,55 @@
|
||||
package com.ruoyi.oss.service.abstractd;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.service.ICloudStorageService;
|
||||
import com.ruoyi.oss.utils.DateUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 云存储(支持七牛、阿里云、腾讯云、minio)
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
public abstract class AbstractCloudStorageService implements ICloudStorageService {
|
||||
public abstract class AbstractCloudStorageService implements ICloudStorageService, InitializingBean {
|
||||
|
||||
/**
|
||||
* 云存储配置信息
|
||||
*/
|
||||
protected CloudStorageConfig config;
|
||||
|
||||
public int getServiceType() {
|
||||
return config.getType();
|
||||
}
|
||||
@Override
|
||||
public abstract String getServiceType();
|
||||
|
||||
@Override
|
||||
public String getPath(String prefix, String suffix) {
|
||||
// 生成uuid
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
String uuid = IdUtil.fastSimpleUUID();
|
||||
// 文件路径
|
||||
String path = DateUtils.dateTime() + "/" + uuid;
|
||||
String path = DateUtil.format(new Date(), "yyyyMMdd") + "/" + uuid;
|
||||
if (StrUtil.isNotBlank(prefix)) {
|
||||
path = prefix + "/" + path;
|
||||
}
|
||||
return path + suffix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String upload(byte[] data, String path);
|
||||
|
||||
@Override
|
||||
public abstract void delete(String path);
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String uploadSuffix(byte[] data, String suffix);
|
||||
|
||||
@Override
|
||||
public abstract String uploadSuffix(InputStream inputStream, String suffix);
|
||||
|
||||
@Override
|
||||
public abstract void afterPropertiesSet() throws Exception;
|
||||
}
|
||||
|
||||
@@ -1,28 +1,47 @@
|
||||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import com.aliyun.oss.ClientConfiguration;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
||||
import com.ruoyi.oss.enumd.CloudServiceEnumd;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
import com.ruoyi.oss.properties.AliyunProperties;
|
||||
import com.ruoyi.oss.properties.CloudStorageProperties;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 阿里云存储
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
@Lazy
|
||||
@Service
|
||||
public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean {
|
||||
|
||||
private OSSClient client;
|
||||
private final OSSClient client;
|
||||
private final AliyunProperties properties;
|
||||
|
||||
public AliyunCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
@Autowired
|
||||
public AliyunCloudStorageServiceImpl(CloudStorageProperties properties) {
|
||||
this.properties = properties.getAliyun();
|
||||
ClientConfiguration configuration = new ClientConfiguration();
|
||||
DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(
|
||||
this.properties.getAccessKeyId(),
|
||||
this.properties.getAccessKeySecret());
|
||||
client = new OSSClient(this.properties.getEndpoint(), credentialProvider, configuration);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
client = new OSSClient(config.getDomain(), config.getAccessKey(), config.getSecretKey());
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return CloudServiceEnumd.ALIYUN.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,18 +52,18 @@ public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
client.putObject(config.getBucketName(), path, inputStream);
|
||||
client.putObject(this.properties.getBucketName(), path, inputStream);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查配置信息");
|
||||
}
|
||||
return config.getDomain() + "/" + path;
|
||||
return this.properties.getEndpoint() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(config.getDomain() + "/", "");
|
||||
path = path.replace(this.properties.getEndpoint() + "/", "");
|
||||
try {
|
||||
client.deleteObject(config.getBucketName(), path);
|
||||
client.deleteObject(this.properties.getBucketName(), path);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查配置信息");
|
||||
}
|
||||
@@ -52,11 +71,16 @@ public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
return upload(data, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
OssFactory.register(getServiceType(),this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,43 @@
|
||||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.enumd.CloudServiceEnumd;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
import com.ruoyi.oss.properties.CloudStorageProperties;
|
||||
import com.ruoyi.oss.properties.MinioProperties;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
import io.minio.MinioClient;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* minio存储
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
@Lazy
|
||||
@Service
|
||||
public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean {
|
||||
|
||||
private MinioClient minioClient;
|
||||
private final MinioClient minioClient;
|
||||
private final MinioProperties properties;
|
||||
|
||||
public MinioCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
@Autowired
|
||||
public MinioCloudStorageServiceImpl(CloudStorageProperties properties) {
|
||||
this.properties = properties.getMinio();
|
||||
minioClient = MinioClient.builder()
|
||||
.endpoint(this.properties.getEndpoint())
|
||||
.credentials(this.properties.getAccessKey(), this.properties.getSecretKey())
|
||||
.build();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
minioClient = MinioClient.builder()
|
||||
.endpoint(config.getDomain())
|
||||
.credentials(config.getAccessKey(), config.getSecretKey())
|
||||
.build();
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return CloudServiceEnumd.MINIO.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -35,7 +47,7 @@ public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对Minio配置信息");
|
||||
}
|
||||
return config.getDomain() + "/" + path;
|
||||
return this.properties.getEndpoint() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -47,20 +59,18 @@ public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
return upload(data, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
OssFactory.register(getServiceType(),this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,50 @@
|
||||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.qcloud.cos.ClientConfig;
|
||||
import com.qcloud.cos.auth.BasicCOSCredentials;
|
||||
import com.qcloud.cos.auth.COSCredentials;
|
||||
import com.qcloud.cos.region.Region;
|
||||
import com.ruoyi.oss.enumd.CloudServiceEnumd;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
import com.ruoyi.oss.properties.CloudStorageProperties;
|
||||
import com.ruoyi.oss.properties.QcloudProperties;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 腾讯云存储
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
@Lazy
|
||||
@Service
|
||||
public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean {
|
||||
|
||||
private COSClient client;
|
||||
private final COSClient client;
|
||||
private final QcloudProperties properties;
|
||||
|
||||
public QcloudCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
@Autowired
|
||||
public QcloudCloudStorageServiceImpl(CloudStorageProperties properties) {
|
||||
this.properties = properties.getQcloud();
|
||||
COSCredentials credentials = new BasicCOSCredentials(
|
||||
this.properties.getSecretId(),
|
||||
this.properties.getSecretKey());
|
||||
// 初始化客户端配置
|
||||
ClientConfig clientConfig = new ClientConfig();
|
||||
// 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
|
||||
clientConfig.setRegion(new Region(this.properties.getRegion()));
|
||||
client = new COSClient(credentials, clientConfig);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(),
|
||||
// config.getQcloudSecretKey());
|
||||
// 初始化客户端配置
|
||||
// ClientConfig clientConfig = new ClientConfig();
|
||||
// // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
|
||||
// clientConfig.setRegion(config.getQcloudRegion());
|
||||
// client = new COSClient(clientConfig, credentials);
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return CloudServiceEnumd.QCLOUD.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,7 +60,7 @@ public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
// if (Convert.toInt(jsonObject.get("code")) != 0) {
|
||||
// throw new OssException("文件上传失败," + Convert.toStr(jsonObject.get("message")));
|
||||
// }
|
||||
return config.getDomain() + path;
|
||||
return this.properties.getDomain() + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,21 +74,18 @@ public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
return upload(data, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
OssFactory.register(getServiceType(),this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,59 @@
|
||||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.Region;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.util.Auth;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.enumd.CloudServiceEnumd;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
import com.ruoyi.oss.properties.CloudStorageProperties;
|
||||
import com.ruoyi.oss.properties.QiniuProperties;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 七牛云存储
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
@Lazy
|
||||
@Service
|
||||
public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean {
|
||||
|
||||
private UploadManager uploadManager;
|
||||
private BucketManager bucketManager;
|
||||
private String token;
|
||||
private final UploadManager uploadManager;
|
||||
private final BucketManager bucketManager;
|
||||
private final String token;
|
||||
private final QiniuProperties properties;
|
||||
|
||||
public QiniuCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
@Autowired
|
||||
public QiniuCloudStorageServiceImpl(CloudStorageProperties properties) {
|
||||
this.properties = properties.getQiniu();
|
||||
// z0 z1 z2
|
||||
Configuration config = new Configuration(Region.autoRegion());
|
||||
// 默认不使用https
|
||||
config.useHttpsDomains = false;
|
||||
uploadManager = new UploadManager(config);
|
||||
Auth auth = Auth.create(this.config.getAccessKey(), this.config.getSecretKey());
|
||||
token = auth.uploadToken(this.config.getBucketName());
|
||||
Auth auth = Auth.create(
|
||||
this.properties.getAccessKey(),
|
||||
this.properties.getSecretKey());
|
||||
token = auth.uploadToken(this.properties.getBucketName());
|
||||
bucketManager = new BucketManager(auth, config);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return CloudServiceEnumd.QINIU.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
try {
|
||||
@@ -49,14 +64,14 @@ public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对七牛配置信息");
|
||||
}
|
||||
return config.getDomain() + "/" + path;
|
||||
return this.properties.getDomain() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
try {
|
||||
path = path.replace(config.getDomain() + "/", "");
|
||||
Response res = bucketManager.delete(config.getBucketName(), path);
|
||||
path = path.replace(this.properties.getDomain() + "/", "");
|
||||
Response res = bucketManager.delete(this.properties.getBucketName(), path);
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("删除七牛文件出错:" + res.toString());
|
||||
}
|
||||
@@ -65,20 +80,19 @@ public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
return upload(data, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
OssFactory.register(getServiceType(),this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user