Todos os produtos
Search
Central de documentação

Object Storage Service:Gerenciar versionamento (SDK para Node.js)

Última atualização: Jul 03, 2026

O versionamento aplica-se a todos os objetos em um bucket. Esse recurso permite recuperar uma versão anterior de objetos sobrescritos ou excluídos acidentalmente.

Um bucket pode assumir três estados de versionamento: sem versionamento (padrão), com versionamento ativado ou com versionamento suspenso. Para obter mais informações sobre o versionamento, consulte Versionamento.

Nota

O versionamento é compatível com as versões 6.8.0 e posteriores do SDK.

Definir o estado de versionamento de um bucket

O código a seguir demonstra como ativar ou suspender o versionamento de um bucket.

const OSS = require('ali-oss');

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // 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 set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function putBucketVersioning() {
  // Set the versioning state of the bucket to Enabled or Suspended.
  const status = 'Enabled'; // `Enabled` or `Suspended`
  const result = await client.putBucketVersioning('BucketName', status);
  console.log(result);
}
putBucketVersioning();

Obter o estado de versionamento de um bucket

O código a seguir demonstra como consultar o estado de versionamento de um bucket.

const OSS = require('ali-oss');

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // 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 set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function getBucketVersioning() {
  // Get the versioning state of the bucket.
  const result = await client.getBucketVersioning('BucketName');
  console.log(result.versionStatus);
}
getBucketVersioning();

Listar todas as versões de objetos em um bucket

O código a seguir demonstra como listar todas as versões de objetos, incluindo marcadores de exclusão, em um bucket específico.

const OSS = require('ali-oss');

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // 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 set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function getBucketVersions() {
  // List all object versions, including delete markers.
  const result = await client.getBucketVersions();
  console.log(result.objects); 
  console.log(result.deleteMarker);
}
getBucketVersions();

Referências

  • Para acessar o código de exemplo completo sobre gerenciamento de versionamento, consulte o exemplo no GitHub.

  • Para obter detalhes sobre a operação da API usada para definir o estado de versionamento de um bucket, consulte PutBucketVersioning.

  • Para obter informações sobre a operação da API usada para consultar o estado de versionamento de um bucket, consulte GetBucketVersioning.

  • Para obter informações sobre a operação da API usada para listar todas as versões de objetos em um bucket, incluindo marcadores de exclusão, consulte ListObjectVersions (GetBucketVersions).