All Products
Search
Document Center

Object Storage Service:Obtain object tags (C# SDK V2)

Last Updated:Aug 06, 2025

Object tagging uses key-value pairs to mark objects. This topic describes how to obtain the tags of an object.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • To query the tags of an object, you must have the oss:GetObjectTagging permission. For more information, see Attach a custom policy to a RAM user.

Sample code

The following sample code shows how to obtain object tags.

using OSS = AlibabaCloud.OSS.V2;  // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou";  // Required. Specify the region where the bucket is located. In this example, the China (Hangzhou) region is used. Set region to cn-hangzhou.
var endpoint = null as string;  // Optional. Specify the endpoint used to access the OSS service. In this example, the endpoint for the China (Hangzhou) region is used. Set endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name";  // Required. The name of the destination bucket.
var key = "your object name";  // Required. The name of the destination object. The format is folder/objectName.

// Load the default configurations of the OSS SDK. The configurations automatically read credential information such as the AccessKey from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification. The format is OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region in the configuration.
cfg.Region = region;   
// If an endpoint is specified, overwrite the default endpoint. 
if(endpoint != null)
{
    cfg.Endpoint = endpoint;
} 

// Create an OSS client instance based on the configuration information.
using var client = new OSS.Client(cfg); 

// Call the GetObjectTaggingAsync method to obtain the tag information of the destination object.
var result = await client.GetObjectTaggingAsync(new()
 {
    Bucket = bucket,
    Key = key
});

// Print the upload result.
Console.WriteLine("GetObjectTagging done");  // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}");  // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:");  // The response header information.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.
result.Tagging?.TagSet?.Tags?.ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // If the tag information of the destination object exists, print the information.

References

For the complete sample code, see GetObjectTagging.cs.