This topic describes how to delete all tags from an object in Object Storage Service (OSS) by using OSS SDK for C# V2.
Prerequisites
Before you begin, make sure that you have:
An AccessKey pair with the
oss:DeleteObjectTaggingpermission. For more information, see Grant custom access policies to RAM usersThe
AlibabaCloud.OSS.V2NuGet package installed in your project
Background
Object tags are key-value pairs used to classify and manage objects. When tags are no longer needed, delete them with the DeleteObjectTaggingAsync method. This removes all tags from the specified object in a single call.
For more information about object tagging, see Object tagging. For the API specification, see DeleteObjectTagging.
Sample code
The following example uses the China (Hangzhou) region (cn-hangzhou) with a public endpoint. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For more information, see Regions and endpoints.
using OSS = AlibabaCloud.OSS.V2;
// Specify the region and target object.
var region = "cn-hangzhou";
var endpoint = null as string;
var bucket = "<your-bucket-name>";
var key = "<your-object-key>";
// Load the default SDK configuration.
// Credentials are read from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
// If an endpoint is specified, override the default endpoint.
if (endpoint != null)
{
cfg.Endpoint = endpoint;
}
using var client = new OSS.Client(cfg);
var result = await client.DeleteObjectTaggingAsync(new()
{
Bucket = bucket,
Key = key
});
Console.WriteLine("DeleteObjectTagging 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));Replace the following placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
<your-bucket-name> | Bucket name | examplebucket |
<your-object-key> | Object key (full path) | exampledir/exampleobject.txt |
Related operations
Add object tags -- Add or update tags for an object by using
PutObjectTaggingAsync.Query object tags -- Retrieve the tags of an object by using
GetObjectTaggingAsync.
References
For the complete sample code, see DeleteObjectTagging.cs on GitHub.