Tous les produits
Search
Centre de documentation

Object Storage Service:Interroger les tags d'un objet à l'aide du SDK OSS pour Python 2.0

Dernière mise à jour :Aug 18, 2026

Interrogez les tags d'un objet à l'aide du SDK OSS pour Python 2.0.

Remarques sur l'utilisation

  • L'exemple de code de cette rubrique utilise la région Chine (Hangzhou) (cn-hangzhou) et un endpoint public. Pour accéder à OSS depuis d'autres services Alibaba Cloud dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et endpoints pris en charge, consultez Régions et endpoints.

  • Les tags d'objet sont des paires clé-valeur. Pour plus d'informations, consultez Ajouter des tags aux objets.

  • Pour connaître l'opération API utilisée pour interroger les tags d'objet, consultez GetObjectTagging.

  • Pour interroger les tags d'objet, vous devez disposer de l'autorisation oss:GetObjectTagging. Pour plus d'informations, consultez Accorder des autorisations personnalisées à un utilisateur RAM.

Définition de la méthode

get_object_tagging(request: GetObjectTaggingRequest, **kwargs) → GetObjectTaggingResult

Paramètres de requête

Paramètre

Type

Description

request

GetObjectTaggingRequest

Les paramètres de la requête. Pour plus d'informations, consultez GetObjectTaggingRequest

Valeurs de retour

Type

Description

GetObjectTaggingResult

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

Pour la définition complète de la méthode, consultez get_object_tagging.

Exemple de code

Le code suivant interroge les tags d'un objet spécifié dans un bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser to process parameters passed in by users from the command line.
parser = argparse.ArgumentParser(description="get object tagging sample")

# Add required and optional command-line arguments.
# --region: The region where the OSS bucket is located.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# --bucket: The name of the bucket.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# --endpoint: An optional parameter that specifies the domain name used to access OSS.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# --key: The key of the object (file) in OSS.
parser.add_argument('--key', help='The name of the object.', required=True)

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

    # Load the required authentication credentials for OSS from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default configurations provided by the SDK.
    cfg = oss.config.load_default()

    # Set the credential provider to the previously created object.
    cfg.credentials_provider = credentials_provider

    # Set the region for the OSS client based on user input.
    cfg.region = args.region

    # If the user provides a custom endpoint, update the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client instance using the preceding configurations.
    client = oss.Client(cfg)

    # Query the tag of the specified object.
    result = client.get_object_tagging(oss.GetObjectTaggingRequest(
        bucket=args.bucket,
        key=args.key,
    ))

    # Print the result.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
    )

    # If tags exist, traverse and print the key-value pair of each tag.
    if result.tag_set.tags:
        for o in result.tag_set.tags:
            print(f'tags key: {o.key}, tags value: {o.value}')

# Call the main function when this script is run.
if __name__ == "__main__":
    main()

Référence