Tous les produits
Search
Centre de documentation

Object Storage Service:Supprimer des objets (SDK OSS pour Python 2.0)

Dernière mise à jour :Aug 18, 2026

Cette rubrique explique comment utiliser le SDK OSS pour Python 2.0 afin de supprimer un objet unique ou plusieurs objets.

Remarques sur l'utilisation

  • Le code d'exemple présenté dans cette rubrique utilise par défaut l'ID de région Chine (Hangzhou) cn-hangzhou et un endpoint public. Si vous souhaitez 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 correspondances entre les régions OSS et les endpoints, consultez Régions et endpoints.

  • Pour supprimer un objet, vous devez disposer de l'autorisation oss:DeleteObject. Pour en savoir plus, consultez la section Accorder des autorisations personnalisées à un utilisateur RAM.

Définition de la méthode

Supprimer un objet unique

delete_object(request: DeleteObjectRequest, **kwargs) → DeleteObjectResult

Supprimer plusieurs objets

delete_multiple_objects(request: DeleteMultipleObjectsRequest, **kwargs) → DeleteMultipleObjectsResult

Paramètres de requête

Parameter

Type

Description

request

DeleteObjectRequest

Les paramètres de la requête, tels que le nom de l'objet. Pour plus d'informations, consultez DeleteObjectRequest

DeleteMultipleObjectsRequest

Les paramètres de la requête, tels que la liste des objets à supprimer. Pour plus d'informations, consultez DeleteMultipleObjectsRequest

Valeurs de retour

Type

Description

DeleteObjectResult

La valeur de retour. Pour plus d'informations, consultez DeleteObjectResult

DeleteMultipleObjectsResult

La valeur de retour. Pour plus d'informations, consultez DeleteMultipleObjectsResult

  • Pour consulter la définition complète de la méthode de suppression d'un objet unique, reportez-vous à la documentation delete_object.

  • Pour consulter la définition complète de la méthode de suppression de plusieurs objets, reportez-vous à la documentation delete_multiple_objects.

Exemples de code

Supprimer un objet unique

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="delete object sample")
# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is not required.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --key command-line argument, which specifies the name of the object. This argument is required.
parser.add_argument('--key', help='The name of the object.', required=True)

def main():
    args = parser.parse_args()  # Parse command-line arguments.

    # Load credentials from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Load the default configurations of the SDK and set the credentials provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    # Set the region in the configuration.
    cfg.region = args.region
    # If the endpoint parameter is provided, set the endpoint in the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client using the configured information.
    client = oss.Client(cfg)

    # Execute a request to delete an object. Specify the bucket name and object name.
    result = client.delete_object(oss.DeleteObjectRequest(
        bucket=args.bucket,
        key=args.key,
    ))

    # Print the status code, request ID, version ID, and delete marker of the request to check whether the request is successful.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
          f' delete marker: {result.delete_marker},'
    )

if __name__ == "__main__":
    main()  # The entry point of the script. The main function is called when the file is directly run.

Supprimer plusieurs objets spécifiés

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="delete multiple objects sample")
# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is not required.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --key command-line argument, which specifies the name of the object. This argument is required.
parser.add_argument('--key', help='The name of the object.', required=True)
# Note: If you want to delete multiple objects, add the --key2 command-line argument, which specifies the name of the object.
parser.add_argument('--key2', help='The name of the object.', required=True)

def main():
    # Parse command-line arguments.
    args = parser.parse_args()

    # Load credentials from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Load the default configurations of the SDK and set the credentials provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    # Set the region in the configuration.
    cfg.region = args.region
    # If the endpoint parameter is provided, set the endpoint in the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client using the configured information.
    client = oss.Client(cfg)

    # Define the list of objects to delete.
    # Note: If you want to delete multiple objects, expand the objects list in the following format.
    objects = [oss.DeleteObject(key=args.key), oss.DeleteObject(key=args.key2)]

    # Execute a request to delete multiple objects. Specify the bucket name, encoding type, and object list.
    result = client.delete_multiple_objects(oss.DeleteMultipleObjectsRequest(
        bucket=args.bucket,
        encoding_type='url',
        objects=objects,
    ))

    # Print the status code, request ID, and information about the deleted objects to check whether the request is successful.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' key: {result.deleted_objects[0].key},'
          f' version id: {result.deleted_objects[0].version_id},'
          f' delete marker: {result.deleted_objects[0].delete_marker},'
          f' delete marker version id: {result.deleted_objects[0].delete_marker_version_id},'
          f' encoding type: {result.encoding_type},'
    )

if __name__ == "__main__":
    main() # The entry point of the script. The main function is called when the file is directly run.

Références

  • Pour obtenir l'exemple de code complet relatif à la suppression d'un objet unique, consultez le fichier delete_object.py.

  • Pour obtenir l'exemple de code complet relatif à la suppression de plusieurs objets, consultez le fichier delete_multiple_objects.py.