All Products
Search
Document Center

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

Last Updated:Nov 28, 2025

Object Storage Service (OSS) lets you use object tagging to classify objects in buckets. You can set lifecycle rules, access permissions, and other configurations for objects that have the same tags.

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 set object tags, you must have the oss:PutObjectTagging permission. For more information, see Attach a custom policy to a RAM user.

Add object tags during an upload

  • Add object tags during a simple upload

    The following code provides an example of how to add object tags during a simple upload.

    package main
    
    import (
    	"fmt"
    	"os"
    	"strings"
    
    	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
    	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    	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 the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual Endpoint.
    	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
    	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. For example, examplebucket.
    	bucketName := "examplebucket"
    	// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
    	objectName := "exampledir/exampleobject.txt"
    
    	// Get the bucket.
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and value for each object tag. For example, set the key to owner and the value to John.
    	tag1 := oss.Tag{
    		Key:   "owner",
    		Value: "John",
    	}
    	tag2 := oss.Tag{
    		Key:   "type",
    		Value: "document",
    	}
    	tagging := oss.Tagging{
    		Tags: []oss.Tag{tag1, tag2},
    	}
    
    	// Set the object tags.
    	err = bucket.PutObject(objectName, strings.NewReader("Hello OSS"), oss.SetTagging(tagging))
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	fmt.Println(bucket.GetObjectTagging(objectName))
    }
    
  • Add object tags during a multipart upload

    The following code provides an example of how to add object tags when you perform a multipart upload of a local file.

    package main
    
    import (
    	"fmt"
    	"os"
    
    	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
    	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    	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 the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual Endpoint.
    	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
    	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. For example, examplebucket.
    	bucketName := "examplebucket"
    	// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
    	objectName := "exampledir/exampleobject.txt"
    	// Specify the full path of the local file. For example, D:\\localpath\\examplefile.txt.
    	// If you specify only the file name, such as examplefile.txt, the file is uploaded from the local path of the project that contains the sample program.
    	fileName := "D:\\localpath\\examplefile.txt"
    
    	// Get the bucket.
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and value for each object tag. For example, set the key to owner and the value to John.
    	tag1 := oss.Tag{
    		Key:   "owner",
    		Value: "John",
    	}
    	tag2 := oss.Tag{
    		Key:   "type",
    		Value: "document",
    	}
    	tagging := oss.Tagging{
    		Tags: []oss.Tag{tag1, tag2},
    	}
    
    	// Split the file into three parts for upload. Determine the number of parts based on the file size.
    	chunks, err := oss.SplitFileByPartNum(fileName, 3)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Open the file.
    	fd, err := os.Open(fileName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	defer fd.Close()
    
    	// Initialize the multipart upload and set the object tags.
    	imur, err := bucket.InitiateMultipartUpload(objectName, oss.SetTagging(tagging))
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Upload parts.
    	var parts []oss.UploadPart
    	for _, chunk := range chunks {
    		fd.Seek(chunk.Offset, os.SEEK_SET)
    		part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number)
    		if err != nil {
    			fmt.Println("Error:", err)
    			os.Exit(-1)
    		}
    		parts = append(parts, part)
    	}
    	_, err = bucket.CompleteMultipartUpload(imur, parts)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	fmt.Println(bucket.GetObjectTagging(objectName))
    }
    
  • Add object tags during an append upload

    The following code provides an example of how to add object tags during an append upload.

    package main
    
    import (
    	"fmt"
    	"os"
    	"strings"
    
    	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
    	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    	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 the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual Endpoint.
    	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
    	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. For example, examplebucket.
    	bucketName := "examplebucket"
    	// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
    	objectName := "exampledir/exampleobject.txt"
    
    	// Get the bucket.
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and value for each object tag. For example, set the key to owner and the value to John.
    	tag1 := oss.Tag{
    		Key:   "owner",
    		Value: "John",
    	}
    	tag2 := oss.Tag{
    		Key:   "type",
    		Value: "document",
    	}
    	tagging := oss.Tagging{
    		Tags: []oss.Tag{tag1, tag2},
    	}
    
    	var nextPos int64
    	// Append data to an appendable object for the first time. The tags that you set in the first AppendObject call take effect. Tags that you set in subsequent AppendObject calls do not take effect.
    	nextPos, err = bucket.AppendObject(objectName, strings.NewReader("Hello OSS A \n"), nextPos, oss.SetTagging(tagging))
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	// Append data for the second time.
    	nextPos, err = bucket.AppendObject(objectName, strings.NewReader("Hello OSS B \n"), nextPos)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	fmt.Println(bucket.GetObjectTagging(objectName))
    }
    
  • Add object tags during a resumable upload

    The following code provides an example of how to add object tags when you perform a resumable upload of a local file.

    package main
    
    import (
    	"fmt"
    	"os"
    
    	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
    	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    	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 the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual Endpoint.
    	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
    	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. For example, examplebucket.
    	bucketName := "examplebucket"
    	// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
    	objectName := "exampledir/exampleobject.txt"
    	// Specify the full path of the local file. For example, D:\\localpath\\examplefile.txt.
    	// If you specify only the file name, such as examplefile.txt, the file is uploaded from the local path of the project that contains the sample program.
    	fileName := "D:\\localpath\\examplefile.txt"
    
    	// Get the bucket.
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and value for each object tag. For example, set the key to owner and the value to John.
    	tag1 := oss.Tag{
    		Key:   "owner",
    		Value: "John",
    	}
    	tag2 := oss.Tag{
    		Key:   "type",
    		Value: "document",
    	}
    	tagging := oss.Tagging{
    		Tags: []oss.Tag{tag1, tag2},
    	}
    
    	// Split the file into multiple parts. Set the size of each part to 100 KB. Use three coroutines to concurrently upload the parts and set object tags.
    	err = bucket.UploadFile(objectName, fileName, 100*1024, oss.Routines(3), oss.SetTagging(tagging))
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	fmt.Println(bucket.GetObjectTagging(objectName))
    }
    

Add or change object tags for an uploaded object

If you did not add object tags when you uploaded an object, or if the existing tags do not meet your requirements, you can add or change the object tags after the upload.

The following code provides an example of how to add or change object tags for an uploaded object.

package main

import (
	"fmt"
	"os"

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

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	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 the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual Endpoint.
	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
	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. For example, examplebucket.
	bucketName := "examplebucket"
	// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
	objectName := "exampledir/exampleobject.txt"

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

	// Specify the key and value for each object tag. For example, set the key to owner and the value to John.
	tag1 := oss.Tag{
		Key:   "owner",
		Value: "John",
	}
	tag2 := oss.Tag{
		Key:   "type",
		Value: "document",
	}
	tagging := oss.Tagging{
		Tags: []oss.Tag{tag1, tag2},
	}
	// Set the object tags.
	err = bucket.PutObjectTagging(objectName, tagging)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println(bucket.GetObjectTagging(objectName))
}

Add or change object tags for a specific version of an object

In a versioning-enabled bucket, you can add or change object tags for a specific version of an object by specifying its version ID.

The following code provides an example of how to add or change object tags for a specific version of an object.

Note

For more information about how to obtain a version ID, see List objects (Go SDK V1).

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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	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 the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual Endpoint.
	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
	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. For example, examplebucket.
	bucketName := "examplebucket"
	// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
	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)
	}
	// Specify the key and value for each object tag. For example, set the key to owner and the value to John.
	tag1 := oss.Tag{
		Key:   "owner",
		Value: "John",
	}

	tag2 := oss.Tag{
		Key:   "type",
		Value: "document",
	}

	tagging := oss.Tagging{
		Tags: []oss.Tag{tag1, tag2},
	}

	// Set tags for the specified version of the object.
	err = bucket.PutObjectTagging(objectName, tagging, oss.VersionId(versionId))

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

Set object tags when you copy an object

You can use the one of following methods to configure object tagging when you copy an object:

  • Copy: The tag of the source object is copied to the destination object.

  • Replace: The destination object has the tag specified in the request instead of the tag of the source object.

The following examples describe how to add tags to an object smaller than 1 GB in simple copy mode and larger than 1 GB in multipart copy mode:

  • Set object tags during a simple copy

    The following code provides an example of how to set object tags when you perform a simple copy of an object that is smaller than 1 GB.

    package main
    
    import (
    	"fmt"
    	"os"
    
    	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
    	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    	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 the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual Endpoint.
    	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
    	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. For example, examplebucket.
    	bucketName := "examplebucket"
    	// Specify the full path of the source object. The full path cannot contain the bucket name. For example, srcexampledir/exampleobject.txt.
    	srcObjectName := "srcexampledir/exampleobject.txt"
    	// Specify the full path of the destination object. The full path cannot contain the bucket name. For example, destexampledir1/exampleobject.txt.
    	destObjectName1 := "destexampledir1/exampleobject.txt"
    	// Specify the full path of the destination object. The full path cannot contain the bucket name. For example, destexampledir2/exampleobject.txt.
    	destObjectName2 := "destexampledir2/exampleobject.txt"
    
    	// Get the bucket.
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and value for each object tag. For example, set the key to owner and the value to John.
    	tag1 := oss.Tag{
    		Key:   "owner",
    		Value: "John",
    	}
    	tag2 := oss.Tag{
    		Key:   "type",
    		Value: "document",
    	}
    	tagging := oss.Tagging{
    		Tags: []oss.Tag{tag1, tag2},
    	}
    
    	// When you copy an object, if you set only the tagging parameter, the object tags do not take effect.
    	_, err = bucket.CopyObject(srcObjectName, destObjectName1, oss.SetTagging(tagging))
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// When you copy an object, you must set both the TaggingReplace and tagging parameters for the object tags to take effect.
    	_, err = bucket.CopyObject(srcObjectName, destObjectName2, oss.SetTagging(tagging), oss.TaggingDirective(oss.TaggingReplace))
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	fmt.Println(bucket.GetObjectTagging(srcObjectName))
    	fmt.Println(bucket.GetObjectTagging(destObjectName1))
    	fmt.Println(bucket.GetObjectTagging(destObjectName2))
    }
    
  • Set object tags during a multipart copy

    The following code provides an example of how to set object tags when you perform a multipart copy of an object that is larger than 1 GB.

    package main
    
    import (
    	"fmt"
    	"os"
    	"strings"
    
    	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
    	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    	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 the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual Endpoint.
    	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
    	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. For example, examplebucket.
    	bucketName := "examplebucket"
    	// Specify the full path of the source object. The full path cannot contain the bucket name. For example, srcexampledir/exampleobject.txt.
    	srcObjectName := "srcexampledir/exampleobject.txt"
    	// Specify the full path of the destination object. The full path cannot contain the bucket name. For example, destexampledir/exampleobject.txt.
    	destObjectName := "destexampledir/exampleobject.txt"
    
    	// Get the bucket.
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and value for each object tag. For example, set the key to owner and the value to John.
    	tag1 := oss.Tag{
    		Key:   "owner",
    		Value: "John",
    	}
    	tag2 := oss.Tag{
    		Key:   "type",
    		Value: "document",
    	}
    	tagging := oss.Tagging{
    		Tags: []oss.Tag{tag1, tag2},
    	}
    
    	// Upload an object for the multipart copy.
    	content := "this your object value"
    	err = bucket.PutObject(srcObjectName, strings.NewReader(content))
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Initialize the multipart upload and set the object tags.
    	imur, err := bucket.InitiateMultipartUpload(destObjectName, oss.SetTagging(tagging))
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	// Split the object into one part for upload.
    	part, err := bucket.UploadPartCopy(imur, bucketName, srcObjectName, 0, int64(len(content)), 1)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	parts := []oss.UploadPart{part}
    	_, err = bucket.CompleteMultipartUpload(imur, parts)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	fmt.Println(bucket.GetObjectTagging(destObjectName))
    }
    

Set tags for a symbolic link

The following code provides an example of how to set tags for a symbolic link.

package main

import (
	"fmt"
	"os"
	"strings"

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

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	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 the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual Endpoint.
	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
	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. For example, examplebucket.
	bucketName := "examplebucket"
	// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
	objectName := "exampledir/exampleobject.txt"
	// Specify the full path of the symbolic link. For example, shortcut/myobject.txt.
	symlinkName := "shortcut/myobject.txt"

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

	// Specify the key and value for each object tag. For example, set the key to owner and the value to John.
	tag1 := oss.Tag{
		Key:   "owner",
		Value: "John",
	}
	tag2 := oss.Tag{
		Key:   "type",
		Value: "document",
	}
	tagging := oss.Tagging{
		Tags: []oss.Tag{tag1, tag2},
	}

	err = bucket.PutObject(objectName, strings.NewReader("Hello OSS"))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	err = bucket.PutSymlink(objectName, symlinkName, oss.SetTagging(tagging))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println(bucket.GetObjectTagging(objectName))
}