Przeglądaj źródła

cos 冻结解冻

dsx 2 lat temu
rodzic
commit
4845aa4818

+ 34 - 20
4dkankan-utils-fyun-cos/src/main/java/com/fdkankan/fyun/oss/CosFileService.java

@@ -3,6 +3,7 @@ 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.common.util.DateExtUtil;
 import com.fdkankan.fyun.constant.FYunTypeEnum;
 import com.fdkankan.fyun.face.AbstractFYunFileService;
 import com.qcloud.cos.COSClient;
@@ -39,22 +40,22 @@ 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;
-//    }
+    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) {
@@ -434,10 +435,6 @@ public class CosFileService extends AbstractFYunFileService {
         // 校验Object是否为归档类型Object。
         StorageClass storageClass = objectMetadata.getStorageClassEnum();
         if (storageClass == StorageClass.Archive) {
-            // 设置解冻冷归档Object的优先级。
-            // RestoreTier.RESTORE_TIER_EXPEDITED 表示1小时内完成解冻。
-            // RestoreTier.RESTORE_TIER_STANDARD 表示2~5小时内完成解冻。
-            // RestoreTier.RESTORE_TIER_BULK 表示5~12小时内完成解冻。
             Tier tier = null;
             switch (priority){
                 case 1 :
@@ -490,4 +487,21 @@ public class CosFileService extends AbstractFYunFileService {
         return total;
     }
 
+    @Override
+    public void copyFileToArchive(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
+        try {
+            List<String> files = listRemoteFiles(sourceBucketName, sourcePath);
+            if (ObjectUtils.isEmpty(files)) {
+                return;
+            }
+            files.stream().forEach(file -> {
+                CopyObjectRequest request = new CopyObjectRequest(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
+                request.setStorageClass(StorageClass.Archive);
+                cosClient.copyObject(request);
+            });
+        } catch (Exception e) {
+            log.error("复制文件或目录失败,key:" + sourcePath, e);
+        }
+    }
+
 }

+ 5 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/AbstractFYunFileService.java

@@ -87,6 +87,11 @@ public abstract class AbstractFYunFileService implements FYunFileServiceInterfac
         copyFileBetweenBucket(fYunFileConfig.getBucket(), sourcePath, fYunFileConfig.getBucket(), targetPath);
     }
 
+    @Override
+    public void copyFileToArchive(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
+        copyFileToArchive(sourceBucketName, sourcePath, targetBucketName, targetPath);
+    }
+
     public void copyFileInBucket(String bucket, String sourcePath, String targetPath) {
         copyFileBetweenBucket(bucket, sourcePath, bucket, targetPath);
     }

+ 13 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/FYunFileServiceInterface.java

@@ -233,6 +233,18 @@ public interface FYunFileServiceInterface {
 
     /**
      * <p>
+     * 拷贝目录
+     * </p>
+     *
+     * @param sourcePath
+     * @param targetPath
+     * @author dengsixing
+     * @date 2022/1/18
+     **/
+    void copyFileToArchive(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) ;
+
+    /**
+     * <p>
      * 拷贝系统bucket目录至指定bucket
      * </p>
      *
@@ -268,6 +280,7 @@ public interface FYunFileServiceInterface {
     void copyFilesBetweenBucket(String targetBucketName, Map<String, String> pathMap) ;
 
 
+
     /**
      * 获取文件内容,文件内容过大时,会有造成内存溢出的风险,慎用
      *