Todos os produtos
Search
Central de documentação

Object Storage Service:Obter a capacidade de armazenamento de um bucket (SDK do Node.js)

Última atualização: Jul 03, 2026

Consulte a capacidade total de armazenamento de um bucket, além da quantidade e do volume de objetos por classe de armazenamento.

Precauções

A permissão oss:GetBucketStat é obrigatória para obter a capacidade de armazenamento de um bucket. Para mais informações, consulte Exemplos comuns de políticas do RAM.

Código de exemplo

O código a seguir obtém a capacidade total de armazenamento do bucket examplebucket, bem como a quantidade e o volume de objetos de cada classe de armazenamento.

Importante

Somente o SDK do Node.js V6.18.0 ou posterior oferece suporte a todas as propriedades retornadas no código de exemplo a seguir.

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();

Referências

Para obter detalhes sobre a operação de API usada neste tópico, consulte GetBucketStat.