一些调整、以及 AWS S3 2.x API 引入
This commit is contained in:
@@ -11,7 +11,8 @@ public enum StorageTypeEnum implements BasicEnum<Integer> {
|
||||
|
||||
LOCAL(0, "本地"),
|
||||
ONE_DRIVE(1, "OneDrive"),
|
||||
ALIYUN_OSS(2, "阿里云OSS");
|
||||
ALIYUN_OSS(2, "阿里云OSS"),
|
||||
AMAZON_S3(3, "Amazon S3");
|
||||
|
||||
private final int value;
|
||||
private final String description;
|
||||
|
||||
@@ -10,10 +10,7 @@ import lombok.Data;
|
||||
@Data
|
||||
public abstract class FileInitParam {
|
||||
|
||||
/**
|
||||
* 获取挂载路径
|
||||
* @return 挂载路径
|
||||
*/
|
||||
public abstract String getMountPath();
|
||||
/** 挂载路径 */
|
||||
private String mountPath;
|
||||
|
||||
}
|
||||
|
||||
@@ -23,18 +23,4 @@ public class AliYunOssParam extends OssParam {
|
||||
/** AccessKeySecret 是用户用于加密签名字符串和 OSS 用来验证签名字符串的密钥,必须保密 */
|
||||
private String accessKeySecret;
|
||||
|
||||
/** 阿里云 OSS Bucket 名称 */
|
||||
private String bucketName;
|
||||
|
||||
/** 挂载路径 */
|
||||
private String mountPath;
|
||||
|
||||
/**
|
||||
* 获取挂载路径
|
||||
* @return 挂载路径
|
||||
*/
|
||||
public String getMountPath() {
|
||||
return mountPath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.besscroft.diyfile.common.param.storage.init;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* @Description Amazon S3 初始化参数
|
||||
* @Author Bess Croft
|
||||
* @Date 2023/3/10 23:32
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class AmazonS3Param extends S3Param {
|
||||
|
||||
/** 访问密钥 */
|
||||
private String accessKey;
|
||||
|
||||
/** 机密密钥 */
|
||||
private String secretKey;
|
||||
|
||||
/** 地域 */
|
||||
private String region;
|
||||
|
||||
/** endpoint 端点 */
|
||||
private String endpoint;
|
||||
|
||||
}
|
||||
@@ -14,16 +14,4 @@ import lombok.EqualsAndHashCode;
|
||||
@Builder
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class LocalParam extends FileInitParam {
|
||||
|
||||
/** 挂载路径 */
|
||||
private String mountPath;
|
||||
|
||||
/**
|
||||
* 获取挂载路径
|
||||
* @return 挂载路径
|
||||
*/
|
||||
public String getMountPath() {
|
||||
return mountPath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,9 +30,6 @@ public class OneDriveParam extends FileInitParam {
|
||||
/** 刷新令牌 */
|
||||
private String refreshToken;
|
||||
|
||||
/** 挂载路径 */
|
||||
private String mountPath;
|
||||
|
||||
/** 代理地址 */
|
||||
private String proxyUrl;
|
||||
|
||||
|
||||
@@ -13,12 +13,7 @@ import lombok.EqualsAndHashCode;
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class OssParam extends FileInitParam {
|
||||
|
||||
/**
|
||||
* 获取挂载路径
|
||||
* @return 挂载路径
|
||||
*/
|
||||
public String getMountPath() {
|
||||
return null;
|
||||
}
|
||||
/** Bucket 名称 */
|
||||
private String bucketName;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.besscroft.diyfile.common.param.storage.init;
|
||||
|
||||
import com.besscroft.diyfile.common.param.FileInitParam;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* @Description S3 初始化参数
|
||||
* @Author Bess Croft
|
||||
* @Date 2023/3/10 23:27
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class S3Param extends FileInitParam {
|
||||
|
||||
/** Bucket 名称 */
|
||||
private String bucketName;
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.besscroft.diyfile.common.entity.StorageConfig;
|
||||
import com.besscroft.diyfile.common.enums.StorageTypeEnum;
|
||||
import com.besscroft.diyfile.common.param.FileInitParam;
|
||||
import com.besscroft.diyfile.common.param.storage.init.AliYunOssParam;
|
||||
import com.besscroft.diyfile.common.param.storage.init.AmazonS3Param;
|
||||
import com.besscroft.diyfile.common.param.storage.init.LocalParam;
|
||||
import com.besscroft.diyfile.common.param.storage.init.OneDriveParam;
|
||||
|
||||
@@ -36,26 +37,39 @@ public class ParamUtils {
|
||||
if (endWith) {
|
||||
configMap.put("proxy_url", StrUtil.subBefore(configMap.get("proxy_url"), "/", true));
|
||||
}
|
||||
return OneDriveParam.builder()
|
||||
OneDriveParam param = OneDriveParam.builder()
|
||||
.clientId(configMap.get("client_id"))
|
||||
.clientSecret(configMap.get("client_secret"))
|
||||
.redirectUri(configMap.get("redirect_uri"))
|
||||
.refreshToken(configMap.get("refresh_token"))
|
||||
.mountPath(configMap.get("mount_path"))
|
||||
.proxyUrl(Optional.ofNullable(configMap.get("proxy_url")).orElse(""))
|
||||
.build();
|
||||
param.setMountPath(configMap.get("mount_path"));
|
||||
return param;
|
||||
} else if (Objects.equals(storage.getType(), StorageTypeEnum.ALIYUN_OSS.getValue())) {
|
||||
return AliYunOssParam.builder()
|
||||
AliYunOssParam param = AliYunOssParam.builder()
|
||||
.accessKeyId(configMap.get("accessKeyId"))
|
||||
.accessKeySecret(configMap.get("accessKeySecret"))
|
||||
.endpoint(configMap.get("endpoint"))
|
||||
.bucketName(configMap.get("bucketName"))
|
||||
.mountPath(configMap.get("mount_path"))
|
||||
.build();
|
||||
param.setMountPath(configMap.get("mount_path"));
|
||||
param.setBucketName(configMap.get("bucketName"));
|
||||
return param;
|
||||
} else if (Objects.equals(storage.getType(), StorageTypeEnum.LOCAL.getValue())) {
|
||||
return LocalParam.builder()
|
||||
.mountPath(configMap.get("mount_path"))
|
||||
LocalParam param = LocalParam.builder()
|
||||
.build();
|
||||
param.setMountPath(configMap.get("mount_path"));
|
||||
return param;
|
||||
} else if (Objects.equals(storage.getType(), StorageTypeEnum.AMAZON_S3.getValue())) {
|
||||
AmazonS3Param param = AmazonS3Param.builder()
|
||||
.accessKey(configMap.get("accessKey"))
|
||||
.secretKey(configMap.get("secretKey"))
|
||||
.region(configMap.get("region"))
|
||||
.endpoint(configMap.get("endpoint"))
|
||||
.build();
|
||||
param.setMountPath(configMap.get("mount_path"));
|
||||
param.setBucketName(configMap.get("bucketName"));
|
||||
return param;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -71,6 +71,11 @@
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
</dependency>
|
||||
<!-- aws-java-sdk-s3 -->
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.besscroft.diyfile.storage.service.base;
|
||||
|
||||
import com.besscroft.diyfile.common.param.storage.init.S3Param;
|
||||
import com.besscroft.diyfile.common.vo.FileInfoVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
import software.amazon.awssdk.services.s3.model.S3Object;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description S3 基础服务抽象类
|
||||
* @Author Bess Croft
|
||||
* @Date 2023/3/10 23:27
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class AbstractS3BaseService<T extends S3Param> extends AbstractFileBaseService<T> {
|
||||
|
||||
/** S3 客户端 */
|
||||
protected S3Client s3Client;
|
||||
|
||||
public abstract void init();
|
||||
|
||||
@Override
|
||||
public abstract String getFileDownloadUrl(String fileName, String filePath);
|
||||
|
||||
@Override
|
||||
public List<FileInfoVo> getFileList(String folderPath) {
|
||||
try {
|
||||
ListObjectsRequest listObjects = ListObjectsRequest
|
||||
.builder()
|
||||
.bucket(initParam.getBucketName())
|
||||
.build();
|
||||
|
||||
ListObjectsResponse res = s3Client.listObjects(listObjects);
|
||||
List<S3Object> objects = res.contents();
|
||||
for (S3Object myValue : objects) {
|
||||
log.info("The name of the key is " + myValue.key());
|
||||
log.info("The object is " + calKb(myValue.size()) + " KBs");
|
||||
log.info("The owner is " + myValue.owner());
|
||||
}
|
||||
|
||||
} catch (S3Exception e) {
|
||||
log.error(e.awsErrorDetails().errorMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract Integer getStorageType();
|
||||
|
||||
@Override
|
||||
public FileInfoVo getFileInfo(String filePath, String fileName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createItem(String folderPath, String fileName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateItem(String filePath, String fileName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteItem(String filePath) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadItem(String folderPath, String fileName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUploadSession(String folderPath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* convert bytes to kbs.
|
||||
* @param val bytes
|
||||
* @return kbs
|
||||
*/
|
||||
private static long calKb(Long val) {
|
||||
return val/1024;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,9 +13,6 @@ import com.besscroft.diyfile.common.exception.DiyFileException;
|
||||
import com.besscroft.diyfile.common.param.storage.init.AliYunOssParam;
|
||||
import com.besscroft.diyfile.common.vo.FileInfoVo;
|
||||
import com.besscroft.diyfile.storage.service.base.AbstractOSSBaseService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
@@ -35,12 +32,9 @@ import java.util.Objects;
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class AliYunOssServiceImpl extends AbstractOSSBaseService<AliYunOssParam> {
|
||||
|
||||
private final Cache<String, Object> caffeineCache;
|
||||
private final ObjectMapper objectMapper;
|
||||
private OSS ossClient;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.besscroft.diyfile.storage.service.impl;
|
||||
|
||||
import com.besscroft.diyfile.common.enums.StorageTypeEnum;
|
||||
import com.besscroft.diyfile.common.param.storage.init.AmazonS3Param;
|
||||
import com.besscroft.diyfile.storage.service.base.AbstractS3BaseService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Service;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
|
||||
/**
|
||||
* @Description Amazon S3 服务实现类
|
||||
* @Author Bess Croft
|
||||
* @Date 2023/3/10 23:32
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class AmazonS3ServiceImpl extends AbstractS3BaseService<AmazonS3Param> {
|
||||
|
||||
/**
|
||||
* https://docs.aws.amazon.com/zh_cn/sdk-for-java/latest/developer-guide/examples-s3.html
|
||||
* https://docs.aws.amazon.com/zh_cn/sdk-for-java/latest/developer-guide/home.html
|
||||
* https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2
|
||||
* https://docs.aws.amazon.com/zh_cn/sdk-for-java/latest/developer-guide/crt-based-s3-client.html
|
||||
* https://docs.aws.amazon.com/zh_cn/sdk-for-java/latest/developer-guide/transfer-manager.html
|
||||
*/
|
||||
@Override
|
||||
public void init() {
|
||||
super.s3Client = S3Client.builder()
|
||||
.region(Region.of(initParam.getRegion()))
|
||||
.credentialsProvider(
|
||||
StaticCredentialsProvider.create(
|
||||
AwsBasicCredentials.create(initParam.getAccessKey(), initParam.getSecretKey())
|
||||
)
|
||||
)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileDownloadUrl(String fileName, String filePath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getStorageType() {
|
||||
return StorageTypeEnum.AMAZON_S3.getValue();
|
||||
}
|
||||
|
||||
}
|
||||
9
pom.xml
9
pom.xml
@@ -39,6 +39,7 @@
|
||||
<jaxb-api.version>2.3.1</jaxb-api.version>
|
||||
<activation.version>1.1.1</activation.version>
|
||||
<jaxb-runtime.version>2.3.3</jaxb-runtime.version>
|
||||
<software.amazon.awssdk.version>2.20.20</software.amazon.awssdk.version>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
@@ -163,6 +164,14 @@
|
||||
<artifactId>jaxb-runtime</artifactId>
|
||||
<version>${jaxb-runtime.version}</version>
|
||||
</dependency>
|
||||
<!-- AWS SDK for Java 2.0 -->
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>bom</artifactId>
|
||||
<version>${software.amazon.awssdk.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user