This topic describes how to query the total storage usage of a specific bucket and the number and storage usage of objects of different storage classes in the bucket.
Usage notes
To query the storage usage of a bucket, you must have the oss:GetBucketStat
permission. For more information, see Common examples of RAM policies.
Examples
The following sample code provides an example on how to query the total storage usage of a bucket named examplebucket and the number and storage usage of objects of different storage classes in the bucket.
Only Object Storage Service (OSS) SDK for Node.js 6.18.0 and later support all attributes that are included in the following sample code.
const OSS = require("ali-oss");
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: "oss-cn-hangzhou",
// Obtain access credentials from environment variables. Before you run 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,
// Specify the name of your bucket.
bucket: 'yourBucketName',
});
async function getBucketStat() {
// Specify the name of the bucket. Example: examplebucket.
const bucket = "examplebucket";
try {
const stat = await client.getBucketStat(bucket);
console.log("Total storage usage of the bucket", stat.stat.Storage);
console.log("Total number of objects in the bucket", stat.stat.ObjectCount);
console.log(
"The number of multipart upload tasks that are initialized but are not completed or canceled.",
stat.stat.MultipartUploadCount
);
console.log("The number of LiveChannels", stat.stat.LiveChannelCount);
console.log("All uploaded parts", stat.stat.MultipartPartCount);
console.log("The number of delete markers", stat.stat.DeleteMarkerCount);
console.log("The point in time at which storage information is queried", stat.stat.LastModifiedTime);
console.log("The storage usage of Standard objects", stat.stat.StandardStorage);
console.log("The number of Standard objects", stat.stat.StandardObjectCount);
console.log("The billable storage usage of Infrequent Access (IA) objects", stat.stat.InfrequentAccessStorage);
console.log(
"The actual storage usage of IA objects",
stat.stat.InfrequentAccessRealStorage
);
console.log(
"The number of IA objects",
stat.stat.InfrequentAccessObjectCount
);
console.log("The storage usage of Archive objects", stat.stat.ArchiveStorage);
console.log("The actual storage usage of Archive objects", stat.stat.ArchiveRealStorage);
console.log("The number of Archive objects", stat.stat.ArchiveObjectCount);
console.log("The billable storage usage of Cold Archive objects", stat.stat.ColdArchiveStorage);
console.log("The actual storage usage of Cold Archive objects", stat.stat.ColdArchiveRealStorage);
console.log("The number of Cold Archive objects", stat.stat.ColdArchiveObjectCount);
console.log(
"The billable storage usage of Deep Cold Archive objects",
stat.stat.DeepColdArchiveStorage
);
console.log(
"The actual storage usage of Deep Cold Archive objects",
stat.stat.DeepColdArchiveRealStorage
);
console.log(
"The number of Deep Cold Archive objects",
stat.stat.DeepColdArchiveObjectCount
);
} catch (error) {
console. Error("An error occurred while querying the bucket statistics", error);
}
}
getBucketStat();
References
For more information about the API operation that you can call to query the storage usage of a specific bucket and the number and storage usage of objects of different storage classes in the bucket, see GetBucketStat.