فهرست منبع

add copyFolder

xiewj 1 سال پیش
والد
کامیت
aa3a4d2ba8

+ 2 - 1
4dkankan-utils-filestorage/src/main/java/com/fdkankan/filestorage/FileStorageTemplate.java

@@ -69,7 +69,8 @@ public interface FileStorageTemplate {
     Boolean copyObject(String oldPath, String newPath);
 
     Boolean copyObject(String bucket, String oldPath, String toBucket, String newPath);
-
+    void copyFolder(String sourcePath, String targetPath) ;
+    void copyFolder(String sourceBucketName,String sourcePath, String targetBucketName, String targetPath) ;
 
     void deleteObject(String keyName);
 

+ 20 - 0
4dkankan-utils-filestorage/src/main/java/com/fdkankan/filestorage/aliyun/AliyunOssTemplate.java

@@ -17,6 +17,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Base64Utils;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 
 import java.io.*;
@@ -413,4 +414,23 @@ public class AliyunOssTemplate implements FileStorageTemplate {
         host=this.getBucket()+"."+host;
         return new URI(uri.getScheme(), host, uri.getPath()+"/"+key, uri.getFragment()).toString();
     }
+
+    @Override
+    public void copyFolder(String sourcePath, String targetPath) {
+        copyFolder(ossProperties.getBucket(),   sourcePath,  ossProperties.getBucket(),   targetPath);
+    }
+    @Override
+    public void copyFolder(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
+        try {
+            List<String> files = this.getFileFolder(sourceBucketName, sourcePath);
+            if (ObjectUtils.isEmpty(files)) {
+                return;
+            }
+            files.stream().forEach(file -> {
+                this.copyObject(sourceBucketName, file, targetBucketName ,file.replace(sourcePath, targetPath));
+            });
+        } catch (Exception e) {
+            log.error("列举文件目录失败,key:" + sourcePath, e);
+        }
+    }
 }

+ 19 - 0
4dkankan-utils-filestorage/src/main/java/com/fdkankan/filestorage/aws/AwsTemplate.java

@@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Base64Utils;
+import org.springframework.util.ObjectUtils;
 
 import java.io.*;
 import java.net.URI;
@@ -463,4 +464,22 @@ public class AwsTemplate implements FileStorageTemplate {
         host=this.getBucket()+"."+host;
         return new URI(uri.getScheme(), host, uri.getPath()+"/"+key, uri.getFragment()).toString();
     }
+    @Override
+    public void copyFolder(String sourcePath, String targetPath) {
+        copyFolder(awsProperties.getBucket(),   sourcePath,  awsProperties.getBucket(),   targetPath);
+    }
+    @Override
+    public void copyFolder(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
+        try {
+            List<String> files = this.getFileFolder(sourceBucketName, sourcePath);
+            if (ObjectUtils.isEmpty(files)) {
+                return;
+            }
+            files.stream().forEach(file -> {
+                this.copyObject(sourceBucketName, file, targetBucketName ,file.replace(sourcePath, targetPath));
+            });
+        } catch (Exception e) {
+            log.error("列举文件目录失败,key:" + sourcePath, e);
+        }
+    }
 }

+ 20 - 0
4dkankan-utils-filestorage/src/main/java/com/fdkankan/filestorage/cos/CosTemplate.java

@@ -16,6 +16,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Base64Utils;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
 
 import java.io.*;
 import java.net.URI;
@@ -422,4 +423,23 @@ public class CosTemplate implements FileStorageTemplate {
         host=this.getBucket()+"."+host;
         return new URI(uri.getScheme(), host, uri.getPath()+"/"+key, uri.getFragment()).toString();
     }
+
+    @Override
+    public void copyFolder(String sourcePath, String targetPath) {
+        copyFolder(cosProperties.getBucket(),   sourcePath,  cosProperties.getBucket(),   targetPath);
+    }
+    @Override
+    public void copyFolder(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
+        try {
+            List<String> files = this.getFileFolder(sourceBucketName, sourcePath);
+            if (ObjectUtils.isEmpty(files)) {
+                return;
+            }
+            files.stream().forEach(file -> {
+               this.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
+            });
+        } catch (Exception e) {
+            log.error("列举文件目录失败,key:" + sourcePath, e);
+        }
+    }
 }

+ 19 - 0
4dkankan-utils-filestorage/src/main/java/com/fdkankan/filestorage/minio/MinioTemplate.java

@@ -23,6 +23,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Base64Utils;
+import org.springframework.util.ObjectUtils;
 
 import java.io.*;
 import java.net.URI;
@@ -466,4 +467,22 @@ public class MinioTemplate implements FileStorageTemplate {
         host=this.getBucket()+"."+host;
         return new URI(uri.getScheme(), host, uri.getPath()+"/"+key, uri.getFragment()).toString();
     }
+    @Override
+    public void copyFolder(String sourcePath, String targetPath) {
+        copyFolder(minioProperties.getBucket(),   sourcePath,  minioProperties.getBucket(),   targetPath);
+    }
+    @Override
+    public void copyFolder(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
+        try {
+            List<String> files = this.getFileFolder(sourceBucketName, sourcePath);
+            if (ObjectUtils.isEmpty(files)) {
+                return;
+            }
+            files.stream().forEach(file -> {
+                this.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
+            });
+        } catch (Exception e) {
+            log.error("列举文件目录失败,key:" + sourcePath, e);
+        }
+    }
 }