使用此方法,我从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 的情况下运行,那么您将收到类似“不要在主线程上运行”之类的错误