refactor:oss模块引入

V0.5.x
Longjun.Tu 2023-05-23 19:00:25 +08:00
parent 6da2c1de6e
commit aa2423c7d1
44 changed files with 597 additions and 15 deletions

View File

@ -7,7 +7,7 @@
* | Author: xw2sy@163.com * | Author: xw2sy@163.com
* +---------------------------------------------------------------------- * +----------------------------------------------------------------------
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data;
import cc.iotkit.model.Id; import cc.iotkit.model.Id;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;

View File

@ -7,8 +7,9 @@
* | Author: xw2sy@163.com * | Author: xw2sy@163.com
* +---------------------------------------------------------------------- * +----------------------------------------------------------------------
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.Owned; import cc.iotkit.model.Owned;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;

View File

@ -11,6 +11,7 @@ package cc.iotkit.data.manager;
import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.alert.AlertConfig; import cc.iotkit.model.alert.AlertConfig;

View File

@ -11,6 +11,7 @@ package cc.iotkit.data.manager;
import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.alert.AlertRecord; import cc.iotkit.model.alert.AlertRecord;

View File

@ -9,6 +9,7 @@
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.product.Category; import cc.iotkit.model.product.Category;
public interface ICategoryData extends ICommonData<Category, String> { public interface ICategoryData extends ICommonData<Category, String> {

View File

@ -1,5 +1,6 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.notify.ChannelConfig; import cc.iotkit.model.notify.ChannelConfig;
/** /**

View File

@ -1,5 +1,6 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.notify.Channel; import cc.iotkit.model.notify.Channel;
/** /**

View File

@ -1,5 +1,6 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.notify.ChannelTemplate; import cc.iotkit.model.notify.ChannelTemplate;
/** /**

View File

@ -9,6 +9,7 @@
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.device.DeviceConfig; import cc.iotkit.model.device.DeviceConfig;
public interface IDeviceConfigData extends ICommonData<DeviceConfig, String> { public interface IDeviceConfigData extends ICommonData<DeviceConfig, String> {

View File

@ -10,6 +10,7 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.device.DeviceGroup; import cc.iotkit.model.device.DeviceGroup;
public interface IDeviceGroupData extends ICommonData<DeviceGroup, String> { public interface IDeviceGroupData extends ICommonData<DeviceGroup, String> {

View File

@ -10,6 +10,7 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.device.DeviceInfo; import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.stats.DataItem; import cc.iotkit.model.stats.DataItem;

View File

@ -9,6 +9,7 @@
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.space.Home; import cc.iotkit.model.space.Home;

View File

@ -9,6 +9,7 @@
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.OauthClient; import cc.iotkit.model.OauthClient;
public interface IOauthClientData extends ICommonData<OauthClient, String> { public interface IOauthClientData extends ICommonData<OauthClient, String> {

View File

@ -9,6 +9,7 @@
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.product.Product; import cc.iotkit.model.product.Product;
import java.util.List; import java.util.List;

View File

@ -9,6 +9,7 @@
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.product.ProductModel; import cc.iotkit.model.product.ProductModel;
import java.util.List; import java.util.List;

View File

@ -1,5 +1,6 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.protocol.ProtocolComponent; import cc.iotkit.model.protocol.ProtocolComponent;
import java.util.List; import java.util.List;

View File

@ -1,5 +1,6 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.protocol.ProtocolConverter; import cc.iotkit.model.protocol.ProtocolConverter;
public interface IProtocolConverterData extends IOwnedData<ProtocolConverter, String> { public interface IProtocolConverterData extends IOwnedData<ProtocolConverter, String> {

View File

@ -1,6 +1,7 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.rule.RuleInfo; import cc.iotkit.model.rule.RuleInfo;
import java.util.List; import java.util.List;

View File

@ -9,6 +9,7 @@
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.space.Space; import cc.iotkit.model.space.Space;
import java.util.List; import java.util.List;

View File

@ -9,6 +9,7 @@
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.space.SpaceDevice; import cc.iotkit.model.space.SpaceDevice;
import java.util.List; import java.util.List;

View File

@ -1,5 +1,6 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.rule.TaskInfo; import cc.iotkit.model.rule.TaskInfo;
public interface ITaskInfoData extends IOwnedData<TaskInfo, String> { public interface ITaskInfoData extends IOwnedData<TaskInfo, String> {

View File

@ -1,5 +1,6 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.product.ThingModel; import cc.iotkit.model.product.ThingModel;
public interface IThingModelData extends ICommonData<ThingModel, String> { public interface IThingModelData extends ICommonData<ThingModel, String> {

View File

@ -9,6 +9,7 @@
*/ */
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.UserInfo; import cc.iotkit.model.UserInfo;
import java.util.List; import java.util.List;

View File

@ -10,6 +10,7 @@
package cc.iotkit.data.manager; package cc.iotkit.data.manager;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.data.IOwnedData;
import cc.iotkit.model.device.VirtualDevice; import cc.iotkit.model.device.VirtualDevice;
import java.util.List; import java.util.List;

View File

@ -1,7 +1,6 @@
package cc.iotkit.data.system; package cc.iotkit.data.system;
import cc.iotkit.common.api.Paging; import cc.iotkit.data.ICommonData;
import cc.iotkit.data.manager.ICommonData;
import cc.iotkit.model.system.SysConfig; import cc.iotkit.model.system.SysConfig;
/** /**

View File

@ -1,7 +1,7 @@
package cc.iotkit.data.system; package cc.iotkit.data.system;
import cc.iotkit.data.manager.ICommonData; import cc.iotkit.data.ICommonData;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.model.system.SysDept; import cc.iotkit.model.system.SysDept;

View File

@ -1,6 +1,6 @@
package cc.iotkit.data.system; package cc.iotkit.data.system;
import cc.iotkit.data.manager.ICommonData; import cc.iotkit.data.ICommonData;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.model.system.SysDictData; import cc.iotkit.model.system.SysDictData;
import cc.iotkit.model.system.SysDictType; import cc.iotkit.model.system.SysDictType;

View File

@ -1,6 +1,6 @@
package cc.iotkit.data.system; package cc.iotkit.data.system;
import cc.iotkit.data.manager.ICommonData; import cc.iotkit.data.ICommonData;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.model.system.SysDictType; import cc.iotkit.model.system.SysDictType;

View File

@ -1,6 +1,6 @@
package cc.iotkit.data.system; package cc.iotkit.data.system;
import cc.iotkit.data.manager.ICommonData; import cc.iotkit.data.ICommonData;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.model.system.SysLogininfor; import cc.iotkit.model.system.SysLogininfor;

View File

@ -1,6 +1,6 @@
package cc.iotkit.data.system; package cc.iotkit.data.system;
import cc.iotkit.data.manager.ICommonData; import cc.iotkit.data.ICommonData;
import cc.iotkit.model.system.SysMenu; import cc.iotkit.model.system.SysMenu;
import java.util.List; import java.util.List;

View File

@ -1,6 +1,6 @@
package cc.iotkit.data.system; package cc.iotkit.data.system;
import cc.iotkit.data.manager.ICommonData; import cc.iotkit.data.ICommonData;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.model.system.SysNotice; import cc.iotkit.model.system.SysNotice;

View File

@ -1,6 +1,6 @@
package cc.iotkit.data.system; package cc.iotkit.data.system;
import cc.iotkit.data.manager.ICommonData; import cc.iotkit.data.ICommonData;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import cc.iotkit.model.system.SysOperLog; import cc.iotkit.model.system.SysOperLog;

View File

@ -9,7 +9,7 @@
*/ */
package cc.iotkit.data.service; package cc.iotkit.data.service;
import cc.iotkit.data.manager.ICommonData; import cc.iotkit.data.ICommonData;
import cc.iotkit.model.Id; import cc.iotkit.model.Id;
import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Paging;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;

View File

@ -10,6 +10,22 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>iot-common-oss</artifactId> <artifactId>iot-common-oss</artifactId>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-common-redis</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-common-core</artifactId>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -0,0 +1,38 @@
package cc.iotkit.common.oss.constant;
import java.util.Arrays;
import java.util.List;
/**
*
*
* @author Lion Li
*/
public interface OssConstant {
/**
* KEY
*/
String DEFAULT_CONFIG_KEY = "sys_oss:default_config";
/**
* Key
*/
String PEREVIEW_LIST_RESOURCE_KEY = "sys.oss.previewListResource";
/**
* ids
*/
List<Long> SYSTEM_DATA_IDS = Arrays.asList(1L, 2L, 3L, 4L);
/**
*
*/
String[] CLOUD_SERVICE = new String[] {"aliyun", "qcloud", "qiniu", "obs"};
/**
* https
*/
String IS_HTTPS = "Y";
}

View File

@ -0,0 +1,245 @@
package cc.iotkit.common.oss.core;
import cc.iotkit.common.oss.constant.OssConstant;
import cc.iotkit.common.oss.exception.OssException;
import cc.iotkit.common.oss.properties.OssProperties;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.HttpMethod;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import cc.iotkit.common.oss.entity.UploadResult;
import cc.iotkit.common.oss.enumd.AccessPolicyType;
import cc.iotkit.common.oss.enumd.PolicyType;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
/**
* S3 S3
* minio
*
* @author Lion Li
*/
public class OssClient {
private final String configKey;
private final OssProperties properties;
private final AmazonS3 client;
public OssClient(String configKey, OssProperties ossProperties) {
this.configKey = configKey;
this.properties = ossProperties;
try {
AwsClientBuilder.EndpointConfiguration endpointConfig =
new AwsClientBuilder.EndpointConfiguration(properties.getEndpoint(), properties.getRegion());
AWSCredentials credentials = new BasicAWSCredentials(properties.getAccessKey(), properties.getSecretKey());
AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
ClientConfiguration clientConfig = new ClientConfiguration();
if (OssConstant.IS_HTTPS.equals(properties.getIsHttps())) {
clientConfig.setProtocol(Protocol.HTTPS);
} else {
clientConfig.setProtocol(Protocol.HTTP);
}
AmazonS3ClientBuilder build = AmazonS3Client.builder()
.withEndpointConfiguration(endpointConfig)
.withClientConfiguration(clientConfig)
.withCredentials(credentialsProvider)
.disableChunkedEncoding();
if (!StringUtils.containsAny(properties.getEndpoint(), OssConstant.CLOUD_SERVICE)) {
// minio 使用https限制使用域名访问 需要此配置 站点填域名
build.enablePathStyleAccess();
}
this.client = build.build();
createBucket();
} catch (Exception e) {
if (e instanceof OssException) {
throw e;
}
throw new OssException("配置错误! 请检查系统配置:[" + e.getMessage() + "]");
}
}
public void createBucket() {
try {
String bucketName = properties.getBucketName();
if (client.doesBucketExistV2(bucketName)) {
return;
}
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
AccessPolicyType accessPolicy = getAccessPolicy();
createBucketRequest.setCannedAcl(accessPolicy.getAcl());
client.createBucket(createBucketRequest);
client.setBucketPolicy(bucketName, getPolicy(bucketName, accessPolicy.getPolicyType()));
} catch (Exception e) {
throw new OssException("创建Bucket失败, 请核对配置信息:[" + e.getMessage() + "]");
}
}
public UploadResult upload(byte[] data, String path, String contentType) {
return upload(new ByteArrayInputStream(data), path, contentType);
}
public UploadResult upload(InputStream inputStream, String path, String contentType) {
if (!(inputStream instanceof ByteArrayInputStream)) {
inputStream = new ByteArrayInputStream(IoUtil.readBytes(inputStream));
}
try {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(contentType);
metadata.setContentLength(inputStream.available());
PutObjectRequest putObjectRequest = new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata);
// 设置上传对象的 Acl 为公共读
putObjectRequest.setCannedAcl(getAccessPolicy().getAcl());
client.putObject(putObjectRequest);
} catch (Exception e) {
throw new OssException("上传文件失败,请检查配置信息:[" + e.getMessage() + "]");
}
return UploadResult.builder().url(getUrl() + "/" + path).filename(path).build();
}
public void delete(String path) {
path = path.replace(getUrl() + "/", "");
try {
client.deleteObject(properties.getBucketName(), path);
} catch (Exception e) {
throw new OssException("删除文件失败,请检查配置信息:[" + e.getMessage() + "]");
}
}
public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
return upload(data, getPath(properties.getPrefix(), suffix), contentType);
}
public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
}
/**
*
*
* @param path
*/
public ObjectMetadata getObjectMetadata(String path) {
path = path.replace(getUrl() + "/", "");
S3Object object = client.getObject(properties.getBucketName(), path);
return object.getObjectMetadata();
}
public InputStream getObjectContent(String path) {
path = path.replace(getUrl() + "/", "");
S3Object object = client.getObject(properties.getBucketName(), path);
return object.getObjectContent();
}
public String getUrl() {
String domain = properties.getDomain();
String endpoint = properties.getEndpoint();
String header = OssConstant.IS_HTTPS.equals(properties.getIsHttps()) ? "https://" : "http://";
// 云服务商直接返回
if (StringUtils.containsAny(endpoint, OssConstant.CLOUD_SERVICE)) {
if (StringUtils.isNotBlank(domain)) {
return header + domain;
}
return header + properties.getBucketName() + "." + endpoint;
}
// minio 单独处理
if (StringUtils.isNotBlank(domain)) {
return header + domain + "/" + properties.getBucketName();
}
return header + endpoint + "/" + properties.getBucketName();
}
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 getConfigKey() {
return configKey;
}
/**
* URL
*
* @param objectKey KEY
* @param second
*/
public String getPrivateUrl(String objectKey, Integer second) {
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(properties.getBucketName(), objectKey)
.withMethod(HttpMethod.GET)
.withExpiration(new Date(System.currentTimeMillis() + 1000L * second));
URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString();
}
/**
*
*/
public boolean checkPropertiesSame(OssProperties properties) {
return this.properties.equals(properties);
}
/**
*
*
* @return code
*/
public AccessPolicyType getAccessPolicy() {
return AccessPolicyType.getByType(properties.getAccessPolicy());
}
private static String getPolicy(String bucketName, PolicyType policyType) {
StringBuilder builder = new StringBuilder();
builder.append("{\n\"Statement\": [\n{\n\"Action\": [\n");
builder.append(switch (policyType) {
case WRITE -> "\"s3:GetBucketLocation\",\n\"s3:ListBucketMultipartUploads\"\n";
case READ_WRITE -> "\"s3:GetBucketLocation\",\n\"s3:ListBucket\",\n\"s3:ListBucketMultipartUploads\"\n";
default -> "\"s3:GetBucketLocation\"\n";
});
builder.append("],\n\"Effect\": \"Allow\",\n\"Principal\": \"*\",\n\"Resource\": \"arn:aws:s3:::");
builder.append(bucketName);
builder.append("\"\n},\n");
if (policyType == PolicyType.READ) {
builder.append("{\n\"Action\": [\n\"s3:ListBucket\"\n],\n\"Effect\": \"Deny\",\n\"Principal\": \"*\",\n\"Resource\": \"arn:aws:s3:::");
builder.append(bucketName);
builder.append("\"\n},\n");
}
builder.append("{\n\"Action\": ");
builder.append(switch (policyType) {
case WRITE -> "[\n\"s3:AbortMultipartUpload\",\n\"s3:DeleteObject\",\n\"s3:ListMultipartUploadParts\",\n\"s3:PutObject\"\n],\n";
case READ_WRITE -> "[\n\"s3:AbortMultipartUpload\",\n\"s3:DeleteObject\",\n\"s3:GetObject\",\n\"s3:ListMultipartUploadParts\",\n\"s3:PutObject\"\n],\n";
default -> "\"s3:GetObject\",\n";
});
builder.append("\"Effect\": \"Allow\",\n\"Principal\": \"*\",\n\"Resource\": \"arn:aws:s3:::");
builder.append(bucketName);
builder.append("/*\"\n}\n],\n\"Version\": \"2012-10-17\"\n}\n");
return builder.toString();
}
}

View File

@ -0,0 +1,24 @@
package cc.iotkit.common.oss.entity;
import lombok.Builder;
import lombok.Data;
/**
*
*
* @author Lion Li
*/
@Data
@Builder
public class UploadResult {
/**
*
*/
private String url;
/**
*
*/
private String filename;
}

View File

@ -0,0 +1,55 @@
package cc.iotkit.common.oss.enumd;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 访
*
* @author
*/
@Getter
@AllArgsConstructor
public enum AccessPolicyType {
/**
* private
*/
PRIVATE("0", CannedAccessControlList.Private, PolicyType.WRITE),
/**
* public
*/
PUBLIC("1", CannedAccessControlList.PublicRead, PolicyType.READ),
/**
* custom
*/
CUSTOM("2",CannedAccessControlList.PublicRead, PolicyType.READ);
/**
*
*/
private final String type;
/**
*
*/
private final CannedAccessControlList acl;
/**
*
*/
private final PolicyType policyType;
public static AccessPolicyType getByType(String type) {
for (AccessPolicyType value : values()) {
if (value.getType().equals(type)) {
return value;
}
}
throw new RuntimeException("'type' not found By " + type);
}
}

View File

@ -0,0 +1,35 @@
package cc.iotkit.common.oss.enumd;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* minio
*
* @author Lion Li
*/
@Getter
@AllArgsConstructor
public enum PolicyType {
/**
*
*/
READ("read-only"),
/**
*
*/
WRITE("write-only"),
/**
*
*/
READ_WRITE("read-write");
/**
*
*/
private final String type;
}

View File

@ -0,0 +1,18 @@
package cc.iotkit.common.oss.exception;
/**
* OSS
*
* @author Lion Li
*/
public class OssException extends RuntimeException {
private static final long serialVersionUID = 1L;
public OssException(String msg) {
super(msg);
}
}

View File

@ -0,0 +1,64 @@
package cc.iotkit.common.oss.factory;
import cc.iotkit.common.constant.CacheNames;
import cc.iotkit.common.oss.constant.OssConstant;
import cc.iotkit.common.oss.core.OssClient;
import cc.iotkit.common.oss.exception.OssException;
import cc.iotkit.common.oss.properties.OssProperties;
import cc.iotkit.common.redis.utils.CacheUtils;
import cc.iotkit.common.redis.utils.RedisUtils;
import cc.iotkit.common.utils.JsonUtils;
import cc.iotkit.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Factory
*
* @author Lion Li
*/
@Slf4j
public class OssFactory {
private static final Map<String, OssClient> CLIENT_CACHE = new ConcurrentHashMap<>();
/**
*
*/
public static OssClient instance() {
// 获取redis 默认类型
String configKey = RedisUtils.getCacheObject(OssConstant.DEFAULT_CONFIG_KEY);
if (StringUtils.isEmpty(configKey)) {
throw new OssException("文件存储服务类型无法找到!");
}
return instance(configKey);
}
/**
*
*/
public static OssClient instance(String configKey) {
String json = CacheUtils.get(CacheNames.SYS_OSS_CONFIG, configKey);
if (json == null) {
throw new OssException("系统异常, '" + configKey + "'配置信息不存在!");
}
OssProperties properties = JsonUtils.parseObject(json, OssProperties.class);
OssClient client = CLIENT_CACHE.get(configKey);
if (client == null) {
CLIENT_CACHE.put(configKey, new OssClient(configKey, properties));
log.info("创建OSS实例 key => {}", configKey);
return CLIENT_CACHE.get(configKey);
}
// 配置不相同则重新构建
if (!client.checkPropertiesSame(properties)) {
CLIENT_CACHE.put(configKey, new OssClient(configKey, properties));
log.info("重载OSS实例 key => {}", configKey);
return CLIENT_CACHE.get(configKey);
}
return client;
}
}

View File

@ -0,0 +1,58 @@
package cc.iotkit.common.oss.properties;
import lombok.Data;
/**
* OSS
*
* @author Lion Li
*/
@Data
public class OssProperties {
/**
* 访
*/
private String endpoint;
/**
*
*/
private String domain;
/**
*
*/
private String prefix;
/**
* ACCESS_KEY
*/
private String accessKey;
/**
* SECRET_KEY
*/
private String secretKey;
/**
*
*/
private String bucketName;
/**
*
*/
private String region;
/**
* httpsY=,N=
*/
private String isHttps;
/**
* (0private 1public 2custom)
*/
private String accessPolicy;
}

View File

@ -11,7 +11,7 @@ package cc.iotkit.manager.service;
import cc.iotkit.common.enums.ErrCode; import cc.iotkit.common.enums.ErrCode;
import cc.iotkit.common.exception.BizException; import cc.iotkit.common.exception.BizException;
import cc.iotkit.data.manager.ICommonData; import cc.iotkit.data.ICommonData;
import cc.iotkit.model.Owned; import cc.iotkit.model.Owned;
import cc.iotkit.model.device.DeviceInfo; import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.utils.AuthUtil; import cc.iotkit.utils.AuthUtil;

View File

@ -29,6 +29,7 @@
<beanutils.version>1.9.3</beanutils.version> <beanutils.version>1.9.3</beanutils.version>
<jackson.version>2.13.1</jackson.version> <jackson.version>2.13.1</jackson.version>
<lang3.version>3.7</lang3.version> <lang3.version>3.7</lang3.version>
<aws-java-sdk-s3.version>1.12.400</aws-java-sdk-s3.version>
<io.version>2.11.0</io.version> <io.version>2.11.0</io.version>
<codec.version>1.14</codec.version> <codec.version>1.14</codec.version>
<hutool.version>5.8.18</hutool.version> <hutool.version>5.8.18</hutool.version>
@ -71,6 +72,12 @@
<version>${lang3.version}</version> <version>${lang3.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>${aws-java-sdk-s3.version}</version>
</dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>