既然答案很多,我想我会自己解决的。我使用LINQPad在C#中编写了实现。复制,粘贴并输入要查询的访问密钥,秘密密钥,区域终结点和存储桶名称。另外,请确保添加AWSSDK nuget软件包。
在我的一个存储桶中进行测试,它得出的计数为128075,大小为70.6GB。我知道这是99.9999%的准确率,所以我对结果很满意。
void Main() {
var s3Client = new AmazonS3Client("accessKey", "secretKey", RegionEndpoint.???);
var stop = false;
var objectsCount = 0;
var objectsSize = 0L;
var nextMarker = string.Empty;
while (!stop) {
var response = s3Client.ListObjects(new ListObjectsRequest {
BucketName = "",
Marker = nextMarker
});
objectsCount += response.S3Objects.Count;
objectsSize += response.S3Objects.Sum(
o =>
o.Size);
nextMarker = response.NextMarker;
stop = response.S3Objects.Count < 1000;
}
new {
Count = objectsCount,
Size = objectsSize.BytesToString()
}.Dump();
}
static class Int64Extensions {
public static string BytesToString(
this long byteCount) {
if (byteCount == 0) {
return "0B";
}
var suffix = new string[] { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
var longBytes = Math.Abs(byteCount);
var place = Convert.ToInt32(Math.Floor(Math.Log(longBytes, 1024)));
var number = Math.Round(longBytes / Math.Pow(1024, place), 1);
return string.Format("{0}{1}", Math.Sign(byteCount) * number, suffix[place]);
}
}