Object Storage Service (OSS) provides multiple storage classes, such as Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive. These storage classes meet various storage needs for data, from hot to cold. In OSS, you cannot modify the content of an object after it is created. This means you cannot directly change the storage class of an existing object. Instead, you must create a new object. To change the storage class of an object, use the Bucket.CopyObject method. This method copies the source object to a new object with the desired storage class.
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 convert the storage class of an object, you must have the
oss:GetObject,oss:PutObject, andoss:RestoreObjectpermissions. For more information, see Attach a custom policy to a RAM user.
Sample code
The following code provides examples of how to use the Bucket.CopyObject method to change the storage class of an object:
Change the storage class of an object from Standard or Infrequent Access to Archive:
package main import ( "log" "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 { log.Fatalf("Failed to create credentials provider: %v", err) } // 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, use 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, use 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 { log.Fatalf("Failed to create OSS client: %v", err) } // Set yourBucketName to the name of the bucket. bucketName := "yourBucketName" // Replace with your actual bucket name. // Set yourObjectName to the full path of the object. Do not include the bucket name. objectName := "yourObjectName" // Replace with your actual object path. bucket, err := client.Bucket(bucketName) if err != nil { log.Fatalf("Failed to get bucket: %v", err) } // Change the storage class of the object. For example, change it to Archive. _, err = bucket.CopyObject(objectName, objectName, oss.ObjectStorageClass(oss.StorageArchive)) if err != nil { log.Fatalf("Failed to change storage class of object: %v", err) } log.Println("Storage class changed successfully.") }Change the storage class of an object from Archive to Standard:
package main import ( "log" "time" "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 { log.Fatalf("Failed to create credentials provider: %v", err) } // 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, use 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, use 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 { log.Fatalf("Failed to create OSS client: %v", err) } // Set yourBucketName to the name of the bucket. bucketName := "yourBucketName" // Replace with your actual bucket name. // Set yourObjectName to the full path of the object. Do not include the bucket name. objectName := "yourObjectName" // Replace with your actual object path. bucket, err := client.Bucket(bucketName) if err != nil { log.Fatalf("Failed to get bucket: %v", err) } // Check if the object is an Archive object. If it is, you must restore it before you can change its storage class. meta, err := bucket.GetObjectDetailedMeta(objectName) if err != nil { log.Fatalf("Failed to get object detailed metadata: %v", err) } log.Printf("X-Oss-Storage-Class: %s\n", meta.Get(oss.HTTPHeaderOssStorageClass)) if meta.Get(oss.HTTPHeaderOssStorageClass) == string(oss.StorageArchive) { // Restore the object. err = bucket.RestoreObject(objectName) if err != nil { log.Fatalf("Failed to restore object: %v", err) } // Wait for the object to be restored. This typically takes about 1 minute. meta, err = bucket.GetObjectDetailedMeta(objectName) for meta.Get("X-Oss-Restore") == "ongoing-request=\"true\"" { log.Printf("X-Oss-Restore: %s\n", meta.Get("X-Oss-Restore")) time.Sleep(1 * time.Second) meta, err = bucket.GetObjectDetailedMeta(objectName) if err != nil { log.Fatalf("Failed to get object detailed metadata during restore process: %v", err) } } } // Change the storage class of the restored object. For example, change it to Standard. _, err = bucket.CopyObject(objectName, objectName, oss.ObjectStorageClass(oss.StorageStandard)) if err != nil { log.Fatalf("Failed to change storage class of object: %v", err) } log.Println("Storage class changed successfully.") }
References
For more information about the API operation that is used to change the storage class of an object, see CopyObject.