mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-24 07:19:46 +08:00
update 格式化代码 统一间隔符
update 格式化代码 统一间隔符
This commit is contained in:
@ -10,29 +10,29 @@ import java.util.List;
|
||||
*/
|
||||
public class OssConstant {
|
||||
|
||||
/**
|
||||
* OSS模块KEY
|
||||
*/
|
||||
public static final String SYS_OSS_KEY = "sys_oss:";
|
||||
/**
|
||||
* OSS模块KEY
|
||||
*/
|
||||
public static final String SYS_OSS_KEY = "sys_oss:";
|
||||
|
||||
/**
|
||||
* 对象存储配置KEY
|
||||
*/
|
||||
public static final String OSS_CONFIG_KEY = "OssConfig";
|
||||
/**
|
||||
* 对象存储配置KEY
|
||||
*/
|
||||
public static final String OSS_CONFIG_KEY = "OssConfig";
|
||||
|
||||
/**
|
||||
* 缓存配置KEY
|
||||
*/
|
||||
public static final String CACHE_CONFIG_KEY = SYS_OSS_KEY + OSS_CONFIG_KEY;
|
||||
/**
|
||||
* 缓存配置KEY
|
||||
*/
|
||||
public static final String CACHE_CONFIG_KEY = SYS_OSS_KEY + OSS_CONFIG_KEY;
|
||||
|
||||
/**
|
||||
* 预览列表资源开关Key
|
||||
*/
|
||||
public static final String PEREVIEW_LIST_RESOURCE_KEY = "sys.oss.previewListResource";
|
||||
/**
|
||||
* 预览列表资源开关Key
|
||||
*/
|
||||
public static final String PEREVIEW_LIST_RESOURCE_KEY = "sys.oss.previewListResource";
|
||||
|
||||
/**
|
||||
* 系统数据ids
|
||||
*/
|
||||
public static final List<Integer> SYSTEM_DATA_IDS = Arrays.asList(1, 2, 3, 4);
|
||||
/**
|
||||
* 系统数据ids
|
||||
*/
|
||||
public static final List<Integer> SYSTEM_DATA_IDS = Arrays.asList(1, 2, 3, 4);
|
||||
|
||||
}
|
||||
|
@ -14,13 +14,13 @@ import lombok.experimental.Accessors;
|
||||
@Accessors(chain = true)
|
||||
public class UploadResult {
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 文件路径
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String filename;
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String filename;
|
||||
}
|
||||
|
@ -17,47 +17,47 @@ import lombok.Getter;
|
||||
@AllArgsConstructor
|
||||
public enum OssEnumd {
|
||||
|
||||
/**
|
||||
* 七牛云
|
||||
*/
|
||||
QINIU("qiniu", QiniuOssStrategy.class),
|
||||
/**
|
||||
* 七牛云
|
||||
*/
|
||||
QINIU("qiniu", QiniuOssStrategy.class),
|
||||
|
||||
/**
|
||||
* 阿里云
|
||||
*/
|
||||
ALIYUN("aliyun", AliyunOssStrategy.class),
|
||||
/**
|
||||
* 阿里云
|
||||
*/
|
||||
ALIYUN("aliyun", AliyunOssStrategy.class),
|
||||
|
||||
/**
|
||||
* 腾讯云
|
||||
*/
|
||||
QCLOUD("qcloud", QcloudOssStrategy.class),
|
||||
/**
|
||||
* 腾讯云
|
||||
*/
|
||||
QCLOUD("qcloud", QcloudOssStrategy.class),
|
||||
|
||||
/**
|
||||
* minio
|
||||
*/
|
||||
MINIO("minio", MinioOssStrategy.class);
|
||||
/**
|
||||
* minio
|
||||
*/
|
||||
MINIO("minio", MinioOssStrategy.class);
|
||||
|
||||
private final String value;
|
||||
private final String value;
|
||||
|
||||
private final Class<?> serviceClass;
|
||||
private final Class<?> serviceClass;
|
||||
|
||||
public static Class<?> getServiceClass(String value) {
|
||||
for (OssEnumd clazz : values()) {
|
||||
if (clazz.getValue().equals(value)) {
|
||||
return clazz.getServiceClass();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static Class<?> getServiceClass(String value) {
|
||||
for (OssEnumd clazz : values()) {
|
||||
if (clazz.getValue().equals(value)) {
|
||||
return clazz.getServiceClass();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getServiceName(String value) {
|
||||
for (OssEnumd clazz : values()) {
|
||||
if (clazz.getValue().equals(value)) {
|
||||
return StringUtils.uncapitalize(clazz.getServiceClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String getServiceName(String value) {
|
||||
for (OssEnumd clazz : values()) {
|
||||
if (clazz.getValue().equals(value)) {
|
||||
return StringUtils.uncapitalize(clazz.getServiceClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -28,24 +28,24 @@ import lombok.Getter;
|
||||
@AllArgsConstructor
|
||||
public enum PolicyType {
|
||||
|
||||
/**
|
||||
* 只读
|
||||
*/
|
||||
READ("read-only"),
|
||||
/**
|
||||
* 只读
|
||||
*/
|
||||
READ("read-only"),
|
||||
|
||||
/**
|
||||
* 只写
|
||||
*/
|
||||
WRITE("write-only"),
|
||||
/**
|
||||
* 只写
|
||||
*/
|
||||
WRITE("write-only"),
|
||||
|
||||
/**
|
||||
* 读写
|
||||
*/
|
||||
READ_WRITE("read-write");
|
||||
/**
|
||||
* 读写
|
||||
*/
|
||||
READ_WRITE("read-write");
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private final String type;
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private final String type;
|
||||
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.ruoyi.oss.exception;
|
||||
*/
|
||||
public class OssException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public OssException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
public OssException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,10 +23,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
@Slf4j
|
||||
public class OssFactory {
|
||||
|
||||
/**
|
||||
* 服务实例缓存
|
||||
*/
|
||||
private static final Map<String, IOssStrategy> SERVICES = new ConcurrentHashMap<>();
|
||||
/**
|
||||
* 服务实例缓存
|
||||
*/
|
||||
private static final Map<String, IOssStrategy> SERVICES = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 初始化工厂
|
||||
@ -42,40 +42,40 @@ public class OssFactory {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认实例
|
||||
*/
|
||||
public static IOssStrategy instance() {
|
||||
// 获取redis 默认类型
|
||||
String type = RedisUtils.getCacheObject(OssConstant.CACHE_CONFIG_KEY);
|
||||
if (StringUtils.isEmpty(type)) {
|
||||
throw new OssException("文件存储服务类型无法找到!");
|
||||
}
|
||||
return instance(type);
|
||||
}
|
||||
/**
|
||||
* 获取默认实例
|
||||
*/
|
||||
public static IOssStrategy instance() {
|
||||
// 获取redis 默认类型
|
||||
String type = RedisUtils.getCacheObject(OssConstant.CACHE_CONFIG_KEY);
|
||||
if (StringUtils.isEmpty(type)) {
|
||||
throw new OssException("文件存储服务类型无法找到!");
|
||||
}
|
||||
return instance(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据类型获取实例
|
||||
*/
|
||||
public static IOssStrategy instance(String type) {
|
||||
/**
|
||||
* 根据类型获取实例
|
||||
*/
|
||||
public static IOssStrategy instance(String type) {
|
||||
IOssStrategy service = SERVICES.get(type);
|
||||
if (service == null) {
|
||||
refreshService(type);
|
||||
service = SERVICES.get(type);
|
||||
}
|
||||
return service;
|
||||
}
|
||||
if (service == null) {
|
||||
refreshService(type);
|
||||
service = SERVICES.get(type);
|
||||
}
|
||||
return service;
|
||||
}
|
||||
|
||||
private static void refreshService(String type) {
|
||||
Object json = RedisUtils.getCacheObject(OssConstant.SYS_OSS_KEY + type);
|
||||
private static void refreshService(String type) {
|
||||
Object json = RedisUtils.getCacheObject(OssConstant.SYS_OSS_KEY + type);
|
||||
OssProperties properties = JsonUtils.parseObject(json.toString(), OssProperties.class);
|
||||
if (properties == null) {
|
||||
throw new OssException("系统异常, '" + type + "'配置信息不存在!");
|
||||
}
|
||||
// 获取redis配置信息 创建对象 并缓存
|
||||
if (properties == null) {
|
||||
throw new OssException("系统异常, '" + type + "'配置信息不存在!");
|
||||
}
|
||||
// 获取redis配置信息 创建对象 并缓存
|
||||
IOssStrategy service = (IOssStrategy) ReflectUtils.newInstance(OssEnumd.getServiceClass(type));
|
||||
((AbstractOssStrategy)service).init(properties);
|
||||
SERVICES.put(type, service);
|
||||
}
|
||||
((AbstractOssStrategy)service).init(properties);
|
||||
SERVICES.put(type, service);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,39 +10,39 @@ import lombok.Data;
|
||||
@Data
|
||||
public class OssProperties {
|
||||
|
||||
/**
|
||||
* 域名
|
||||
*/
|
||||
private String endpoint;
|
||||
/**
|
||||
* 域名
|
||||
*/
|
||||
private String endpoint;
|
||||
|
||||
/**
|
||||
* 前缀
|
||||
*/
|
||||
private String prefix;
|
||||
/**
|
||||
* 前缀
|
||||
*/
|
||||
private String prefix;
|
||||
|
||||
/**
|
||||
* ACCESS_KEY
|
||||
*/
|
||||
private String accessKey;
|
||||
/**
|
||||
* ACCESS_KEY
|
||||
*/
|
||||
private String accessKey;
|
||||
|
||||
/**
|
||||
* SECRET_KEY
|
||||
*/
|
||||
private String secretKey;
|
||||
/**
|
||||
* SECRET_KEY
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* 存储空间名
|
||||
*/
|
||||
private String bucketName;
|
||||
/**
|
||||
* 存储空间名
|
||||
*/
|
||||
private String bucketName;
|
||||
|
||||
/**
|
||||
* 存储区域
|
||||
*/
|
||||
private String region;
|
||||
/**
|
||||
* 存储区域
|
||||
*/
|
||||
private String region;
|
||||
|
||||
/**
|
||||
* 是否https(Y=是,N=否)
|
||||
*/
|
||||
private String isHttps;
|
||||
/**
|
||||
* 是否https(Y=是,N=否)
|
||||
*/
|
||||
private String isHttps;
|
||||
|
||||
}
|
||||
|
@ -11,54 +11,54 @@ import java.io.InputStream;
|
||||
*/
|
||||
public interface IOssStrategy {
|
||||
|
||||
void createBucket();
|
||||
void createBucket();
|
||||
|
||||
/**
|
||||
* 获取服务商类型
|
||||
*/
|
||||
String getServiceType();
|
||||
/**
|
||||
* 获取服务商类型
|
||||
*/
|
||||
String getServiceType();
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param data 文件字节数组
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
UploadResult upload(byte[] data, String path, String contentType);
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param data 文件字节数组
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
UploadResult upload(byte[] data, String path, String contentType);
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param path 文件路径,包含文件名
|
||||
*/
|
||||
void delete(String path);
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param path 文件路径,包含文件名
|
||||
*/
|
||||
void delete(String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param data 文件字节数组
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
UploadResult uploadSuffix(byte[] data, String suffix, String contentType);
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param data 文件字节数组
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
UploadResult uploadSuffix(byte[] data, String suffix, String contentType);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param inputStream 字节流
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
UploadResult upload(InputStream inputStream, String path, String contentType);
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param inputStream 字节流
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
UploadResult upload(InputStream inputStream, String path, String contentType);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param inputStream 字节流
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType);
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param inputStream 字节流
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType);
|
||||
|
||||
}
|
||||
|
@ -17,44 +17,44 @@ import java.io.InputStream;
|
||||
*/
|
||||
public abstract class AbstractOssStrategy implements IOssStrategy {
|
||||
|
||||
protected OssProperties properties;
|
||||
protected OssProperties properties;
|
||||
|
||||
public abstract void init(OssProperties properties);
|
||||
public abstract void init(OssProperties properties);
|
||||
|
||||
@Override
|
||||
public abstract void createBucket();
|
||||
@Override
|
||||
public abstract void createBucket();
|
||||
|
||||
@Override
|
||||
public abstract String getServiceType();
|
||||
@Override
|
||||
public abstract String getServiceType();
|
||||
|
||||
public String getPath(String prefix, String suffix) {
|
||||
// 生成uuid
|
||||
String uuid = IdUtil.fastSimpleUUID();
|
||||
// 文件路径
|
||||
String path = DateUtils.datePath() + "/" + uuid;
|
||||
if (StringUtils.isNotBlank(prefix)) {
|
||||
path = prefix + "/" + path;
|
||||
}
|
||||
return path + suffix;
|
||||
}
|
||||
public String getPath(String prefix, String suffix) {
|
||||
// 生成uuid
|
||||
String uuid = IdUtil.fastSimpleUUID();
|
||||
// 文件路径
|
||||
String path = DateUtils.datePath() + "/" + uuid;
|
||||
if (StringUtils.isNotBlank(prefix)) {
|
||||
path = prefix + "/" + path;
|
||||
}
|
||||
return path + suffix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract UploadResult upload(byte[] data, String path, String contentType);
|
||||
@Override
|
||||
public abstract UploadResult upload(byte[] data, String path, String contentType);
|
||||
|
||||
@Override
|
||||
public abstract void delete(String path);
|
||||
@Override
|
||||
public abstract void delete(String path);
|
||||
|
||||
@Override
|
||||
public UploadResult upload(InputStream inputStream, String path, String contentType) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path, contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult upload(InputStream inputStream, String path, String contentType) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path, contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract UploadResult uploadSuffix(byte[] data, String suffix, String contentType);
|
||||
@Override
|
||||
public abstract UploadResult uploadSuffix(byte[] data, String suffix, String contentType);
|
||||
|
||||
@Override
|
||||
public abstract UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType);
|
||||
@Override
|
||||
public abstract UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType);
|
||||
|
||||
public abstract String getEndpointLink();
|
||||
public abstract String getEndpointLink();
|
||||
}
|
||||
|
@ -24,90 +24,90 @@ import java.io.InputStream;
|
||||
*/
|
||||
public class AliyunOssStrategy extends AbstractOssStrategy {
|
||||
|
||||
private OSSClient client;
|
||||
private OSSClient client;
|
||||
|
||||
@Override
|
||||
public void init(OssProperties cloudStorageProperties) {
|
||||
properties = cloudStorageProperties;
|
||||
try {
|
||||
ClientConfiguration configuration = new ClientConfiguration();
|
||||
DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(
|
||||
properties.getAccessKey(), properties.getSecretKey());
|
||||
client = new OSSClient(properties.getEndpoint(), credentialProvider, configuration);
|
||||
createBucket();
|
||||
} catch (Exception e) {
|
||||
throw new OssException("阿里云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void init(OssProperties cloudStorageProperties) {
|
||||
properties = cloudStorageProperties;
|
||||
try {
|
||||
ClientConfiguration configuration = new ClientConfiguration();
|
||||
DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(
|
||||
properties.getAccessKey(), properties.getSecretKey());
|
||||
client = new OSSClient(properties.getEndpoint(), credentialProvider, configuration);
|
||||
createBucket();
|
||||
} catch (Exception e) {
|
||||
throw new OssException("阿里云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createBucket() {
|
||||
try {
|
||||
String bucketName = properties.getBucketName();
|
||||
if (client.doesBucketExist(bucketName)) {
|
||||
return;
|
||||
}
|
||||
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
|
||||
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
|
||||
client.createBucket(createBucketRequest);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("创建Bucket失败, 请核对阿里云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void createBucket() {
|
||||
try {
|
||||
String bucketName = properties.getBucketName();
|
||||
if (client.doesBucketExist(bucketName)) {
|
||||
return;
|
||||
}
|
||||
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
|
||||
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
|
||||
client.createBucket(createBucketRequest);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("创建Bucket失败, 请核对阿里云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return OssEnumd.ALIYUN.getValue();
|
||||
}
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return OssEnumd.ALIYUN.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult upload(byte[] data, String path, String contentType) {
|
||||
return upload(new ByteArrayInputStream(data), path, contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult upload(byte[] data, String path, String contentType) {
|
||||
return upload(new ByteArrayInputStream(data), path, contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult upload(InputStream inputStream, String path, String contentType) {
|
||||
try {
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentType(contentType);
|
||||
client.putObject(new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata));
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查阿里云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
|
||||
}
|
||||
@Override
|
||||
public UploadResult upload(InputStream inputStream, String path, String contentType) {
|
||||
try {
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentType(contentType);
|
||||
client.putObject(new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata));
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查阿里云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(getEndpointLink() + "/", "");
|
||||
try {
|
||||
client.deleteObject(properties.getBucketName(), path);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查阿里云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(getEndpointLink() + "/" , "");
|
||||
try {
|
||||
client.deleteObject(properties.getBucketName(), path);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查阿里云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
|
||||
return upload(data, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
|
||||
return upload(data, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
|
||||
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
|
||||
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEndpointLink() {
|
||||
String endpoint = properties.getEndpoint();
|
||||
StringBuilder sb = new StringBuilder(endpoint);
|
||||
if (StringUtils.containsAnyIgnoreCase(endpoint, "http://")) {
|
||||
sb.insert(7, properties.getBucketName() + ".");
|
||||
} else if (StringUtils.containsAnyIgnoreCase(endpoint, "https://")) {
|
||||
sb.insert(8, properties.getBucketName() + ".");
|
||||
} else {
|
||||
throw new OssException("Endpoint配置错误");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
@Override
|
||||
public String getEndpointLink() {
|
||||
String endpoint = properties.getEndpoint();
|
||||
StringBuilder sb = new StringBuilder(endpoint);
|
||||
if (StringUtils.containsAnyIgnoreCase(endpoint, "http://")) {
|
||||
sb.insert(7, properties.getBucketName() + ".");
|
||||
} else if (StringUtils.containsAnyIgnoreCase(endpoint, "https://")) {
|
||||
sb.insert(8, properties.getBucketName() + ".");
|
||||
} else {
|
||||
throw new OssException("Endpoint配置错误");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -20,162 +20,162 @@ import java.io.InputStream;
|
||||
*/
|
||||
public class MinioOssStrategy extends AbstractOssStrategy {
|
||||
|
||||
private MinioClient minioClient;
|
||||
private MinioClient minioClient;
|
||||
|
||||
@Override
|
||||
public void init(OssProperties cloudStorageProperties) {
|
||||
properties = cloudStorageProperties;
|
||||
try {
|
||||
minioClient = MinioClient.builder()
|
||||
.endpoint(properties.getEndpoint())
|
||||
.credentials(properties.getAccessKey(), properties.getSecretKey())
|
||||
.build();
|
||||
createBucket();
|
||||
} catch (Exception e) {
|
||||
throw new OssException("Minio存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void init(OssProperties cloudStorageProperties) {
|
||||
properties = cloudStorageProperties;
|
||||
try {
|
||||
minioClient = MinioClient.builder()
|
||||
.endpoint(properties.getEndpoint())
|
||||
.credentials(properties.getAccessKey(), properties.getSecretKey())
|
||||
.build();
|
||||
createBucket();
|
||||
} catch (Exception e) {
|
||||
throw new OssException("Minio存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createBucket() {
|
||||
try {
|
||||
String bucketName = properties.getBucketName();
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
if (exists) {
|
||||
return;
|
||||
}
|
||||
// 不存在就创建桶
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.config(getPolicy(bucketName, PolicyType.READ))
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new OssException("创建Bucket失败, 请核对Minio配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void createBucket() {
|
||||
try {
|
||||
String bucketName = properties.getBucketName();
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
if (exists) {
|
||||
return;
|
||||
}
|
||||
// 不存在就创建桶
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.config(getPolicy(bucketName, PolicyType.READ))
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new OssException("创建Bucket失败, 请核对Minio配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return OssEnumd.MINIO.getValue();
|
||||
}
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return OssEnumd.MINIO.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult upload(byte[] data, String path, String contentType) {
|
||||
return upload(new ByteArrayInputStream(data), path, contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult upload(byte[] data, String path, String contentType) {
|
||||
return upload(new ByteArrayInputStream(data), path, contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult upload(InputStream inputStream, String path, String contentType) {
|
||||
try {
|
||||
minioClient.putObject(PutObjectArgs.builder()
|
||||
.bucket(properties.getBucketName())
|
||||
.object(path)
|
||||
.contentType(StringUtils.blankToDefault(contentType, MediaType.APPLICATION_OCTET_STREAM_VALUE))
|
||||
.stream(inputStream, inputStream.available(), -1)
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对Minio配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
|
||||
}
|
||||
@Override
|
||||
public UploadResult upload(InputStream inputStream, String path, String contentType) {
|
||||
try {
|
||||
minioClient.putObject(PutObjectArgs.builder()
|
||||
.bucket(properties.getBucketName())
|
||||
.object(path)
|
||||
.contentType(StringUtils.blankToDefault(contentType, MediaType.APPLICATION_OCTET_STREAM_VALUE))
|
||||
.stream(inputStream, inputStream.available(), -1)
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对Minio配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(getEndpointLink() + "/", "");
|
||||
try {
|
||||
minioClient.removeObject(RemoveObjectArgs.builder()
|
||||
.bucket(properties.getBucketName())
|
||||
.object(path)
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new OssException(e.getMessage());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(getEndpointLink() + "/", "");
|
||||
try {
|
||||
minioClient.removeObject(RemoveObjectArgs.builder()
|
||||
.bucket(properties.getBucketName())
|
||||
.object(path)
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new OssException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
|
||||
return upload(data, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
|
||||
return upload(data, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
|
||||
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
|
||||
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEndpointLink() {
|
||||
return properties.getEndpoint() + "/" + properties.getBucketName();
|
||||
}
|
||||
@Override
|
||||
public String getEndpointLink() {
|
||||
return properties.getEndpoint() + "/" + properties.getBucketName();
|
||||
}
|
||||
|
||||
private String getPolicy(String bucketName, PolicyType policyType) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("{\n");
|
||||
builder.append(" \"Statement\": [\n");
|
||||
builder.append(" {\n");
|
||||
builder.append(" \"Action\": [\n");
|
||||
if (policyType == PolicyType.WRITE) {
|
||||
builder.append(" \"s3:GetBucketLocation\",\n");
|
||||
builder.append(" \"s3:ListBucketMultipartUploads\"\n");
|
||||
} else if (policyType == PolicyType.READ_WRITE) {
|
||||
builder.append(" \"s3:GetBucketLocation\",\n");
|
||||
builder.append(" \"s3:ListBucket\",\n");
|
||||
builder.append(" \"s3:ListBucketMultipartUploads\"\n");
|
||||
} else {
|
||||
builder.append(" \"s3:GetBucketLocation\"\n");
|
||||
}
|
||||
builder.append(" ],\n");
|
||||
builder.append(" \"Effect\": \"Allow\",\n");
|
||||
builder.append(" \"Principal\": \"*\",\n");
|
||||
builder.append(" \"Resource\": \"arn:aws:s3:::");
|
||||
builder.append(bucketName);
|
||||
builder.append("\"\n");
|
||||
builder.append(" },\n");
|
||||
if (PolicyType.READ.equals(policyType)) {
|
||||
builder.append(" {\n");
|
||||
builder.append(" \"Action\": [\n");
|
||||
builder.append(" \"s3:ListBucket\"\n");
|
||||
builder.append(" ],\n");
|
||||
builder.append(" \"Effect\": \"Deny\",\n");
|
||||
builder.append(" \"Principal\": \"*\",\n");
|
||||
builder.append(" \"Resource\": \"arn:aws:s3:::");
|
||||
builder.append(bucketName);
|
||||
builder.append("\"\n");
|
||||
builder.append(" },\n");
|
||||
}
|
||||
builder.append(" {\n");
|
||||
builder.append(" \"Action\": ");
|
||||
switch (policyType) {
|
||||
case WRITE:
|
||||
builder.append("[\n");
|
||||
builder.append(" \"s3:AbortMultipartUpload\",\n");
|
||||
builder.append(" \"s3:DeleteObject\",\n");
|
||||
builder.append(" \"s3:ListMultipartUploadParts\",\n");
|
||||
builder.append(" \"s3:PutObject\"\n");
|
||||
builder.append(" ],\n");
|
||||
break;
|
||||
case READ_WRITE:
|
||||
builder.append("[\n");
|
||||
builder.append(" \"s3:AbortMultipartUpload\",\n");
|
||||
builder.append(" \"s3:DeleteObject\",\n");
|
||||
builder.append(" \"s3:GetObject\",\n");
|
||||
builder.append(" \"s3:ListMultipartUploadParts\",\n");
|
||||
builder.append(" \"s3:PutObject\"\n");
|
||||
builder.append(" ],\n");
|
||||
break;
|
||||
default:
|
||||
builder.append("\"s3:GetObject\",\n");
|
||||
break;
|
||||
}
|
||||
builder.append(" \"Effect\": \"Allow\",\n");
|
||||
builder.append(" \"Principal\": \"*\",\n");
|
||||
builder.append(" \"Resource\": \"arn:aws:s3:::");
|
||||
builder.append(bucketName);
|
||||
builder.append("/*\"\n");
|
||||
builder.append(" }\n");
|
||||
builder.append(" ],\n");
|
||||
builder.append(" \"Version\": \"2012-10-17\"\n");
|
||||
builder.append("}\n");
|
||||
return builder.toString();
|
||||
}
|
||||
private String getPolicy(String bucketName, PolicyType policyType) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("{\n");
|
||||
builder.append(" \"Statement\": [\n");
|
||||
builder.append(" {\n");
|
||||
builder.append(" \"Action\": [\n");
|
||||
if (policyType == PolicyType.WRITE) {
|
||||
builder.append(" \"s3:GetBucketLocation\",\n");
|
||||
builder.append(" \"s3:ListBucketMultipartUploads\"\n");
|
||||
} else if (policyType == PolicyType.READ_WRITE) {
|
||||
builder.append(" \"s3:GetBucketLocation\",\n");
|
||||
builder.append(" \"s3:ListBucket\",\n");
|
||||
builder.append(" \"s3:ListBucketMultipartUploads\"\n");
|
||||
} else {
|
||||
builder.append(" \"s3:GetBucketLocation\"\n");
|
||||
}
|
||||
builder.append(" ],\n");
|
||||
builder.append(" \"Effect\": \"Allow\",\n");
|
||||
builder.append(" \"Principal\": \"*\",\n");
|
||||
builder.append(" \"Resource\": \"arn:aws:s3:::");
|
||||
builder.append(bucketName);
|
||||
builder.append("\"\n");
|
||||
builder.append(" },\n");
|
||||
if (PolicyType.READ.equals(policyType)) {
|
||||
builder.append(" {\n");
|
||||
builder.append(" \"Action\": [\n");
|
||||
builder.append(" \"s3:ListBucket\"\n");
|
||||
builder.append(" ],\n");
|
||||
builder.append(" \"Effect\": \"Deny\",\n");
|
||||
builder.append(" \"Principal\": \"*\",\n");
|
||||
builder.append(" \"Resource\": \"arn:aws:s3:::");
|
||||
builder.append(bucketName);
|
||||
builder.append("\"\n");
|
||||
builder.append(" },\n");
|
||||
}
|
||||
builder.append(" {\n");
|
||||
builder.append(" \"Action\": ");
|
||||
switch (policyType) {
|
||||
case WRITE:
|
||||
builder.append("[\n");
|
||||
builder.append(" \"s3:AbortMultipartUpload\",\n");
|
||||
builder.append(" \"s3:DeleteObject\",\n");
|
||||
builder.append(" \"s3:ListMultipartUploadParts\",\n");
|
||||
builder.append(" \"s3:PutObject\"\n");
|
||||
builder.append(" ],\n");
|
||||
break;
|
||||
case READ_WRITE:
|
||||
builder.append("[\n");
|
||||
builder.append(" \"s3:AbortMultipartUpload\",\n");
|
||||
builder.append(" \"s3:DeleteObject\",\n");
|
||||
builder.append(" \"s3:GetObject\",\n");
|
||||
builder.append(" \"s3:ListMultipartUploadParts\",\n");
|
||||
builder.append(" \"s3:PutObject\"\n");
|
||||
builder.append(" ],\n");
|
||||
break;
|
||||
default:
|
||||
builder.append("\"s3:GetObject\",\n");
|
||||
break;
|
||||
}
|
||||
builder.append(" \"Effect\": \"Allow\",\n");
|
||||
builder.append(" \"Principal\": \"*\",\n");
|
||||
builder.append(" \"Resource\": \"arn:aws:s3:::");
|
||||
builder.append(bucketName);
|
||||
builder.append("/*\"\n");
|
||||
builder.append(" }\n");
|
||||
builder.append(" ],\n");
|
||||
builder.append(" \"Version\": \"2012-10-17\"\n");
|
||||
builder.append("}\n");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
@ -24,98 +24,98 @@ import java.io.InputStream;
|
||||
*/
|
||||
public class QcloudOssStrategy extends AbstractOssStrategy {
|
||||
|
||||
private COSClient client;
|
||||
private COSClient client;
|
||||
|
||||
@Override
|
||||
public void init(OssProperties cloudStorageProperties) {
|
||||
properties = cloudStorageProperties;
|
||||
try {
|
||||
COSCredentials credentials = new BasicCOSCredentials(
|
||||
properties.getAccessKey(), properties.getSecretKey());
|
||||
// 初始化客户端配置
|
||||
ClientConfig clientConfig = new ClientConfig();
|
||||
// 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
|
||||
clientConfig.setRegion(new Region(properties.getRegion()));
|
||||
if ("Y".equals(properties.getIsHttps())) {
|
||||
clientConfig.setHttpProtocol(HttpProtocol.https);
|
||||
} else {
|
||||
clientConfig.setHttpProtocol(HttpProtocol.http);
|
||||
}
|
||||
client = new COSClient(credentials, clientConfig);
|
||||
createBucket();
|
||||
} catch (Exception e) {
|
||||
throw new OssException("腾讯云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void init(OssProperties cloudStorageProperties) {
|
||||
properties = cloudStorageProperties;
|
||||
try {
|
||||
COSCredentials credentials = new BasicCOSCredentials(
|
||||
properties.getAccessKey(), properties.getSecretKey());
|
||||
// 初始化客户端配置
|
||||
ClientConfig clientConfig = new ClientConfig();
|
||||
// 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
|
||||
clientConfig.setRegion(new Region(properties.getRegion()));
|
||||
if ("Y".equals(properties.getIsHttps())) {
|
||||
clientConfig.setHttpProtocol(HttpProtocol.https);
|
||||
} else {
|
||||
clientConfig.setHttpProtocol(HttpProtocol.http);
|
||||
}
|
||||
client = new COSClient(credentials, clientConfig);
|
||||
createBucket();
|
||||
} catch (Exception e) {
|
||||
throw new OssException("腾讯云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createBucket() {
|
||||
try {
|
||||
String bucketName = properties.getBucketName();
|
||||
if (client.doesBucketExist(bucketName)) {
|
||||
return;
|
||||
}
|
||||
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
|
||||
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
|
||||
client.createBucket(createBucketRequest);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("创建Bucket失败, 请核对腾讯云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void createBucket() {
|
||||
try {
|
||||
String bucketName = properties.getBucketName();
|
||||
if (client.doesBucketExist(bucketName)) {
|
||||
return;
|
||||
}
|
||||
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
|
||||
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
|
||||
client.createBucket(createBucketRequest);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("创建Bucket失败, 请核对腾讯云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return OssEnumd.QCLOUD.getValue();
|
||||
}
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return OssEnumd.QCLOUD.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult upload(byte[] data, String path, String contentType) {
|
||||
return upload(new ByteArrayInputStream(data), path, contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult upload(byte[] data, String path, String contentType) {
|
||||
return upload(new ByteArrayInputStream(data), path, contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult upload(InputStream inputStream, String path, String contentType) {
|
||||
try {
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentType(contentType);
|
||||
client.putObject(new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata));
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查腾讯云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
|
||||
}
|
||||
@Override
|
||||
public UploadResult upload(InputStream inputStream, String path, String contentType) {
|
||||
try {
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentType(contentType);
|
||||
client.putObject(new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata));
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查腾讯云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(getEndpointLink() + "/", "");
|
||||
try {
|
||||
client.deleteObject(new DeleteObjectRequest(properties.getBucketName(), path));
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检腾讯云查配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(getEndpointLink() + "/", "");
|
||||
try {
|
||||
client.deleteObject(new DeleteObjectRequest(properties.getBucketName(), path));
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检腾讯云查配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
|
||||
return upload(data, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
|
||||
return upload(data, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
|
||||
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
|
||||
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEndpointLink() {
|
||||
String endpoint = properties.getEndpoint();
|
||||
StringBuilder sb = new StringBuilder(endpoint);
|
||||
if (StringUtils.containsAnyIgnoreCase(endpoint, "http://")) {
|
||||
sb.insert(7, properties.getBucketName() + ".");
|
||||
} else if (StringUtils.containsAnyIgnoreCase(endpoint, "https://")) {
|
||||
sb.insert(8, properties.getBucketName() + ".");
|
||||
} else {
|
||||
throw new OssException("Endpoint配置错误");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
@Override
|
||||
public String getEndpointLink() {
|
||||
String endpoint = properties.getEndpoint();
|
||||
StringBuilder sb = new StringBuilder(endpoint);
|
||||
if (StringUtils.containsAnyIgnoreCase(endpoint, "http://")) {
|
||||
sb.insert(7, properties.getBucketName() + ".");
|
||||
} else if (StringUtils.containsAnyIgnoreCase(endpoint, "https://")) {
|
||||
sb.insert(8, properties.getBucketName() + ".");
|
||||
} else {
|
||||
throw new OssException("Endpoint配置错误");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -22,106 +22,106 @@ import java.io.InputStream;
|
||||
*/
|
||||
public class QiniuOssStrategy extends AbstractOssStrategy {
|
||||
|
||||
private UploadManager uploadManager;
|
||||
private BucketManager bucketManager;
|
||||
private Auth auth;
|
||||
private UploadManager uploadManager;
|
||||
private BucketManager bucketManager;
|
||||
private Auth auth;
|
||||
|
||||
@Override
|
||||
public void init(OssProperties cloudStorageProperties) {
|
||||
properties = cloudStorageProperties;
|
||||
try {
|
||||
Configuration config = new Configuration(getRegion(properties.getRegion()));
|
||||
// https设置
|
||||
config.useHttpsDomains = false;
|
||||
config.useHttpsDomains = "Y".equals(properties.getIsHttps());
|
||||
uploadManager = new UploadManager(config);
|
||||
auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
|
||||
String bucketName = properties.getBucketName();
|
||||
bucketManager = new BucketManager(auth, config);
|
||||
@Override
|
||||
public void init(OssProperties cloudStorageProperties) {
|
||||
properties = cloudStorageProperties;
|
||||
try {
|
||||
Configuration config = new Configuration(getRegion(properties.getRegion()));
|
||||
// https设置
|
||||
config.useHttpsDomains = false;
|
||||
config.useHttpsDomains = "Y".equals(properties.getIsHttps());
|
||||
uploadManager = new UploadManager(config);
|
||||
auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
|
||||
String bucketName = properties.getBucketName();
|
||||
bucketManager = new BucketManager(auth, config);
|
||||
|
||||
if (!ArrayUtil.contains(bucketManager.buckets(), bucketName)) {
|
||||
bucketManager.createBucket(bucketName, properties.getRegion());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException("七牛云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
if (!ArrayUtil.contains(bucketManager.buckets(), bucketName)) {
|
||||
bucketManager.createBucket(bucketName, properties.getRegion());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException("七牛云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createBucket() {
|
||||
try {
|
||||
String bucketName = properties.getBucketName();
|
||||
if (ArrayUtil.contains(bucketManager.buckets(), bucketName)) {
|
||||
return;
|
||||
}
|
||||
bucketManager.createBucket(bucketName, properties.getRegion());
|
||||
} catch (Exception e) {
|
||||
throw new OssException("创建Bucket失败, 请核对七牛云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void createBucket() {
|
||||
try {
|
||||
String bucketName = properties.getBucketName();
|
||||
if (ArrayUtil.contains(bucketManager.buckets(), bucketName)) {
|
||||
return;
|
||||
}
|
||||
bucketManager.createBucket(bucketName, properties.getRegion());
|
||||
} catch (Exception e) {
|
||||
throw new OssException("创建Bucket失败, 请核对七牛云配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return OssEnumd.QINIU.getValue();
|
||||
}
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return OssEnumd.QINIU.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult upload(byte[] data, String path, String contentType) {
|
||||
try {
|
||||
@Override
|
||||
public UploadResult upload(byte[] data, String path, String contentType) {
|
||||
try {
|
||||
String token = auth.uploadToken(properties.getBucketName());
|
||||
Response res = uploadManager.put(data, path, token, null, contentType, false);
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("上传七牛出错:" + res.error);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对七牛配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
|
||||
}
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("上传七牛出错:" + res.error);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对七牛配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
try {
|
||||
path = path.replace(getEndpointLink() + "/", "");
|
||||
Response res = bucketManager.delete(properties.getBucketName(), path);
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("删除七牛文件出错:" + res.error);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException(e.getMessage());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
try {
|
||||
path = path.replace(getEndpointLink() + "/", "");
|
||||
Response res = bucketManager.delete(properties.getBucketName(), path);
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("删除七牛文件出错:" + res.error);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
|
||||
return upload(data, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
|
||||
return upload(data, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
|
||||
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
@Override
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
|
||||
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEndpointLink() {
|
||||
return properties.getEndpoint();
|
||||
}
|
||||
@Override
|
||||
public String getEndpointLink() {
|
||||
return properties.getEndpoint();
|
||||
}
|
||||
|
||||
private Region getRegion(String region) {
|
||||
switch (region) {
|
||||
case "z0":
|
||||
return Region.region0();
|
||||
case "z1":
|
||||
return Region.region1();
|
||||
case "z2":
|
||||
return Region.region2();
|
||||
case "na0":
|
||||
return Region.regionNa0();
|
||||
case "as0":
|
||||
return Region.regionAs0();
|
||||
default:
|
||||
return Region.autoRegion();
|
||||
}
|
||||
}
|
||||
private Region getRegion(String region) {
|
||||
switch (region) {
|
||||
case "z0":
|
||||
return Region.region0();
|
||||
case "z1":
|
||||
return Region.region1();
|
||||
case "z2":
|
||||
return Region.region2();
|
||||
case "na0":
|
||||
return Region.regionNa0();
|
||||
case "as0":
|
||||
return Region.regionAs0();
|
||||
default:
|
||||
return Region.autoRegion();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user