Object tagging uses key-value pairs to classify and identify objects. This page shows how to retrieve the tags on an object using the OSS SDK for C#.
Prerequisites
Before you begin, make sure that you have:
The
oss:GetObjectTaggingpermission. For details, see Attach a custom policy to a RAM user.An OSSClient instance initialized with a valid endpoint and credentials. For details, see Initialization.
Usage notes
The examples on this page use the public endpoint for the China (Hangzhou) region (
https://oss-cn-hangzhou.aliyuncs.com). To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.To initialize an OSSClient instance using a custom domain name or Security Token Service (STS), see Initialization.
Get object tags
Call GetObjectTagging(bucketName, objectName) to retrieve all tags on an object. The method returns a result object with a Tags collection. Each entry exposes a Key and Value property.
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint for the region where your bucket is located.
// Example: https://oss-cn-hangzhou.aliyuncs.com
var endpoint = "yourEndpoint";
// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name and the full object path (excluding the bucket name).
var bucketName = "examplebucket";
var objectName = "exampledir/exampleobject.txt";
// Specify the region where the bucket is located.
const string region = "cn-hangzhou";
// Configure the client to use signature algorithm V4.
var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Retrieve the object tags.
var result = client.GetObjectTagging(bucketName, objectName);
Console.WriteLine("Get object tags succeeded.");
foreach (var tag in result.Tags)
{
Console.WriteLine("key:{0}, value:{1}", tag.Key, tag.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Get object tags failed. {0}", ex.Message);
}API reference
For the underlying API operation, see GetObjectTagging.