Ver código fonte

Merge remote-tracking branch 'origin/master'

dengsixing 1 ano atrás
pai
commit
ee4d3c5c53

+ 1 - 1
4dkankan-utils-filestorage/pom.xml

@@ -16,7 +16,7 @@
         <cos.version>5.6.166</cos.version>
         <aws.version>1.12.481</aws.version>
         <minio.version>8.5.6</minio.version>
-        <aliyun-sdk-oss.version>3.8.0</aliyun-sdk-oss.version>
+        <aliyun-sdk-oss.version>3.15.1</aliyun-sdk-oss.version>
 
     </properties>
 

+ 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);
 

+ 28 - 8
4dkankan-utils-filestorage/src/main/java/com/fdkankan/filestorage/aliyun/AliyunOssTemplate.java

@@ -1,6 +1,7 @@
 package com.fdkankan.filestorage.aliyun;
 
 
+import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson2.JSON;
 import com.aliyun.oss.OSS;
@@ -17,6 +18,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.*;
@@ -138,7 +140,11 @@ public class AliyunOssTemplate implements FileStorageTemplate {
         InnerUtils.checkArgument(file != null && file.length() > 0, "file can't be empty");
         GetObjectRequest request = new GetObjectRequest(bucket, pathKey);
         log.info("下载开始:下载bucket={},下载pathKey={},下载filePath={}", bucket, pathKey, file);
-        return ossClient.getObject(request, new File(file));
+        File downloadFile = new File(file);
+        if (!FileUtil.exist(downloadFile.getParent())){
+            FileUtil.mkdir(downloadFile.getParent());
+        }
+        return ossClient.getObject(request, downloadFile);
     }
 
     @Override
@@ -278,14 +284,9 @@ public class AliyunOssTemplate implements FileStorageTemplate {
         try {
             boolean flag = true;
             String nextMaker = null;
-            //指定下一级文件
             ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
-            // 设置正斜线(/)为文件夹的分隔符。
-            listObjectsRequest.setDelimiter("/");
-            // 设置prefix参数来获取fun目录下的所有文件。
-            if (!StringUtils.isEmpty(keyName)) {
-                listObjectsRequest.setPrefix(keyName + "/");
-            }
+            //指定下一级文件
+            listObjectsRequest.setPrefix(keyName);
             //设置分页的页容量
             listObjectsRequest.setMaxKeys(200);
             do {
@@ -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);
+        }
+    }
 }

+ 24 - 2
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;
@@ -172,8 +173,11 @@ public class AwsTemplate implements FileStorageTemplate {
         GetObjectRequest request  = new GetObjectRequest(bucket,pathKey);
         log.info("下载开始:下载bucket={},下载pathKey={},下载filePath={}", bucket, pathKey, file);
         try {
-
-            return amazonS3Client.getObject(request,new File(file));
+            File downloadFile = new File(file);
+            if (!FileUtil.exist(downloadFile.getParent())){
+                FileUtil.mkdir(downloadFile.getParent());
+            }
+            return amazonS3Client.getObject(request,downloadFile);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -463,4 +467,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);
+        }
+    }
 }

+ 26 - 1
4dkankan-utils-filestorage/src/main/java/com/fdkankan/filestorage/cos/CosTemplate.java

@@ -1,6 +1,7 @@
 package com.fdkankan.filestorage.cos;
 
 
+import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson2.JSON;
 import com.fdkankan.filestorage.Consumer;
@@ -16,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 java.io.*;
 import java.net.URI;
@@ -153,7 +155,11 @@ public class CosTemplate implements FileStorageTemplate {
         InnerUtils.checkArgument(file != null && file.length() > 0, "file can't be empty");
         GetObjectRequest getObjectRequest = new  GetObjectRequest(bucket, pathKey);
         log.info("下载开始:下载bucket={},下载pathKey={},下载filePath={}", bucket, pathKey, file);
-        return cosClient.getObject(getObjectRequest, new File(file));
+        File downloadFile = new File(file);
+        if (!FileUtil.exist(downloadFile.getParent())){
+            FileUtil.mkdir(downloadFile.getParent());
+        }
+        return cosClient.getObject(getObjectRequest,downloadFile);
     }
 
     @Override
@@ -422,4 +428,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);
+        }
+    }
 }

+ 27 - 12
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;
@@ -163,24 +164,19 @@ public class MinioTemplate implements FileStorageTemplate {
         InnerUtils.checkArgument(pathKey != null && pathKey.length() > 0, "pathKey can't be empty");
         InnerUtils.checkArgument(file != null && file.length() > 0, "file can't be empty");
 
-        InputStream in=null;
 
         log.info("下载开始:下载bucket={},下载pathKey={},下载filePath={}", bucket, pathKey, file);
         try {
-            GetObjectArgs objectArgs = GetObjectArgs.builder().object(pathKey)
-                    .bucket(bucket).build();
-            in = minioClient.getObject(objectArgs);
+            File downloadFile = new File(file);
+            if (!FileUtil.exist(downloadFile.getParent())){
+                FileUtil.mkdir(downloadFile.getParent());
+            }
+            DownloadObjectArgs objectArgs = DownloadObjectArgs.builder().object(pathKey)
+                    .bucket(bucket).filename(file).build();
+            minioClient.downloadObject(objectArgs);
             return null;
         } catch (Exception e) {
             e.printStackTrace();
-        }finally {
-            if(in!=null){
-                try {
-                    in.close();
-                } catch (IOException e) {
-                    log.error(e.getMessage(), e);
-                }
-            }
         }
         return null;
     }
@@ -200,6 +196,7 @@ public class MinioTemplate implements FileStorageTemplate {
         InputStream in=null;
         try {
             in = minioClient.getObject(objectArgs);
+            handler.accept(in);
             return null;
         } catch (Exception e) {
             e.printStackTrace();
@@ -466,4 +463,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);
+        }
+    }
 }