All Products
Search
Document Center

Object Storage Service:Query object tags

Last Updated:Aug 23, 2023

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 key-value pair to tag objects. For more information about object tagging, see Object tagging.

  • For more information about how to query tags of an object, see GetObjectTagging.

Usage notes

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

  • 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.

Query the tags 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 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. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
    objectName := "exampledir/exampleobject.txt"

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

    // Query the tags 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)
}

Query the tags of a specified 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 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"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

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. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
	objectName := "exampledir/exampleobject.txt"
	// Specify the version ID of the object. 
	versionId := "CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****"

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

	// Query the tags 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)
}