-1

How to get s3 bucket/folder size for android and ios i can get folder size using .net c# code from the following link but i couldn't find a code or api to get the same result in ios and android

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

Thank you.

4

3 回答 3

3

这是我用来从 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;
    }
于 2014-08-13T10:36:53.863 回答
1

使用此方法,我从aws s3存储桶中获取文件夹大小!谢谢@Gopi.cs

public void GetFodlerSize(Context context) {

            //s3 get details where you saved s3 credential 
            AmazonS3Client s3 = util.getS3Client(context);

            int Finalsize = 0;

            new AsyncTask<Void, Void, String>() {

                String str = "";
                int Finalsize = 0;

                @Override
                protected void onPreExecute() {
                    dialog = ProgressDialog.show(context,
                            "Loading",
                            "wait");
                }

                @Override
                protected String doInBackground(Void... inputs) {
                    // Queries files in the bucket from S3.

                    ListObjectsRequest listObjectsRequest =
                            new ListObjectsRequest()
                                    .withBucketName(Constants.BUCKET_NAME)
                                    .withPrefix(NAME_OF_FOLDER_YOU_WANT_TO_GET_SIZE);

                    ObjectListing objects = s3.listObjects(listObjectsRequest);

                    for (S3ObjectSummary objectSummary  : objects.getObjectSummaries() ) {
                        str = (objectSummary.getSize() + "");
                        int size = Integer.parseInt(str);

                        Finalsize = Finalsize + size;

                        objects = s3.listNextBatchOfObjects(objects);
                    }

                    String sizeString = null;

                    if (Finalsize > 1024) {
                        Finalsize = Finalsize / 1024;
                        sizeString = " KB";
                    }
                    if (Finalsize > 1024) {
                        Finalsize = Finalsize / 1024;
                        sizeString = " MB";
                    }
                    if (Finalsize > 1024) {
                        Finalsize = Finalsize / 1024;
                        sizeString = " GB";
                    }

                    return ""+Finalsize+sizeString;
                }

                @Override
                protected void onPostExecute(String result) {
                    dialog.dismiss();

                }
            }.execute();
        }

注意:我使用 AsyncTask 是因为如果您在没有 AsyncTask 的情况下运行,那么您将收到类似“不要在主线程上运行”之类的错误

于 2018-05-11T08:33:18.330 回答
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")
            }
        }
    }
于 2018-12-05T06:37:54.007 回答