This topic describes how to delete the tags of an object using OSS SDK for Go V2.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhoufor the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket using other Alibaba Cloud services in the same region as the bucket, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To delete object tags, you must have the
oss:DeleteObjectTaggingpermission. For more information, see Grant custom policy to RAM users.
Object tagging uses a key-value pair to identify objects. For more information about object tagging, see Object tagging in OSS Developer Guide.
For more information about how to delete the tags of an object, see DeleteObjectTagging.
Sample code
The following code provides an example of how to delete the tags of an object in a bucket.
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)
}
References
For the complete sample code used to delete the tags of an object, visit GitHub repository.
For more information about the API operation that you can call to delete the tags of an object, see DeleteObjectTagging.