Elimine de forma permanente o temporal un objeto, varios objetos o los objetos que coincidan con un prefijo dentro de un bucket con control de versiones habilitado.
Comportamiento de la eliminación en buckets con control de versiones
Cuando elimina un objeto de un bucket con control de versiones, debe decidir si especifica un identificador de versión.
-
Eliminar un objeto sin especificar un identificador de versión (eliminación temporal)
Si no especifica un identificador de versión, OSS no elimina la versión actual. En su lugar, agrega un marcador de eliminación como la versión más reciente. Las solicitudes GetObject posteriores devuelven
404 Not Foundconheader:x-oss-delete-marker = truey el valorx-oss-version-iddel marcador de eliminación en la respuesta.Cuando
x-oss-delete-markeres true,x-oss-version-iddevuelve el identificador de versión del marcador de eliminación. -
Eliminar un objeto al especificar un identificador de versión (eliminación permanente)
Si especifica un identificador de versión, OSS elimina permanentemente esa versión mediante el parámetro
versionIdenparams. Para eliminar la versión con un identificador nulo, establezcaparams['versionId'] = "null"enparams. OSS interpreta la cadena "null" como el identificador de versión de destino.
Eliminar un solo archivo
Elimine un objeto de manera permanente o temporal desde un bucket con control de versiones.
-
Eliminación permanente
Para eliminar un objeto de forma permanente, especifique su identificador de versión:
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(); -
Eliminación temporal
Para eliminar un objeto de forma temporal, omita el identificador de versión:
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();
Eliminar varios archivos
Elimine varios objetos de forma permanente o temporal.
-
Eliminación permanente
Especifique los identificadores de versión para eliminar de forma permanente varios objetos y marcadores de eliminación:
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(); -
Eliminación temporal
A continuación se muestra cómo eliminar varios objetos de forma temporal sin indicar sus versionIds. Las versiones eliminadas temporalmente se pueden recuperar.
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();
Eliminar objetos por prefijo
Elimine los objetos que coincidan con un prefijo determinado:
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();
Referencias
API para eliminar un solo objeto: DeleteObject
API para eliminar objetos por lotes: DeleteMultipleObjects