Supprimez un seul objet, 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.
Notes
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.
Les identifiants d'accès utilisés dans cette rubrique proviennent des variables d'environnement. Pour savoir comment les configurer, consultez Configurer les identifiants d'accès à l'aide du SDK OSS pour Python 1.0.
Cette rubrique explique comment créer une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Initialisation.
La suppression d'un objet nécessite l'autorisation
oss:DeleteObject. Pour plus d'informations, consultez Accorder une politique personnalisée.
Supprimer un seul fichier
Le code suivant supprime l'objet nommé exampleobject.txt du bucket examplebucket :
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Delete the file.
# Replace yourObjectName with the full path of the file to delete. The full path does not include the bucket name. For example, exampledir/exampleobject.txt.
# To delete a folder, set yourObjectName to the folder name. Empty the folder before deleting it.
bucket.delete_object('exampledir/exampleobject.txt')
Supprimer plusieurs fichiers
Vous pouvez supprimer jusqu'à 1 000 objets simultanément par nom, par préfixe ou par répertoire.
Vous pouvez également configurer des règles de cycle de vie pour supprimer automatiquement les objets. Pour plus d'informations, consultez Règles de cycle de vie basées sur la dernière heure de modification.
Supprimer plusieurs objets avec des noms spécifiés
Le code suivant supprime plusieurs objets par nom :
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Batch delete three files. You can delete up to 1,000 files in a single request.
# Specify the full paths of the three files to delete. The full paths do not include the bucket name.
result = bucket.batch_delete_objects(['exampleobject1.jpg', 'testobject2.png', 'destobject3.txt'])
# Print the names of the successfully deleted files.
print('\n'.join(result.deleted_keys))
Supprimer plusieurs objets avec le préfixe de nom d'objet spécifié ou dans le répertoire spécifié
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.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Set the prefix for deletion. Use 'src' to delete all files with 'src' prefix.
# Use 'src/' to delete only the folder 'src' and all files within it.
# prefix = 'src/'
# Delete multiple files.
for obj in oss2.ObjectIterator(bucket, prefix=prefix):
bucket.delete_object(obj.key)
Références
Exemple de code complet pour la suppression de fichiers : Exemples GitHub.
Opération API pour la suppression d'un seul fichier : DeleteObject.
Opération API pour la suppression de plusieurs fichiers : DeleteMultipleObjects.