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 utilizar dados de forma que impeça sua exclusão e modificação. Para impedir que todos os usuários, incluindo o proprietário do recurso, modifiquem ou excluam objetos em um bucket 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, só é possível fazer upload e leitura de objetos no bucket. Após o término do período de retenção, você pode modificar ou excluir os objetos.
Antes de configure políticas de retenção, familiarize-se com esse recurso. Para mais informações, consulte Retention policies.
Criar 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()
Cancelar 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()
Obter uma política de retenção de dados
O código a seguir recupera 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 consultar o código de exemplo completo sobre políticas de retenção de dados, acesse 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.