瀏覽代碼

添加获取文件数量的方法

tianboguang 2 年之前
父節點
當前提交
6a11d95e4d

+ 6 - 0
4dkankan-utils-fyun-local/src/main/java/com/fdkankan/fyun/local/LocalFileService.java

@@ -1,6 +1,7 @@
 package com.fdkankan.fyun.local;
 
 import cn.hutool.core.io.FileUtil;
+import com.fdkankan.common.util.FileUtils;
 import com.fdkankan.fyun.constant.FYunConstants;
 import com.fdkankan.fyun.constant.FYunTypeEnum;
 import com.fdkankan.fyun.face.AbstractFYunFileService;
@@ -150,6 +151,11 @@ public class LocalFileService extends AbstractFYunFileService {
         return null;
     }
 
+    @Override
+    public long getSubFileNums(String bucket, String url) {
+        return FileUtils.getSubFileNums(new File(getOssPath(bucket, url)));
+    }
+
     private String getOssPath(String bucket, String filePath) {
         return LocalConstants.BASE_PATH.concat(bucket).concat(File.separator).concat(filePath);
     }

+ 30 - 0
4dkankan-utils-fyun-oss/src/main/java/com/fdkankan/fyun/oss/OssFileService.java

@@ -274,4 +274,34 @@ public class OssFileService extends AbstractFYunFileService {
         return ossClient.generatePresignedUrl(generatePresignedUrlRequest);
     }
 
+    @Override
+    public long getSubFileNums(String bucket, String url) {
+        long totalSubFileNum = 0;
+        try {
+            boolean flag = true;
+            String nextMaker = null;
+            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
+            //指定下一级文件
+            listObjectsRequest.setPrefix(url);
+            //设置分页的页容量
+            listObjectsRequest.setMaxKeys(200);
+            do {
+                //获取下一页的起始点,它的下一项
+                listObjectsRequest.setMarker(nextMaker);
+                ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
+                List<String> collect = objectListing.getObjectSummaries().parallelStream()
+                        .map(OSSObjectSummary::getKey).collect(Collectors.toList());
+                if (!CollectionUtils.isEmpty(collect)) {
+                    totalSubFileNum = totalSubFileNum + collect.size();
+                }
+                nextMaker = objectListing.getNextMarker();
+                //全部执行完后,为false
+                flag = objectListing.isTruncated();
+            } while (flag);
+        } catch (Exception e) {
+            log.error("获取文件数量失败,path:" + url, e);
+        }
+        return totalSubFileNum;
+    }
+
 }

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

@@ -135,4 +135,10 @@ public abstract class AbstractFYunFileService implements FYunFileServiceInterfac
     public URL getPresignedUrl(String url) {
         return getPresignedUrl(fYunFileConfig.getBucket(), url);
     }
+
+
+    @Override
+    public long getSubFileNums(String url) {
+        return getSubFileNums(fYunFileConfig.getBucket(), url);
+    }
 }

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

@@ -324,4 +324,8 @@ public interface FYunFileServiceInterface {
     public URL getPresignedUrl(String url);
 
     public URL getPresignedUrl(String bucket,String url);
+
+    public long getSubFileNums(String url);
+
+    public long getSubFileNums(String bucket,String url);
 }

+ 27 - 0
4dkankan-utils-fyun-s3/src/main/java/com/fdkankan/fyun/s3/S3FileService.java

@@ -301,4 +301,31 @@ public class S3FileService extends AbstractFYunFileService {
                 .withMethod(HttpMethod.PUT).withExpiration(expiration);
         return s3.generatePresignedUrl(generatePresignedUrlRequest);
     }
+
+    @Override
+    public long getSubFileNums(String bucket, String url) {
+        long totalFileNums = 0;
+        try {
+            boolean flag = true;
+            String nextMaker = null;
+            ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
+            listObjectsRequest.setBucketName(bucket);
+            listObjectsRequest.setPrefix(url);
+            listObjectsRequest.setMaxKeys(200);
+
+            do {
+                listObjectsRequest.setMarker(nextMaker);
+                ObjectListing objectListing = s3.listObjects(listObjectsRequest);
+                List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries();
+                List<String> collect = objectSummaries.stream().map(S3ObjectSummary::getKey).collect(Collectors.toList());
+                totalFileNums = totalFileNums + collect.size();
+                nextMaker = objectListing.getNextMarker();
+                flag = objectListing.isTruncated();
+            } while (flag);
+        } catch (Exception e) {
+            log.error("获取文件数量失败,path=" + url, e);
+            e.printStackTrace();
+        }
+        return totalFileNums;
+    }
 }