Supprimez définitivement ou temporairement un objet unique, plusieurs objets ou des objets par préfixe dans un bucket pour lequel le versioning est activé.
Comportement de suppression dans les buckets versionnés
Lorsque vous supprimez un objet d'un bucket versionné, vous devez indiquer si vous souhaitez spécifier un ID de version.
-
Supprimer un objet sans spécifier d'ID de version (suppression temporaire)
Si vous ne spécifiez pas d'ID de version, OSS ne supprime pas la version actuelle. Il ajoute plutôt un marqueur de suppression en tant que dernière version. Les requêtes GetObject ultérieures renvoient
404 Not Foundavecheader:x-oss-delete-marker = trueet l'x-oss-version-iddu marqueur de suppression dans la réponse.Lorsque
x-oss-delete-markerest défini sur true,x-oss-version-idrenvoie l'ID de version du marqueur de suppression. -
Supprimer un objet en spécifiant un ID de version (suppression définitive)
Si vous spécifiez un ID de version, OSS supprime définitivement cette version à l'aide du paramètre
versionIddansparams. Pour supprimer la version dont l'ID est null, définissezparams['versionId'] = "null"dansparams. OSS traite la chaîne « null » comme l'ID de version cible.
Supprimer un fichier unique
Supprimez définitivement ou temporairement un objet d'un bucket versionné.
-
Suppression définitive
Supprimez définitivement un objet en spécifiant son ID de version :
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(); -
Suppression temporaire
Supprimez temporairement un objet sans spécifier d'ID de version :
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();
Supprimer plusieurs fichiers
Supprimez définitivement ou temporairement plusieurs objets.
-
Suppression définitive
Supprimez définitivement plusieurs objets et marqueurs de suppression en spécifiant les ID de version :
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(); -
Suppression temporaire
Le code suivant montre comment supprimer temporairement plusieurs objets sans spécifier leurs ID de version. Les versions d'objets supprimées temporairement peuvent être récupérées.
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();
Supprimer des objets par préfixe
Supprimez les objets ayant un préfixe spécifié :
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();
Références
API de suppression d'objet unique : DeleteObject
API de suppression par lot : DeleteMultipleObjects