All Products
Search
Document Center

Object Storage Service:Convert the storage class of an object (Go SDK V2)

Last Updated:Aug 02, 2025

Object Storage Service (OSS) provides multiple storage classes, such as Standard, Infrequent Access, Archive, Cold Archive, and Deep Cold Archive, to meet various storage requirements for data ranging from hot to cold data. In OSS, the content of an object is immutable after it is created. This means that to change the storage class of an object, you cannot directly modify the original object but must create a new one. This topic describes how to use the Copier.Copy method or the Client.CopyObject method in Go SDK V2 to convert the storage class of an object by copying the source object.

Usage notes

  • The sample code in this topic uses the cn-hangzhou region ID for the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you access resources in the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.

  • In this topic, access credentials are retrieved from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • To convert the storage class of an object, you must have the oss:GetObject, oss:PutObject, and oss:RestoreObject permissions. For more information, see Grant a custom policy to a RAM user.

Sample code

(Recommended) Use the Copier to convert the storage class

We recommend that you use the Copier.Copy method to convert the storage class of an object. This method integrates simple copy and multipart copy operations, and automatically selects the appropriate operation based on the parameters in the request.

The following code provides an example of how to use the Copier.Copy method to convert the storage class of an object from Standard to Archive.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region         string // The region.
	srcBucketName  string // The name of the source bucket.
	srcObjectName  string // The name of the source object.
	destBucketName string // The name of the destination bucket.
	destObjectName string // The name of the destination object.
)

// The init function is used to initialize command-line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&srcBucketName, "src-bucket", "", "The name of the source bucket.")
	flag.StringVar(&srcObjectName, "src-object", "", "The name of the source object.")
	flag.StringVar(&destBucketName, "dest-bucket", "", "The name of the destination bucket.")
	flag.StringVar(&destObjectName, "dest-object", "", "The name of the destination object.")
}

func main() {
	// Parse command-line parameters.
	flag.Parse()

	// Check whether the name of the source bucket is empty.
	if len(srcBucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// If the destination bucket name is not specified, the source bucket name is used.
	if len(destBucketName) == 0 {
		destBucketName = srcBucketName
	}

	// Check whether the name of the source object is empty.
	if len(srcObjectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, src object name required")
	}

	// Check whether the name of the destination object is empty.
	if len(destObjectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, destination object name required")
	}

	// Configure the OSS client.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a copier.
	c := client.NewCopier()

	// Build a request to copy the object.
	request := &oss.CopyObjectRequest{
		Bucket:       oss.Ptr(destBucketName), // The name of the destination bucket.
		Key:          oss.Ptr(destObjectName), // The name of the destination object.
		SourceKey:    oss.Ptr(srcObjectName),  // The name of the source object.
		SourceBucket: oss.Ptr(srcBucketName),  // The name of the source bucket.
		StorageClass: oss.StorageClassArchive, // Specify the storage class as Archive.
	}

	// Copy the object.
	result, err := c.Copy(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to copy object %v", err) // If the copy fails, record the error and exit.
	}

	// Print the copy result.
	log.Printf("copy object result:%#v\n", result)
}

Convert the storage class using the CopyObject method

You can use the CopyObject method to convert the storage class of an object from Standard to Archive.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region         string // The region.
	bucketName     string // The name of the source bucket.
	objectName     string // The name of the source object.
	destBucketName string // The name of the destination bucket.
	destObjectName string // The name of the destination object.
)

// The init function is used to initialize command-line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the source bucket.")
	flag.StringVar(&objectName, "src-object", "", "The name of the source object.")
	flag.StringVar(&destBucketName, "dest-bucket", "", "The name of the destination bucket.")
	flag.StringVar(&destObjectName, "dest-object", "", "The name of the destination object.")
}

func main() {
	// Parse command-line parameters.
	flag.Parse()

	// Check whether the name of the source bucket is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, source bucket name required")
	}

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// If the destination bucket name is not specified, the source bucket name is used.
	if len(destBucketName) == 0 {
		destBucketName = bucketName
	}

	// Check whether the name of the source object is empty.
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, source object name required")
	}

	// Check whether the name of the destination object is empty.
	if len(destObjectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, destination object name required")
	}

	// Load the default configurations and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to copy the object.
	copyRequest := &oss.CopyObjectRequest{
		Bucket:       oss.Ptr(destBucketName), // The name of the destination bucket.
		Key:          oss.Ptr(destObjectName), // The name of the destination object.
		SourceKey:    oss.Ptr(objectName),     // The name of the source object.
		SourceBucket: oss.Ptr(bucketName),     // The name of the source bucket.
		StorageClass: oss.StorageClassArchive, // Convert the storage class to Archive.
	}

	// Copy the object and process the result.
	copyResult, err := client.CopyObject(context.TODO(), copyRequest)
	if err != nil {
		log.Fatalf("failed to copy object: %v", err)
	}
	log.Printf("copy object result: %#v\n", copyResult)
}

References

  • For more information about the API operation to convert the storage class of an object using the Copier, see Copier.Copy.

  • For more information about the API operation to convert the storage class of an object using the simple copy method, see CopyObject.