Delete all tags from an object using the DeleteObjectTagging API in OSS SDK for Go V2.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with the object whose tags you want to delete
Access credentials configured in environment variables. For details, see Configure access credentials.
The
oss:DeleteObjectTaggingpermission granted to your RAM user. For details, see Grant custom policy to RAM users.
Usage notes
The sample code uses
cn-hangzhouas the region ID. By default, a public endpoint is used. To use an internal endpoint for same-region Alibaba Cloud service communication, see OSS regions and endpoints.Object tagging uses key-value pairs to identify objects. For background information, see Object tagging.
For the underlying API specification, see DeleteObjectTagging.
Delete object tags
The following example deletes all tags from a specified object. After the call succeeds, the object has no tags.
Parameters
| Parameter | Type | Description |
|---|---|---|
Bucket | *string | Name of the bucket that contains the object |
Key | *string | Name of the object whose tags you want to delete |
Sample 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 // Region in which the bucket is located.
bucketName string // Name of the bucket.
objectName string // Name of the object.
)
// Specify the init function 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 name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the name of the object is specified.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configurations and specify 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 the tags of the object.
request := &oss.DeleteObjectTaggingRequest{
Bucket: oss.Ptr(bucketName), // Name of the bucket.
Key: oss.Ptr(objectName), // Name of the object.
}
// Delete the tags and process the results.
result, err := client.DeleteObjectTagging(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete object tagging %v", err)
}
// Display the result.
log.Printf("delete object tagging result:%#v\n", result)
}For the complete sample, see the GitHub repository.
What's next
For the full API reference, see DeleteObjectTagging.