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.

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() {
    // 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. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    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 of the object cannot contain the bucket name. 
    objectName := "exampledir/exampleobject.txt"

    // Query the bucket information. 
    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 obtain the version IDs 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() {
    // 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. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }
    // Specify the bucket name. Example: examplebucket. 
    bucketName := "examplebucket"
    // Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. 
    objectName := "exampledir/exampleobject.txt"
    // Specify the version ID of the object. 
    versionId := "CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****"

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