This topic describes how to use the C# SDK V2 to delete object tags.
Prerequisites
The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) as an example. By default, a public endpoint is used. If you access OSS from another Alibaba Cloud service in the same region, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.Object tags are key-value pairs that are used to manage objects. For more information about object tagging, see Object tagging in the Development Guide.
For more information about how to delete object tags, see DeleteObjectTagging.
To delete object tags, you must have the
oss:DeleteObjectTaggingpermission. For more information, see Grant custom access policies to RAM users.
Sample code
You can use the following code to delete the tags of a specified object in a bucket.
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. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
var endpoint = null as string; // Optional. Specify the endpoint of the OSS service. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name"; // Required. The bucket name.
var key = "your object key"; // Required. The name of the destination object.
// Load the default configurations of the OSS SDK. The configurations automatically read credential information (such as AccessKey) 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 for 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 DeleteObjectTaggingAsync method to delete the tags of the destination object.
var result = await client.DeleteObjectTaggingAsync(new()
{
Bucket = bucket,
Key = key
});
// Print the result information.
Console.WriteLine("DeleteObjectTagging done"); // Indicate 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.References
For the complete sample code for deleting object tags, see DeleteObjectTagging.cs.