Set object tags (C# SDK V2)
Object Storage Service (OSS) lets you use object tagging to classify objects in buckets. This topic describes how to use the C# SDK V2 to set object tags.
Important
-
The sample code in this topic uses China (Hangzhou) as an example. The region ID is
cn-hangzhou. By default, the public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see OSS regions and endpoints. -
To set object tags, you must have the
oss:PutObjectTaggingpermission. For more information, see Grant custom permissions to a RAM user.
Sample code
You can use the following code to set tags for an object.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.
var region = "cn-hangzhou"; // Required. The region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
var endpoint = null as string; // Optional. The endpoint used to access OSS. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name"; // Required. The name of the bucket.
var key = "your object name"; // Required. The name of the object.
// Load the default configurations of the OSS SDK. The configurations automatically read credential information (such as AccessKeys) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification (format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket in the configuration.
cfg.Region = region;
// If an endpoint is specified, it overwrites the default endpoint.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance using the configuration information.
using var client = new OSS.Client(cfg);
// Call the PutObjectTaggingAsync method to set tags for the destination object.
var result = await client.PutObjectTaggingAsync(new()
{
Bucket = bucket,
Key = key,
Tagging = new()
{
TagSet = new()
{
Tags = [
new OSS.Models.Tag() { Key = "tag1", Value = "value1" }, // The key-value pair of a tag.
new OSS.Models.Tag() { Key = "tag2", Value = "value2" }, // The key-value pair of a tag.
]
}
}
});
// Print the result.
Console.WriteLine("PutObjectTagging 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.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.
References
For the complete sample code used to set object tags, see PutObjectTagging.cs.