|
@@ -1,10 +1,12 @@
|
|
|
package com.fs.system.oss;
|
|
package com.fs.system.oss;
|
|
|
|
|
|
|
|
import com.fs.common.exception.file.OssException;
|
|
import com.fs.common.exception.file.OssException;
|
|
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
import com.qcloud.cos.COSClient;
|
|
import com.qcloud.cos.COSClient;
|
|
|
import com.qcloud.cos.ClientConfig;
|
|
import com.qcloud.cos.ClientConfig;
|
|
|
import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
|
import com.qcloud.cos.auth.COSCredentials;
|
|
import com.qcloud.cos.auth.COSCredentials;
|
|
|
|
|
+import com.qcloud.cos.http.HttpMethodName;
|
|
|
import com.qcloud.cos.model.ObjectMetadata;
|
|
import com.qcloud.cos.model.ObjectMetadata;
|
|
|
import com.qcloud.cos.model.PutObjectRequest;
|
|
import com.qcloud.cos.model.PutObjectRequest;
|
|
|
import com.qcloud.cos.region.Region;
|
|
import com.qcloud.cos.region.Region;
|
|
@@ -13,6 +15,9 @@ import org.apache.commons.io.IOUtils;
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
|
|
+import java.net.URI;
|
|
|
|
|
+import java.net.URL;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 腾讯云存储
|
|
* 腾讯云存储
|
|
@@ -24,14 +29,12 @@ public class QcloudCloudStorageService extends CloudStorageService
|
|
|
public QcloudCloudStorageService(CloudStorageConfig config)
|
|
public QcloudCloudStorageService(CloudStorageConfig config)
|
|
|
{
|
|
{
|
|
|
this.config = config;
|
|
this.config = config;
|
|
|
- // 初始化
|
|
|
|
|
init();
|
|
init();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void init()
|
|
private void init()
|
|
|
{
|
|
{
|
|
|
COSCredentials credentials = new BasicCOSCredentials(config.getQcloudSecretId(), config.getQcloudSecretKey());
|
|
COSCredentials credentials = new BasicCOSCredentials(config.getQcloudSecretId(), config.getQcloudSecretKey());
|
|
|
- // 设置bucket所在的区域,最新sdk不再支持简写,请填写完整
|
|
|
|
|
Region region = new Region(config.getQcloudRegion());
|
|
Region region = new Region(config.getQcloudRegion());
|
|
|
ClientConfig clientConfig = new ClientConfig(region);
|
|
ClientConfig clientConfig = new ClientConfig(region);
|
|
|
client = new COSClient(credentials, clientConfig);
|
|
client = new COSClient(credentials, clientConfig);
|
|
@@ -43,19 +46,16 @@ public class QcloudCloudStorageService extends CloudStorageService
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
- // 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据,存在内存OOM的情况
|
|
|
|
|
objectMetadata.setContentLength(data.length);
|
|
objectMetadata.setContentLength(data.length);
|
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(config.getQcloudBucketName(), path,
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(config.getQcloudBucketName(), path,
|
|
|
new ByteArrayInputStream(data), objectMetadata);
|
|
new ByteArrayInputStream(data), objectMetadata);
|
|
|
- // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
|
|
|
|
|
- // putObjectRequest.setStorageClass(StorageClass.Standard_IA);
|
|
|
|
|
client.putObject(putObjectRequest);
|
|
client.putObject(putObjectRequest);
|
|
|
}
|
|
}
|
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
|
{
|
|
{
|
|
|
throw new OssException("文件上传失败," + e.getMessage());
|
|
throw new OssException("文件上传失败," + e.getMessage());
|
|
|
}
|
|
}
|
|
|
- return config.getQcloudDomain() +"/"+ path;
|
|
|
|
|
|
|
+ return buildPublicUrl(path);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -83,4 +83,88 @@ public class QcloudCloudStorageService extends CloudStorageService
|
|
|
{
|
|
{
|
|
|
return upload(inputStream, getPath(config.getQcloudPrefix(), suffix));
|
|
return upload(inputStream, getPath(config.getQcloudPrefix(), suffix));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String getAccessUrl(String storedUrl)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (StringUtils.isBlank(storedUrl))
|
|
|
|
|
+ {
|
|
|
|
|
+ return storedUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+ String objectKey = extractObjectKey(storedUrl);
|
|
|
|
|
+ if (StringUtils.isBlank(objectKey))
|
|
|
|
|
+ {
|
|
|
|
|
+ return storedUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+ return generatePresignedUrl(objectKey);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String buildPublicUrl(String path)
|
|
|
|
|
+ {
|
|
|
|
|
+ String domain = config.getQcloudDomain();
|
|
|
|
|
+ if (domain.endsWith("/"))
|
|
|
|
|
+ {
|
|
|
|
|
+ domain = domain.substring(0, domain.length() - 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (path.startsWith("/"))
|
|
|
|
|
+ {
|
|
|
|
|
+ path = path.substring(1);
|
|
|
|
|
+ }
|
|
|
|
|
+ return domain + "/" + path;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String generatePresignedUrl(String objectKey)
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ Date expiration = new Date(System.currentTimeMillis()
|
|
|
|
|
+ + CloudConstant.QCLOUD_PRESIGNED_URL_EXPIRE_SECONDS * 1000);
|
|
|
|
|
+ URL url = client.generatePresignedUrl(config.getQcloudBucketName(), objectKey, expiration,
|
|
|
|
|
+ HttpMethodName.GET);
|
|
|
|
|
+ return url.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new OssException("生成预签名 URL 失败," + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String extractObjectKey(String urlOrPath)
|
|
|
|
|
+ {
|
|
|
|
|
+ String path = urlOrPath.trim();
|
|
|
|
|
+ int queryIndex = path.indexOf('?');
|
|
|
|
|
+ if (queryIndex > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ path = path.substring(0, queryIndex);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String domain = config.getQcloudDomain();
|
|
|
|
|
+ if (domain.endsWith("/"))
|
|
|
|
|
+ {
|
|
|
|
|
+ domain = domain.substring(0, domain.length() - 1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (path.startsWith("http://") || path.startsWith("https://"))
|
|
|
|
|
+ {
|
|
|
|
|
+ if (path.startsWith(domain + "/"))
|
|
|
|
|
+ {
|
|
|
|
|
+ return path.substring(domain.length() + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ String uriPath = new URI(path).getPath();
|
|
|
|
|
+ if (StringUtils.isNotBlank(uriPath))
|
|
|
|
|
+ {
|
|
|
|
|
+ return uriPath.startsWith("/") ? uriPath.substring(1) : uriPath;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception ignored)
|
|
|
|
|
+ {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return path.startsWith("/") ? path.substring(1) : path;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|