Delete object tags (Go SDK V2)
When versioning is enabled for a bucket, OSS by default removes tags from only the current version of an object. To remove tags from a specific version, you must provide its versionId in the request.
Prerequisites
-
The sample code in this topic uses the region ID
cn-hangzhouof 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 from other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints. -
In this topic, access credentials are obtained from environment variables. For more information about the regions and endpoints supported by OSS, see Configure access credentials.
-
To delete object tags, you must have the
oss:DeleteObjectTaggingpermission. For more information, see Grant custom permissions to a RAM user.
-
A key-value pair is used to identify objects. For more information about object tagging, see Tag objects.
-
For more information about how to delete the tags of an object, see DeleteObjectTagging.
Sample code
-
If versioning is enabled for the bucket that stores the object whose tags you want to delete, you can delete the tags of a specified version of the object by specifying the version ID. If you do not specify the version ID of the object, only the tags of its current version are deleted.
-
For more information about how to query the version ID of an object, see List objects (OSS SDK for Go 1.0).
Below is the sample code for deleting the tags of a specific version 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 // 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)
}
References
-
For the complete sample code used to delete object tags, see GitHub.
-
For more information about the API operation to delete object tags, see DeleteObjectTagging.