すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:オブジェクトのタグ付けの設定

最終更新日:Nov 08, 2024

Object Storage Service (OSS) では、オブジェクトタグを設定してオブジェクトを分類できます。 ライフサイクルルールを設定し、タグに基づいてオブジェクトへのアクセスを制御できます。

使用上の注意

  • このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。

  • このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。

  • このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。

  • オブジェクトにタグを追加するには、oss:PutObjectTagging権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。

オブジェクトをアップロードするときにタグを追加する

  • 単純アップロードを使用してオブジェクトをアップロードするときにタグを追加する

    次のコードでは、単純アップロードを使用してオブジェクトをアップロードするときに、オブジェクトにタグを追加する方法の例を示します。

    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 the 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. 
    	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    	clientOptions = append(clientOptions, oss.Region("yourRegion"))
    	// Specify the version of the signature algorithm.
    	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 name of the bucket. Example: examplebucket. 
    	bucketName := "examplebucket"
    	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
    	objectName := "exampledir/exampleobject.txt"
    
    	// Obtain the bucket information. 
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and the value of the 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},
    	}
    
    	// Add tags to the object. 
    	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))
    }
    
  • マルチパートアップロードを使用してオブジェクトをアップロードするときにオブジェクトにタグを追加する

    次のコードは、マルチパートアップロードを使用してオブジェクトをアップロードするときに、オブジェクトにタグを追加する方法の例を示しています。

    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 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. 
    	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    	clientOptions = append(clientOptions, oss.Region("yourRegion"))
    	// Specify the version of the signature algorithm.
    	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 name of the bucket. Example: examplebucket. 
    	bucketName := "examplebucket"
    	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
    	objectName := "exampledir/exampleobject.txt"
    	// Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt. 
    	// If you specify only the name of the local file such as examplefile.txt without specifying the local path, the local file is uploaded from the path of the project to which the sample program belongs. 
    	fileName := "D:\\localpath\\examplefile.txt"
    
    	// Obtain the bucket information. 
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and the value of the 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},
    	}
    
    	// You can split an object into multiple parts for upload based on the object size. In this example, the object is split into three parts. 
    	chunks, err := oss.SplitFileByPartNum(fileName, 3)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Open the object. 
    	fd, err := os.Open(fileName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    	defer fd.Close()
    
    	// Initialize the uploaded object and configure object tagging. 
    	imur, err := bucket.InitiateMultipartUpload(objectName, oss.SetTagging(tagging))
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Start the multipart upload task. 
    	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))
    }
  • 追加アップロードを使用してオブジェクトをアップロードするときにタグを追加する

    次のコードは、追加アップロードを使用してオブジェクトをアップロードするときに、オブジェクトにタグを追加する方法の例を示しています。

    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 the 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. 
    	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    	clientOptions = append(clientOptions, oss.Region("yourRegion"))
    	// Specify the version of the signature algorithm.
    	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 name of the bucket. Example: examplebucket. 
    	bucketName := "examplebucket"
    	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
    	objectName := "exampledir/exampleobject.txt"
    
    	// Obtain the bucket information. 
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and the value of the 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
    	// Upload the object by using append upload for the first time. When you call the AppendObject operation to add tags to an object, the tags can be added to the object only when the object is uploaded for the first time. 
    	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)
    	}
    	// Upload the object by using append upload 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))
    }
  • 再開可能アップロードを使用してオブジェクトをアップロードするときにオブジェクトにタグを追加する

    次のコードは、再開可能アップロードを使用してオブジェクトをアップロードするときに、オブジェクトにタグを追加する方法の例を示しています。

    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 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. 
    	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    	clientOptions = append(clientOptions, oss.Region("yourRegion"))
    	// Specify the version of the signature algorithm.
    	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 name of the bucket. Example: examplebucket. 
    	bucketName := "examplebucket"
    	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
    	objectName := "exampledir/exampleobject.txt"
    	// Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt. 
    	// If you specify only the name of the local file such as examplefile.txt without specifying the local path, the local file is uploaded from the path of the project to which the sample program belongs. 
    	fileName := "D:\\localpath\\examplefile.txt"
    
    	// Obtain the bucket information. 
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the key and the value of the 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 object into multiple parts, each of which is 100 KB in size. Then, use 3 threads to concurrently upload the parts, and add tags to the object when you upload the parts. 
    	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))
    }
    

アップロードされたオブジェクトにタグを追加するか、オブジェクトのタグを変更する

既存のオブジェクトにタグがない場合、または追加されたオブジェクトのタグが要件を満たしていない場合は、既存のオブジェクトのタグを追加または変更できます。

次のコードは、アップロードされたオブジェクトにタグを追加する方法、またはオブジェクトのタグを変更する方法の例を示しています。

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 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. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	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 name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
	objectName := "exampledir/exampleobject.txt"

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

	// Specify the key and the value of the 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},
	}
	// Add tags to the object. 
	err = bucket.PutObjectTagging(objectName, tagging)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println(bucket.GetObjectTagging(objectName))
}

指定したオブジェクトバージョンにタグを追加するか、オブジェクトバージョンのタグを変更する

バケットのバージョン管理が有効になっている場合、オブジェクトのバージョンIDを指定することで、バケット内のオブジェクトの指定されたバージョンにタグを追加したり、タグを変更したりできます。

次のコードは、オブジェクトの指定されたバージョンにタグを追加する方法、またはオブジェクトのタグを変更する方法の例を示しています。

説明

オブジェクトのバージョンIDを照会する方法の詳細については、「オブジェクトのリスト」をご参照ください。

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 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. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	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 name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
	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)
	}
	// Specify the key and the value of the 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},
	}

	// Add tags to 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))
}

オブジェクトをコピーするときにオブジェクトにタグを追加する

次のいずれかの方法を使用して、オブジェクトをコピーするときにオブジェクトのタグ付けを設定できます。

  • コピー: ソースオブジェクトのタグがコピー先オブジェクトにコピーされます。

  • 置換: ターゲットオブジェクトには、ソースオブジェクトのタグではなく、リクエストで指定されたタグがあります。

次の例では、シンプルコピーモードで1 GB未満、マルチパートコピーモードで1 GBを超えるオブジェクトにタグを追加する方法について説明します。

  • シンプルコピーモードでオブジェクトをコピーするときにオブジェクトにタグを追加する

    次のコードでは、オブジェクトをシンプルコピーモードでコピーするときに、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 the 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. 
    	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    	clientOptions = append(clientOptions, oss.Region("yourRegion"))
    	// Specify the version of the signature algorithm.
    	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 name of the bucket. Example: examplebucket. 
        bucketName := "examplebucket"
        // Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcexampledir/exampleobject.txt. 
        srcObjectName := "srcexampledir/exampleobject.txt"
        // Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destexampledir1/exampleobject.txt. 
        destObjectName1 := "destexampledir1/exampleobject.txt"
        // Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destexampledir2/exampleobject.txt. 
        destObjectName2 := "destexampledir2/exampleobject.txt"
    
        // Obtain the bucket information. 
        bucket, err := client.Bucket(bucketName)
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    
        // Specify the key and the value of the 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},
        }
    
        // If you configure only the tagging parameter, tags cannot be added to the destination object. 
        _, err = bucket.CopyObject(srcObjectName, destObjectName1, oss.SetTagging(tagging))
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    
        // Tags can be added to the destination object only when you configure both the TaggingReplace and tagging parameters. 
        _, 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))
    }
  • マルチパートコピーモードでオブジェクトをコピーするときにオブジェクトにタグを追加する

    次のコードでは、マルチパートコピーモードでオブジェクトをコピーするときに、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 the 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. 
    	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    	clientOptions = append(clientOptions, oss.Region("yourRegion"))
    	// Specify the version of the signature algorithm.
    	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 name of the bucket. Example: examplebucket. 
        bucketName := "examplebucket"
        // Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcexampledir/exampleobject.txt. 
        srcObjectName := "srcexampledir/exampleobject.txt"
        // Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destexampledir/exampleobject.txt. 
        destObjectName := "destexampledir/exampleobject.txt"
    
        // Obtain the bucket information. 
        bucket, err := client.Bucket(bucketName)
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    
        // Specify the key and the value of the 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 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 uploaded object and configure object tagging. 
        imur, err := bucket.InitiateMultipartUpload(destObjectName, oss.SetTagging(tagging))
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
        // Use the UploadPartCopy method to upload the object as one part. 
        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))
    }

シンボリックリンクにタグを追加する

次のコードは、シンボリックリンクにタグを追加する方法の例を示しています。

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 the 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. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	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 name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
	objectName := "exampledir/exampleobject.txt"
	// Specify the full path of the symbolic link. Example: shortcut/myobject.txt. 
	symlinkName := "shortcut/myobject.txt"

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

	// Specify the key and the value of the 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))
}