Supprimez un objet unique, plusieurs objets par nom, les objets correspondant à un préfixe ou un répertoire entier.
Les objets supprimés sont irrécupérables. Procédez avec prudence.
Supprimer un fichier unique
Le code suivant supprime un objet nommé exampleobject.txt d’un bucket nommé examplebucket :
const OSS = require('ali-oss');
const client = new OSS({
// Specify 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: 'oss-cn-hangzhou',
// 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,
// Specify the bucket name.
bucket: 'examplebucket',
});
async function deleteObject() {
try {
// Specify the full path of the object. The full path cannot contain the bucket name.
const result = await client.delete('exampleobject.txt');
console.log(result);
} catch (error) {
console.log(error);
}
}
deleteObject();
Supprimer des fichiers par lot
Vous pouvez supprimer jusqu’à 1 000 objets à la fois par nom, par préfixe ou par répertoire.
La réponse à une opération de suppression par lot peut prendre l’une des deux formes suivantes :
Mode détaillé : le corps de la réponse d’OSS contient le résultat pour chaque objet supprimé. Il s’agit du mode par défaut.
Mode silencieux : OSS ne renvoie aucun corps de réponse.
Vous pouvez également configurer des règles de cycle de vie pour supprimer automatiquement les objets. Pour plus d’informations, consultez la rubrique Règles de cycle de vie basées sur la dernière heure de modification.
Delete multiple objects with specified names
Le code suivant supprime plusieurs objets par nom :
const OSS = require('ali-oss');
const client = new OSS({
// Specify 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: 'oss-cn-hangzhou',
// 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,
// Specify the bucket name.
bucket: 'examplebucket',
});
async function deleteMulti() {
try {
// Specify the full paths of the objects to delete and set the response mode to verbose. The full paths cannot contain the bucket name.
// let result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt']);
// console.log(result);
// Specify the full paths of the objects to delete and set the response mode to quiet. The full paths cannot contain the bucket name.
const result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt'], {quiet: true});
console.log(result);
} catch (error) {
console.log(error);
}
}
deleteMulti();
Delete multiple objects with the specified object name prefix or in the specified directory
Le code suivant supprime des objets par préfixe ou un répertoire entier :
Si le préfixe n’est pas spécifié ou est défini sur NULL, tous les objets du bucket sont supprimés. Spécifiez les préfixes avec soin.
const OSS = require('ali-oss');
const client = new OSS({
// Specify 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: 'oss-cn-hangzhou',
// 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,
// Specify the bucket name.
bucket: 'examplebucket',
});
// Handle failed requests to prevent promise.all from breaking, and return the failure reason and the failed file name.
async function handleDel(name) {
try {
await client.delete(name);
} catch (error) {
error.failObjectName = name;
return error;
}
}
// Delete multiple files.
async function deletePrefix(prefix) {
const list = await client.list({
prefix: prefix,
});
list.objects = list.objects || [];
const result = await Promise.all(list.objects.map((v) => handleDel(v.name)));
console.log(result);
}
// To delete all objects that have the 'src' prefix, set prefix to 'src'. This deletes files such as 'src_file.txt', the 'src' folder, and all files within the 'src' folder.
deletePrefix('src');
// To delete only the 'src' folder and all files within it, set prefix to 'src/'.
// deletePrefix('src/');
Références
Pour obtenir un exemple de code complet illustrant la suppression d’un ou de plusieurs fichiers, consultez l’exemple GitHub.
Pour la référence API relative à la suppression d’un fichier unique, consultez la rubrique DeleteObject.
Pour la référence API relative à la suppression de plusieurs fichiers, consultez la rubrique DeleteMultipleObjects.