Tous les produits
Search
Centre de documentation

Object Storage Service:Récupérer les tags d'objet (C# SDK V1)

Dernière mise à jour :Sep 22, 2026

Le tagging d'objet utilise des paires clé-valeur pour identifier les objets. Cette rubrique explique comment interroger les tags d'un objet.

Notes

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Regions and endpoints.

  • Cette rubrique illustre la création d'une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Initialization (C# SDK V1).

  • Pour interroger les tags d'un objet, vous devez disposer de l'autorisation oss:GetObjectTagging. Pour plus d'informations, consultez Grant a custom policy.

Exemple de code

L'exemple de code suivant montre comment interroger les tags d'un objet :

using Aliyun.OSS;
using Aliyun.OSS.Common;

// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket. 
var bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
var objectName = "exampledir/exampleobject.txt";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";

// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();

// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;

// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
    // Query the tags of the object. 
    var result = client.GetObjectTagging(bucketName,objectName);
    Console.WriteLine("get objects tagging succeeded");
    foreach (var tag in result.Tags)
    {
        Console.WriteLine("key:{0}, value:{1}", tag.Key, tag.Value);
    }
}
catch (Exception ex)
{
    Console.WriteLine("get objects tagging failed. {0}", ex.Message);
}

Références

Pour plus d'informations sur l'opération API permettant d'interroger les tags d'un objet, consultez GetObjectTagging.