When versioning is enabled on a bucket, OSS deletes the tags of the current version of an object by default. Specify a version ID to delete the tags of a specific version.
Prerequisites
Before you begin, ensure that you have:
The
oss:DeleteObjectTaggingpermission. For more information, see Grant custom permissions to a RAM user.Access credentials configured via environment variables. For more information, see Configure access credentials.
Usage notes
Object tags are key-value pairs that identify objects. For more information, see Object tagging.
If you do not specify a version ID, OSS deletes the tags of the current version. To get the version ID of an object, see List objects.
For more information about the DeleteObjectTagging API operation, see DeleteObjectTagging.
Delete tags for a specific version
Key parameters
| Parameter | Type | Description |
|---|---|---|
Bucket | *string | The bucket name. |
Key | *string | The object name. |
VersionId | *string | The version ID of the object whose tags are deleted. If not specified, OSS deletes the tags of the current version. |
Sample code
The following example uses cn-hangzhou as the region. Replace the placeholder values with your actual values before running the code.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // The region.
bucketName string // The bucket name.
objectName string // The object name.
)
// The init function is used to initialize command-line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
// Parse command-line parameters.
flag.Parse()
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the object name is empty.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configurations and set the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to delete object tags.
request := &oss.DeleteObjectTaggingRequest{
Bucket: oss.Ptr(bucketName), // The bucket name.
Key: oss.Ptr(objectName), // The object name.
VersionId: oss.Ptr("yourVersionId"), // Specify the actual version ID.
}
// Delete the object tags and process the result.
result, err := client.DeleteObjectTagging(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete object tagging %v", err)
}
// Print the result of deleting the object tags.
log.Printf("delete object tagging result:%#v\n", result)
}The sample code uses an Internet endpoint. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For the full list of regions and endpoints, see Regions and endpoints.
References
For the complete sample code, see GitHub.
For the API reference, see DeleteObjectTagging.