Object tagging uses key-value pairs to mark objects. Use GetObjectTaggingAsync to get the tags of an object.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with objects that have tags applied
The
oss:GetObjectTaggingpermission. For details, see Attach a custom policy to a RAM user.
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For more information, see Regions and endpoints.
Get object tags
Call GetObjectTaggingAsync with a Bucket and Key to retrieve the tag set of an object.
using OSS = AlibabaCloud.OSS.V2; // Alias for the Alibaba Cloud OSS SDK V2.
var region = "cn-hangzhou"; // The region where the bucket is located.
var endpoint = null as string; // Optional. Override the default endpoint if needed. For example, set endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name"; // The name of the bucket.
var key = "your object name"; // The object key, for example, folder/objectName.
// Load the default SDK configuration.
// Credentials (OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET) are read from environment variables.
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client.
using var client = new OSS.Client(cfg);
// Retrieve the tags of the specified object.
var result = await client.GetObjectTaggingAsync(new()
{
Bucket = bucket,
Key = key
});
Console.WriteLine("GetObjectTagging done");
Console.WriteLine($"StatusCode: {result.StatusCode}");
Console.WriteLine($"RequestId: {result.RequestId}");
Console.WriteLine("Response Headers:");
result.Headers.ToList().ForEach(x => Console.WriteLine($"{x.Key}: {x.Value}"));
// Print each tag as key: value.
result.Tagging?.TagSet?.Tags?.ForEach(x => Console.WriteLine($"{x.Key}: {x.Value}"));Replace the following placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
your bucket name | The name of the bucket | my-bucket |
your object name | The object key | folder/example.txt |
References
For the complete sample code, see GetObjectTagging.cs.