This topic describes how to obtain the total storage capacity of a specified bucket, and the number and storage capacity of objects for each storage class in the bucket.
Precautions
You must have the oss:GetBucketStat permission to obtain the storage capacity of a bucket. For more information, see Common examples of RAM policies.
Example code
The following code shows how to obtain the total storage capacity of a bucket named examplebucket, and the number and storage capacity of objects for each storage class in the bucket.
Important
Only Node.js SDK V6.18.0 or later supports all the properties returned in the following example code.
const OSS = require("ali-oss");
const client = new OSS({
// Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: "oss-cn-hangzhou",
// Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Set yourBucketName to the name of the bucket.
bucket: 'yourBucketName',
});
async function getBucketStat() {
// Specify the bucket name. For example, examplebucket.
const bucket = "examplebucket";
try {
const stat = await client.getBucketStat(bucket);
console.log("Total storage capacity of the bucket", stat.stat.Storage);
console.log("Total number of objects in the bucket", stat.stat.ObjectCount);
console.log(
"Number of multipart uploads that are initiated but not completed or aborted",
stat.stat.MultipartUploadCount
);
console.log("Number of LiveChannels", stat.stat.LiveChannelCount);
console.log("All successfully uploaded parts", stat.stat.MultipartPartCount);
console.log("Number of delete markers", stat.stat.DeleteMarkerCount);
console.log("Time when the storage information was retrieved", stat.stat.LastModifiedTime);
console.log("Storage capacity of the Standard storage class", stat.stat.StandardStorage);
console.log("Number of objects in the Standard storage class", stat.stat.StandardObjectCount);
console.log("Billed storage capacity of the Infrequent Access storage class", stat.stat.InfrequentAccessStorage);
console.log(
"Actual storage capacity of the Infrequent Access storage class",
stat.stat.InfrequentAccessRealStorage
);
console.log(
"Number of objects in the Infrequent Access storage class",
stat.stat.InfrequentAccessObjectCount
);
console.log("Billed storage capacity of the Archive Storage class", stat.stat.ArchiveStorage);
console.log("Actual storage capacity of the Archive Storage class", stat.stat.ArchiveRealStorage);
console.log("Number of objects in the Archive Storage class", stat.stat.ArchiveObjectCount);
console.log("Billed storage capacity of the Cold Archive storage class", stat.stat.ColdArchiveStorage);
console.log("Actual storage capacity of the Cold Archive storage class", stat.stat.ColdArchiveRealStorage);
console.log("Number of objects in the Cold Archive storage class", stat.stat.ColdArchiveObjectCount);
console.log(
"Billed storage capacity of the Deep Cold Archive storage class",
stat.stat.DeepColdArchiveStorage
);
console.log(
"Actual storage capacity of the Deep Cold Archive storage class",
stat.stat.DeepColdArchiveRealStorage
);
console.log(
"Number of objects in the Deep Cold Archive storage class",
stat.stat.DeepColdArchiveObjectCount
);
} catch (error) {
console.error("Error obtaining bucket statistics:", error);
}
}
getBucketStat();References
For more information about the API operation, see GetBucketStat.