Delete object tags using Go

更新时间:
复制 MD 格式

Use the OSS Go SDK to remove all tags from an object. When versioning is enabled, OSS removes tags from the current version by default. To target a specific version, pass its version ID.

Object tagging uses key-value pairs to tag objects. For more information, see Object tagging and DeleteObjectTagging.

Prerequisites

Before you begin, ensure that you have:

Usage notes

  • The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For more information, see Regions and endpoints.

  • The examples create an OSSClient instance using an OSS endpoint. To create an instance using a custom domain name or Security Token Service (STS), see Configure OSSClient instances.

Delete tags from an object

Both scenarios use the same method. The only difference is whether you pass a version ID:

ScenarioMethod call
No versioning, or delete tags from the current versionbucket.DeleteObjectTagging(objectName)
Delete tags from a specific versionbucket.DeleteObjectTagging(objectName, oss.VersionId(versionId))

Delete tags from the current version

If versioning is not enabled, or if versioning is enabled and you want to remove tags from the current version, call DeleteObjectTagging with the object name only.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Load credentials from environment variables.
	// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance.
	// Replace yourEndpoint with the endpoint for your bucket's region,
	// for example, https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
	// Replace yourRegion with the region ID, for example, cn-hangzhou.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Specify the bucket name and the full object path (excluding the bucket name).
	bucketName := "examplebucket"
	objectName := "exampledir/exampleobject.txt"

	bucket, err := client.Bucket(bucketName)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Delete all tags from the object.
	err = bucket.DeleteObjectTagging(objectName)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println("Object tags deleted.")
}

Delete tags from a specific version

If versioning is enabled and you want to remove tags from a specific version, pass the version ID as an additional argument. To get the version ID of an object, see List objects.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

func main() {
	// Load credentials from environment variables.
	// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance.
	// Replace yourEndpoint with the endpoint for your bucket's region,
	// for example, https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
	// Replace yourRegion with the region ID, for example, cn-hangzhou.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		HandleError(err)
	}

	// Specify the bucket name, the full object path, and the version ID.
	bucketName := "examplebucket"
	objectName := "exampledir/exampleobject.txt"
	versionId := "CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****"

	bucket, err := client.Bucket(bucketName)
	if err != nil {
		HandleError(err)
	}

	// Delete all tags from the specified version of the object.
	err = bucket.DeleteObjectTagging(objectName, oss.VersionId(versionId))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println("Object tags deleted.")
}

What's next