All Products
Search
Document Center

Object Storage Service:Get object tags (Go SDK V1)

Last Updated:Nov 28, 2025

After you configure tags for an object, you can query the tags of the object. If versioning is enabled for a bucket that stores the object whose tags you want to query, Object Storage Service (OSS) returns the tags of the current version of the object by default. To query the tags of a specified version of the object, you must specify the version ID of the object.

Note
  • Object tagging uses a set of key-value pairs to tag an object. For more information, see Object tagging.

  • For more information about how to retrieve object tags, see GetObjectTagging.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see 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.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.

  • To query the tags of an object, you must have the oss:GetObjectTagging permission. For more information, see Attach a custom policy to a RAM user.

Get the tag information of an object

If versioning is not enabled for the bucket that stores the object whose tags you want to query, you can query the tags of the object based on your requirements. If versioning is enabled for the bucket that stores the object whose tags you want to query, OSS returns the tags of the current version of the object by default.

The following sample code provides an example on how to query the tags of the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

package main

import (
	"fmt"
	"os"

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

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Create an OSSClient instance.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint accordingly.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region accordingly.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	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. Example: examplebucket.
	bucketName := "examplebucket"
	// Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name.
	objectName := "exampledir/exampleobject.txt"

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

	// Get the tag information of the object.
	taggingResult, err := bucket.GetObjectTagging(objectName)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Printf("Object Tagging: %v\n", taggingResult)
}

Get the tag information of a specific version of an object

If versioning is enabled for the bucket that stores the object whose tags you want to query, you can query the tags of a specified version of the object by specifying the version ID of the object.

The following sample code provides an example on how to query the tags of a specified version of the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

Note

For more information about how to query 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() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Create an OSSClient instance.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint accordingly.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region accordingly.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	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. Example: examplebucket.
	bucketName := "examplebucket"
	// Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name.
	objectName := "exampledir/exampleobject.txt"
	// Specify the version ID of the object.
	versionId := "CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****"

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

	// Get the tag information of the specified version of the object.
	taggingResult, err := bucket.GetObjectTagging(objectName, oss.VersionId(versionId))

	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Printf("Object Tagging: %v\n", taggingResult)
}