Object Storage Service (OSS) provides the following storage classes to cover various data storage scenarios from hot data to cold data: Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive. This topic describes how to convert the storage class of an object.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 Initialization.
To convert the storage class of an object, you must have the
oss:GetObject
,oss:PutObject
, andoss:RestoreObject
permissions. For more information, see Attach a custom policy to a RAM user.
Sample code
The following section provides examples on how to convert the storage class of an object.
The following sample code provides an example on how to convert the storage class of an object from Standard or IA to Archive:
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 sample 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. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucketName := "yourBucketName" // Specify the full path of the object. Do not include the bucket name in the full path. objectName := "yourObjectName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Convert the storage class of the object. In this example, the storage class is set to Archive. _, err = bucket.CopyObject(objectName, objectName, oss.ObjectStorageClass(oss.StorageArchive)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } }
The following sample code provides an example on how to convert the storage class of an object from Archive to Standard:
package main import ( "fmt" "os" "time" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { /// Obtain access credentials from environment variables. Before you run the sample 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. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucketName := "yourBucketName" // Specify the full path of the object. Do not include the bucket name in the full path. objectName := "yourObjectName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Check whether the object whose storage class you want to convert is an Archive object. If the object is an Archive object, you must restore the object before you can convert its storage class. meta, err := bucket.GetObjectDetailedMeta(objectName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } fmt.Println("X-Oss-Storage-Class : ", meta.Get(oss.HTTPHeaderOssStorageClass)) if meta.Get(oss.HTTPHeaderOssStorageClass) == string(oss.StorageArchive) { // Restore the object. err = bucket.RestoreObject(objectName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Wait for about one minute until the object is restored. meta, err = bucket.GetObjectDetailedMeta(objectName) for meta.Get("X-Oss-Restore") == "ongoing-request=\"true\"" { fmt.Println("X-Oss-Restore:" + meta.Get("X-Oss-Restore")) time.Sleep(1 * time.Second) meta, err = bucket.GetObjectDetailedMeta(objectName) } } // Convert the storage class of the restored object to Standard. _, err = bucket.CopyObject(objectName, objectName, oss.ObjectStorageClass(oss.StorageStandard)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } }
References
For more information about the API operation that you can call to convert the storage class of an object, see CopyObject.