O versionamento se aplica a todos os objetos de um bucket. Esse recurso permite restaurar objetos a uma versão anterior caso sejam sobrescritos ou excluídos acidentalmente.
Um bucket pode estar em um dos três estados de versionamento: não versionado (padrão), versionamento ativado ou versionamento suspenso. Para mais informações sobre o versionamento, consulte Versioning.
O versionamento é compatível com as versões 6.8.0 e posteriores do kit de desenvolvimento de software (SDK).
Defina o estado de versionamento de um bucket
O código a seguir mostra 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();
Obtenha o estado de versionamento de um bucket
O código a seguir mostra como obter 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();
Liste todas as versões de objetos em um bucket
O código a seguir mostra como listar todas as versões de objetos, incluindo marcadores de exclusão, em um bucket especificado.
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 o código de exemplo completo de gerenciamento de versionamento, consulte o exemplo no GitHub.
Para obter informações sobre a operação da API para definir o estado de versionamento de um bucket, consulte PutBucketVersioning.
Para obter informações sobre a operação da API para obter o estado de versionamento de um bucket, consulte GetBucketVersioning.
Para obter informações sobre a operação da API para listar todas as versões de objetos em um bucket, incluindo marcadores de exclusão, consulte ListObjectVersions (GetBucketVersions).