Convert the storage class of an object (OSS SDK for Go 1.0)

更新时间:
复制 MD 格式

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. In OSS, once an object is created, its content cannot be modified. If you want to convert the storage class of an object, you must use the Bucket.CopyObject method to copy the object to create a new object and convert the storage class of the new object.

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.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure a client (Go SDK V1).

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

Examples

You can convert the storage class of an object by using Bucket.CopyObject.

  1. 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 (
    	"log"
    
    	"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 {
    		log.Fatalf("Failed to create credentials provider: %v", err)
    	}
    
    	// 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 your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
    	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    	clientOptions = append(clientOptions, oss.Region("yourRegion"))
    	// Specify 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)
    	}
    
    	// Specify the name of the bucket. 
    	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
    	// Specify the full path of the object. Do not include the bucket name in the full path. 
    	objectName := "yourObjectName" // Replace yourObjectName with the actual object path.
    
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		log.Fatalf("Failed to get bucket: %v", err)
    	}
    
    	// Convert the storage class of the object 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.")
    }
    
  2. The following sample code provides an example on how to convert 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 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 {
    		log.Fatalf("Failed to create credentials provider: %v", err)
    	}
    
    	// 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 your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
    	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    	clientOptions = append(clientOptions, oss.Region("yourRegion"))
    	// Specify 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)
    	}
    
    	// Specify the name of the bucket. 
    	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
    	// Specify the full path of the object. Do not include the bucket name in the full path. 
    	objectName := "yourObjectName" // Replace yourObjectName with the actual object path.
    
    	bucket, err := client.Bucket(bucketName)
    	if err != nil {
    		log.Fatalf("Failed to get bucket: %v", err)
    	}
    
    	// 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 {
    		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 about one minute until the object is restored. 
    		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)
    			}
    		}
    	}
    
    	// Convert the storage class of the restored object 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 you can call to convert the storage class of an object, see CopyObject.