Exclua objetos individuais, vários objetos ou objetos por prefixo de forma permanente ou temporária em um bucket com versionamento ativado.
Comportamento de exclusão em buckets com versionamento
Ao excluir um objeto de um bucket com versionamento, decida se deseja especificar um ID de versão.
-
Excluir um objeto sem especificar um ID de versão (exclusão temporária)
Se você não especificar um ID de versão, o OSS não excluirá a versão atual. Em vez disso, adicionará uma marca de exclusão como a versão mais recente. As solicitações GetObject subsequentes retornam
404 Not Foundcomheader:x-oss-delete-marker = truee ox-oss-version-idda marca de exclusão na resposta.Quando
x-oss-delete-markeré verdadeiro,x-oss-version-idretorna o ID de versão da marca de exclusão. -
Excluir um objeto especificando um ID de versão (exclusão permanente)
Se você especificar um ID de versão, o OSS excluirá permanentemente essa versão usando o parâmetro
versionIdemparams. Para excluir a versão com ID nulo, definaparams['versionId'] = "null"emparams. O OSS trata a string "null" como o ID de versão de destino.
Excluir um único arquivo
Exclua um objeto de um bucket com versionamento de forma permanente ou temporária.
-
Exclusão permanente
Especifique o ID de versão para excluir permanentemente um objeto:
const OSS = require("ali-oss"); const client = new OSS({ // Replace yourregion with 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 running this sample code, make sure 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, // Replace yourbucketname with the bucket name. bucket: 'yourbucketname' }); // Specify the versionId of the object. const versionId = "versionId"; // Specify the object. const objectName = "exampleobject.txt"; async function deleteVersionObject() { const result = await client.delete(objectName, { versionId, }); console.log(result); } deleteVersionObject(); -
Exclusão temporária
Não especifique um ID de versão para excluir temporariamente um objeto:
const OSS = require('ali-oss'); const client = new OSS({ // Replace yourregion with 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 running this sample code, make sure 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, // Replace yourbucketname with the bucket name. bucket: 'yourbucketname' }); // Temporarily delete the object without specifying a versionId. This operation adds a delete marker to the object. // Specify the object. const objectName = "exampleobject.txt"; async function deleteObject() { const result = await client.delete(objectName); console.log(result); } deleteObject();
Excluir vários arquivos
Exclua vários objetos de forma permanente ou temporária.
-
Exclusão permanente
Especifique os IDs de versão para excluir permanentemente vários objetos e marcas de exclusão:
const OSS = require('ali-oss'); const client = new OSS({ // Replace yourregion with 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 running this sample code, make sure 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, // Replace yourbucketname with the bucket name. bucket: 'yourbucketname' }); // Delete multiple objects or delete markers by specifying their versionIds. const names = [ { key: 'key1.js', versionId: 'versionId1' }, { key: 'key2.js', versionId: 'versionId2' } ]; async function deleteMulti() { const result = await client.deleteMulti(names); console.log(result); } deleteMulti(); -
Exclusão temporária
O código a seguir mostra como excluir temporariamente vários objetos sem especificar seus versionIds. É possível recuperar versões de objetos excluídas temporariamente.
const OSS = require('ali-oss'); const client = new OSS({ // Replace yourregion with 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 running this sample code, make sure 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, // Replace yourbucketname with the bucket name. bucket: 'yourbucketname' }); const names = ['key1.js', 'key2.js']; async function deleteMulti() { // Delete multiple objects without specifying versionIds. const result = await client.deleteMulti(names); console.log(result); } deleteMulti();
Excluir objetos por prefixo
Exclua objetos com um prefixo específico:
const OSS = require("ali-oss");
const client = new OSS({
// Replace yourregion with 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 running this sample code, make sure 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,
// Replace yourbucketname with the bucket name.
bucket: 'yourbucketname'
});
// Specify the prefix of the objects to delete.
const prefix = "test";
async function deleteMutiPrefix() {
// Get the versionId information for objects with the specified prefix.
const list = await client.getBucketVersions({
prefix: prefix,
});
for (let i = 0; i < list.objects.length; i++) {
const obj = list.objects[i];
// Delete the object with the specified prefix.
const versionId = obj.versionId;
await client.delete(obj.name, {
versionId,
});
}
}
deleteMutiPrefix();
Referências
API de exclusão de objeto único: DeleteObject
API de exclusão em lote: DeleteMultipleObjects