Todos os produtos
Search
Central de documentação

Object Storage Service:Políticas de retenção de dados (SDK para Node.js)

Última atualização: Jul 03, 2026

As políticas de retenção de dados do Object Storage Service (OSS) têm o atributo Write Once, Read Many (WORM). Esse recurso permite armazenar e usar dados de modo a impedir sua exclusão e modificação. Para evitar que qualquer usuário, inclusive o proprietário do recurso, modifique ou exclua objetos em um bucket do OSS durante um período específico, defina uma política de retenção de dados para o bucket. Antes do término do período de retenção, você só pode fazer upload e ler objetos no bucket. Após o vencimento desse prazo, é possível modificar ou excluir os objetos.

Nota

Antes de configurar políticas de retenção, familiarize-se com esse recurso. Para mais informações, consulte Políticas de retenção.

Crie uma política de retenção de dados

O código a seguir cria uma política de retenção de dados.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Create a data retention policy.
async function initiateBucketWorm() {
 // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
  // Specify the retention period in days.
  const days = '<Retention Days>'
    const res = await client.initiateBucketWorm(bucket, days)
  console.log(res.wormId)
}

initiateBucketWorm()

Cancele uma política de retenção de dados desbloqueada

O código a seguir cancela uma política de retenção de dados desbloqueada.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Cancel the data retention policy.
async function abortBucketWorm() {
  // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
    try {
    await client.abortBucketWorm(bucket)
    console.log('abort success')
  } catch(err) {
        console.log('err: ', err)
    }
}

abortBucketWorm()

Bloquear uma política de retenção de dados

O código a seguir bloqueia uma política de retenção de dados.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Lock the data retention policy.
async function completeBucketWorm() {
  // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
  const wormId = 'Your Worm Id'
    try {
        await client.completeBucketWorm(bucket, wormId)
  } catch(err) {
      console.log(err)
  }
}

completeBucketWorm()

Visualize uma política de retenção de dados

O código a seguir obtém uma política de retenção de dados.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Get the data retention policy.
async function getBucketWorm() {
  // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
    try {
        const res = await client.getBucketWorm(bucket)
    // View the data retention policy ID.
    console.log(res.wormId)
    // View the status of the data retention policy. The status can be "InProgress" (unlocked) or "Locked".
    console.log(res.state)
    // View the retention period for objects.
    console.log(res.days)
  } catch(err) {
      console.log(err)
  }
}

getBucketWorm()

Estender o período de retenção de objetos

O código a seguir estende o período de retenção de objetos em uma política de retenção de dados bloqueada.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Extend the retention period of objects in a locked data retention policy.
async function extendBucketWorm() {
  // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
  const wormId = 'Your Worm Id'
  const days = 'Retention Days'
    try {
        const res = await client.extendBucketWorm(bucket, wormId, days)
    console.log(res)
  } catch(err) {
      console.log(err)
  }
}

extendBucketWorm()

Referências

  • Para obter o código de exemplo completo sobre políticas de retenção de dados, consulte Exemplos no GitHub.

  • Para mais informações sobre a operação de API para criar uma política de retenção de dados, consulte InitiateBucketWorm.

  • Para mais informações sobre a operação de API para cancelar uma política de retenção de dados desbloqueada, consulte AbortBucketWorm.

  • Para mais informações sobre a operação de API para bloquear uma política de retenção de dados, consulte CompleteBucketWorm.

  • Para mais informações sobre a operação de API para obter uma política de retenção de dados, consulte GetBucketWorm.

  • Para mais informações sobre a operação de API para estender o período de retenção de objetos, consulte ExtendBucketWorm.