🔥 移除阿里云实现, 仅使用 亚马逊云的 s3实现
This commit is contained in:
@@ -37,11 +37,6 @@
|
||||
<dependency>
|
||||
<groupId>com.hccake</groupId>
|
||||
<artifactId>ballcat-spring-boot-starter-storage</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hccake</groupId>
|
||||
|
||||
@@ -54,7 +54,6 @@
|
||||
<dynamic-datasource.version>3.3.1</dynamic-datasource.version>
|
||||
<xxl-job.version>2.3.0</xxl-job.version>
|
||||
<spring-boot-admin.version>2.4.1</spring-boot-admin.version>
|
||||
<oss.aliyun.version>3.11.3</oss.aliyun.version>
|
||||
<easyexcel.version>2.2.6</easyexcel.version>
|
||||
<jasypt.version>3.0.3</jasypt.version>
|
||||
<jsoup.version>1.13.1</jsoup.version>
|
||||
@@ -330,12 +329,6 @@
|
||||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
|
||||
<version>${dynamic-datasource.version}</version>
|
||||
</dependency>
|
||||
<!--aliyun oss sdk-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>${oss.aliyun.version}</version>
|
||||
</dependency>
|
||||
<!--easyExcel 处理-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
|
||||
@@ -23,16 +23,9 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.hccake.ballcat.commom.storage;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.hccake.ballcat.commom.storage.client.AliyunOssClient;
|
||||
import com.hccake.ballcat.commom.storage.client.AwsOssClient;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
@@ -21,14 +19,6 @@ public class FileStorageAutoConfiguration {
|
||||
|
||||
private final FileStorageProperties properties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(FileStorageClient.class)
|
||||
@ConditionalOnClass(OSS.class)
|
||||
public FileStorageClient aliyunOssClient() {
|
||||
return new AliyunOssClient(properties.getEndpoint(), properties.getAccessKey(), properties.getAccessSecret(),
|
||||
properties.getBucketName(), properties.getRootPath());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(S3Client.class)
|
||||
@ConditionalOnMissingBean(FileStorageClient.class)
|
||||
|
||||
@@ -15,26 +15,26 @@ import org.springframework.util.Assert;
|
||||
public interface FileStorageClient {
|
||||
|
||||
/**
|
||||
* 文件上传, 本方法会读一遍流, 计算流大小, 推荐使用 upload(stream, filePath, size) 方法
|
||||
* @param filePath 存储对象名称
|
||||
* 文件上传, 本方法会读一遍流, 计算流大小, 推荐使用 upload(stream, absolutePath, size) 方法
|
||||
* @param absolutePath 文件相对 getRoot() 的路径
|
||||
* @param stream 文件输入流
|
||||
* @return 文件相对路径
|
||||
* @return 文件绝对路径
|
||||
* @throws IOException 流操作时异常
|
||||
*/
|
||||
default String upload(InputStream stream, String filePath) throws IOException {
|
||||
default String upload(InputStream stream, String absolutePath) throws IOException {
|
||||
final StreamTemp temp = getSize(stream);
|
||||
return upload(temp.getStream(), filePath, temp.getSize());
|
||||
return upload(temp.getStream(), absolutePath, temp.getSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param stream 文件流
|
||||
* @param filePath 文件路径
|
||||
* @param absolutePath 文件相对 getRoot() 的路径
|
||||
* @param size 输入流大小(仅在使用亚马逊云时有效)
|
||||
* @return 文件路径
|
||||
* @return 文件绝对路径
|
||||
* @author lingting 2021-04-16 15:20
|
||||
*/
|
||||
String upload(InputStream stream, String filePath, Long size);
|
||||
String upload(InputStream stream, String absolutePath, Long size);
|
||||
|
||||
/**
|
||||
* 删除路径
|
||||
@@ -45,18 +45,18 @@ public interface FileStorageClient {
|
||||
|
||||
/**
|
||||
* 复制文件
|
||||
* @param source 原对象名
|
||||
* @param target 目标对象名
|
||||
* @param absoluteSource 相对 getRoot() 的 源文件 路径
|
||||
* @param absoluteTarget 相对 getRoot() 的 目标文件 路径
|
||||
*/
|
||||
void copy(String source, String target);
|
||||
void copy(String absoluteSource, String absoluteTarget);
|
||||
|
||||
/**
|
||||
* 获取下载路径
|
||||
* @param filePath 文件路径
|
||||
* @param absolutePath 文件相对 getRoot() 的路径
|
||||
* @return java.lang.String
|
||||
* @author lingting 2021-04-16 15:33
|
||||
*/
|
||||
String getDownloadUrl(String filePath);
|
||||
String getDownloadUrl(String absolutePath);
|
||||
|
||||
/**
|
||||
* 获取文件操作根路径
|
||||
@@ -78,23 +78,18 @@ public interface FileStorageClient {
|
||||
|
||||
/**
|
||||
* 获取真实文件路径
|
||||
* @param path 相对 root 的文件路径
|
||||
* @return java.lang.String
|
||||
* @param absolutePath 文件相对 getRoot() 的路径
|
||||
* @return 文件绝对路径
|
||||
* @author lingting 2021-05-10 15:58
|
||||
*/
|
||||
default String getPath(String path) {
|
||||
Assert.hasText(path, "path must not be empty");
|
||||
default String getPath(String absolutePath) {
|
||||
Assert.hasText(absolutePath, "path must not be empty");
|
||||
|
||||
// 路径以 root 开头, 表示已经是真实路径了
|
||||
if (path.startsWith(getRoot())) {
|
||||
return path;
|
||||
if (absolutePath.startsWith(PATH_FLAG)) {
|
||||
absolutePath = absolutePath.substring(1);
|
||||
}
|
||||
|
||||
if (path.startsWith(PATH_FLAG)) {
|
||||
path = path.substring(1);
|
||||
}
|
||||
|
||||
return getRoot() + path;
|
||||
return getRoot() + absolutePath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
package com.hccake.ballcat.commom.storage.client;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.hccake.ballcat.commom.storage.FileStorageClient;
|
||||
import java.io.InputStream;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2019/7/16 15:45
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class AliyunOssClient implements FileStorageClient, InitializingBean, DisposableBean {
|
||||
|
||||
private final String endpoint;
|
||||
|
||||
private final String accessKey;
|
||||
|
||||
private final String accessSecret;
|
||||
|
||||
private final String bucketName;
|
||||
|
||||
private final String root;
|
||||
|
||||
private OSS client;
|
||||
|
||||
@Override
|
||||
public String upload(InputStream stream, String filePath, Long size) {
|
||||
final String path = getPath(filePath);
|
||||
client.putObject(bucketName, path, stream);
|
||||
return path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = getPath(path);
|
||||
if (client.doesObjectExist(bucketName, path)) {
|
||||
client.deleteObject(bucketName, path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copy(String source, String target) {
|
||||
client.copyObject(bucketName, getPath(source), bucketName, getPath(target));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String filePath) {
|
||||
return String.format("https://%s.%s/%s", bucketName, endpoint, getPath(filePath));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
Assert.hasText(endpoint, "endpoint 为空");
|
||||
Assert.hasText(accessKey, "Oss accessKey为空");
|
||||
Assert.hasText(accessSecret, "Oss accessSecret为空");
|
||||
client = new OSSClientBuilder().build(endpoint, accessKey, accessSecret);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
if (this.client != null) {
|
||||
this.client.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,8 +48,8 @@ public class AwsOssClient implements FileStorageClient, DisposableBean {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream stream, String filePath, Long size) {
|
||||
final String path = getPath(filePath);
|
||||
public String upload(InputStream stream, String absolutePath, Long size) {
|
||||
final String path = getPath(absolutePath);
|
||||
client.putObject(builder -> builder.bucket(bucketName).key(path), RequestBody.fromInputStream(stream, size));
|
||||
return path;
|
||||
}
|
||||
@@ -61,15 +61,15 @@ public class AwsOssClient implements FileStorageClient, DisposableBean {
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void copy(String source, String target) {
|
||||
String s = getCopyUrl(source);
|
||||
public void copy(String absoluteSource, String absoluteTarget) {
|
||||
String s = getCopyUrl(absoluteSource);
|
||||
client.copyObject(
|
||||
builder -> builder.copySource(s).destinationBucket(bucketName).destinationKey(getPath(target)));
|
||||
builder -> builder.copySource(s).destinationBucket(bucketName).destinationKey(getPath(absoluteTarget)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String filePath) {
|
||||
return String.format("https://%s.%s/%s", bucketName, endpoint, getPath(filePath));
|
||||
public String getDownloadUrl(String absolutePath) {
|
||||
return String.format("https://%s.%s/%s", bucketName, endpoint, getPath(absolutePath));
|
||||
}
|
||||
|
||||
protected String getCopyUrl(String path) throws UnsupportedEncodingException {
|
||||
|
||||
Reference in New Issue
Block a user