AWS sdk for ios android java获取文件夹大小

时间:2014-07-22 04:57:10

标签: java android amazon-web-services amazon-s3 swift4

如何获得android和ios的s3存储桶/文件夹大小 我可以使用.net c#代码从以下链接获取文件夹大小但我无法找到代码或api以在ios和android中获得相同的结果

How to check the size of the sub-folder for a folder inside a Amazon S3 bucket

谢谢。

2 个答案:

答案 0 :(得分:2)

这是我用来从s3存储桶android和java代码获取文件夹大小的方法:

public String GetFodlerSize() {

        String str = "";
        int Finalsize = 0;

        AWSCredentials credentials = new BasicAWSCredentials(accesskey,
                secretkey);
        AmazonS3 conn = new AmazonS3Client(credentials);

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(BucketName).withPrefix(FolderName);
        ObjectListing objectListing;

        do {
            objectListing = conn.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing
                    .getObjectSummaries()) {

                str = (objectSummary.getSize() + "");
                int size = Integer.parseInt(str);

                Finalsize = Finalsize + size;

            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());


        return FolderSize;
    }

答案 1 :(得分:1)

为iOS Swift获取AWSS3存储桶文件夹的文件夹大小

func getFileSize(){
        let s3 = AWSS3.default()
        let getReq : AWSS3ListObjectsRequest = AWSS3ListObjectsRequest()
        getReq.bucket = self.bucketName
        getReq.prefix = "your folderPath" //Folder path to get size            
        let downloadGroup = DispatchGroup()
        downloadGroup.enter()

        s3.listObjects(getReq) { (listObjects, error) in

            var total : Int = 0

            if listObjects?.contents != nil {
                for object in (listObjects?.contents)! {
                    do {
                        let s3Object : AWSS3Object = object
                        total += (s3Object.size?.intValue)!
                    }
                }

                print(total)

                let byteCount = total // replace with data.count
                let bcf = ByteCountFormatter()
                bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only
                bcf.countStyle = .file
                let string = bcf.string(fromByteCount: Int64(byteCount))
                print("File size in MB : \(string)")
            }
            else {
                print("contents is blank")
            }
        }
    }