All Products
Search
Document Center

Object Storage Service:Convert the storage class of an object using OSS SDK for Go 2.0

Last Updated:Mar 20, 2026

OSS supports five storage classes — Standard, Infrequent Access, Archive, Cold Archive, and Deep Cold Archive — to match different data temperature requirements. Because object content is immutable after creation, changing the storage class requires copying the object with the target class specified. This topic shows how to use OSS Go SDK V2 to change an object's storage class.

Prerequisites

Before you begin, ensure that you have:

Usage notes

The sample code uses the cn-hangzhou region ID for the China (Hangzhou) region. A public endpoint is used by default. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For supported regions and endpoints, see OSS regions and endpoints.

Convert the storage class

OSS Go SDK V2 provides two methods for storage class conversion. Both work by copying the source object to a destination with the target storage class set.

MethodDescription
Copier.Copy (recommended)Automatically selects simple copy or multipart copy based on the parameters in the request. Use this for most cases.
Client.CopyObjectCopies the object using the CopyObject API. Use this when you need direct control over the copy operation.

Use Copier.Copy (recommended)

Copier.Copy integrates simple copy and multipart copy, and automatically selects the appropriate operation based on the parameters in the request. The following example converts an object's storage class 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"
)

var (
	region         string
	srcBucketName  string
	srcObjectName  string
	destBucketName string
	destObjectName string
)

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() {
	flag.Parse()

	if len(srcBucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Optional: if no destination bucket is specified, copy within the same bucket.
	if len(destBucketName) == 0 {
		destBucketName = srcBucketName
	}

	if len(srcObjectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, src object name required")
	}

	if len(destObjectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, destination object name required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	// Create a Copier. It selects simple copy or multipart copy automatically.
	c := client.NewCopier()

	// You can't change an object's storage class directly. The only way is to
	// copy the object with the target storage class specified.
	request := &oss.CopyObjectRequest{
		Bucket:       oss.Ptr(destBucketName),
		Key:          oss.Ptr(destObjectName),
		SourceKey:    oss.Ptr(srcObjectName),
		SourceBucket: oss.Ptr(srcBucketName),
		StorageClass: oss.StorageClassArchive, // Set the target storage class.
	}

	result, err := c.Copy(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to copy object %v", err)
	}

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

Use CopyObject

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"
)

var (
	region         string
	bucketName     string
	objectName     string
	destBucketName string
	destObjectName string
)

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() {
	flag.Parse()

	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, source bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Optional: if no destination bucket is specified, copy within the same bucket.
	if len(destBucketName) == 0 {
		destBucketName = bucketName
	}

	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, source object name required")
	}

	if len(destObjectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, destination object name required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	// You can't change an object's storage class directly. The only way is to
	// copy the object with the target storage class specified.
	copyRequest := &oss.CopyObjectRequest{
		Bucket:       oss.Ptr(destBucketName),
		Key:          oss.Ptr(destObjectName),
		SourceKey:    oss.Ptr(objectName),
		SourceBucket: oss.Ptr(bucketName),
		StorageClass: oss.StorageClassArchive, // Set the target storage class.
	}

	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