|
@@ -2,11 +2,17 @@ package com.fdkankan.fyun.oss;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.fdkankan.fyun.constant.FYunTypeEnum;
|
|
|
import com.fdkankan.fyun.face.AbstractFYunFileService;
|
|
|
import com.qcloud.cos.COSClient;
|
|
|
+import com.qcloud.cos.ClientConfig;
|
|
|
+import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
|
+import com.qcloud.cos.auth.COSCredentials;
|
|
|
import com.qcloud.cos.http.HttpMethodName;
|
|
|
+import com.qcloud.cos.http.HttpProtocol;
|
|
|
import com.qcloud.cos.model.*;
|
|
|
+import com.qcloud.cos.region.Region;
|
|
|
import com.qcloud.cos.utils.IOUtils;
|
|
|
import com.qcloud.cos.utils.Md5Utils;
|
|
|
import org.slf4j.Logger;
|
|
@@ -16,13 +22,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
+import sun.misc.Cleaner;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URL;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
@@ -34,6 +39,23 @@ public class CosFileService extends AbstractFYunFileService {
|
|
|
@Autowired
|
|
|
private COSClient cosClient;
|
|
|
|
|
|
+// public static COSClient getClient(){
|
|
|
+// // 1 初始化用户身份信息(secretId, secretKey)。
|
|
|
+// // SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
|
|
|
+// COSCredentials cred = new BasicCOSCredentials("AKIDlz42sV8sV3pW6UiiieXQuU0QrFbq9Qmx", "A8zmMoz1ufCYuCSmvxulV8hAXnx6EOTX");
|
|
|
+// // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
|
|
|
+// // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
|
|
|
+// Region region = new Region("ap-guangzhou");
|
|
|
+// ClientConfig clientConfig = new ClientConfig(region);
|
|
|
+// // 这里建议设置使用 https 协议
|
|
|
+// // 从 5.6.54 版本开始,默认使用了 https
|
|
|
+// clientConfig.setHttpProtocol(HttpProtocol.https);
|
|
|
+// // 3 生成 cos 客户端。
|
|
|
+// COSClient cosClient = new COSClient(cred, clientConfig);
|
|
|
+//
|
|
|
+// return cosClient;
|
|
|
+// }
|
|
|
+
|
|
|
@Override
|
|
|
public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
|
|
|
String contentMd5 = Md5Utils.md5AsBase64(data);
|
|
@@ -145,8 +167,6 @@ public class CosFileService extends AbstractFYunFileService {
|
|
|
// prefix 表示列出的对象名以 prefix 为前缀
|
|
|
// 这里填要列出的目录的相对 bucket 的路径
|
|
|
listObjectsRequest.setPrefix(remoteFolderPath);
|
|
|
- // delimiter 表示目录的截断符, 例如:设置为 / 则表示对象名遇到 / 就当做一级目录)
|
|
|
- listObjectsRequest.setDelimiter("/");
|
|
|
// 设置最大遍历出多少个对象, 一次 listobject 最大支持1000
|
|
|
listObjectsRequest.setMaxKeys(1000);
|
|
|
|
|
@@ -198,7 +218,7 @@ public class CosFileService extends AbstractFYunFileService {
|
|
|
//获取下一页的起始点,它的下一项
|
|
|
ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
|
|
|
List<String> collect = objectListing.getObjectSummaries().parallelStream()
|
|
|
- .map(COSObjectSummary::getKey).filter(entity -> entity.contains(".")).collect(Collectors.toList());
|
|
|
+ .map(COSObjectSummary::getKey).filter(entity -> !entity.endsWith("/")).collect(Collectors.toList());
|
|
|
if (!CollectionUtils.isEmpty(collect)) {
|
|
|
keyList.addAll(collect);
|
|
|
}
|
|
@@ -309,6 +329,9 @@ public class CosFileService extends AbstractFYunFileService {
|
|
|
public long getSubFileNums(String bucket, String url) {
|
|
|
long totalSubFileNum = 0;
|
|
|
try {
|
|
|
+ if (!url.endsWith("/")) {
|
|
|
+ url = url + "/";
|
|
|
+ }
|
|
|
boolean flag = true;
|
|
|
String nextMaker = null;
|
|
|
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
|
|
@@ -322,7 +345,7 @@ public class CosFileService extends AbstractFYunFileService {
|
|
|
listObjectsRequest.setMarker(nextMaker);
|
|
|
ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
|
|
|
List<String> collect = objectListing.getObjectSummaries().parallelStream()
|
|
|
- .map(COSObjectSummary::getKey).collect(Collectors.toList());
|
|
|
+ .filter(summary -> !summary.getKey().endsWith("/")).map(COSObjectSummary::getKey).collect(Collectors.toList());
|
|
|
if (!CollectionUtils.isEmpty(collect)) {
|
|
|
totalSubFileNum = totalSubFileNum + collect.size();
|
|
|
}
|
|
@@ -338,10 +361,8 @@ public class CosFileService extends AbstractFYunFileService {
|
|
|
|
|
|
@Override
|
|
|
public Boolean checkStore(String bucket,String filePath){
|
|
|
- // TODO: 2023/8/1
|
|
|
-// ObjectMetadata objectMetadata = cosClient.getObjectMetadata(bucket, filePath);
|
|
|
-// return !objectMetadata.get();
|
|
|
- return false;
|
|
|
+ ObjectMetadata objectMetadata = cosClient.getObjectMetadata(bucket, filePath);
|
|
|
+ return !isRestoreCompleted(objectMetadata);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -363,6 +384,14 @@ public class CosFileService extends AbstractFYunFileService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private boolean isRestoreCompleted(ObjectMetadata objectMetadata){
|
|
|
+ Date restoreExpirationTime = objectMetadata.getRestoreExpirationTime();
|
|
|
+ if(Objects.nonNull(restoreExpirationTime) && restoreExpirationTime.after(Calendar.getInstance().getTime())){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Integer getRestoreFolderProcess(String bucket,String folderName){
|
|
|
ObjectMetadata objectMetadata ;
|
|
@@ -374,10 +403,10 @@ public class CosFileService extends AbstractFYunFileService {
|
|
|
List<String> restoreFileList = new ArrayList<>();
|
|
|
for (String objectName : objectList) {
|
|
|
objectMetadata = cosClient.getObjectMetadata(bucket, objectName);
|
|
|
-// if(objectMetadata.isRestoreCompleted()){
|
|
|
-// restoreFileList.add(objectName);
|
|
|
-// }
|
|
|
- // TODO: 2023/8/1
|
|
|
+ if(this.isRestoreCompleted(objectMetadata)){
|
|
|
+
|
|
|
+ restoreFileList.add(objectName);
|
|
|
+ }
|
|
|
}
|
|
|
if(objectList.size() <= restoreFileList.size() ){
|
|
|
return 100;
|