In a versioned bucket, the storage classes of different versions of an object can be different. By default, when you call the RestoreObject operation to restore an object, the current version of the object is restored. You can specify a version ID in the request to restore the specified version of an object.

Sample code

The following code provides an example on how to restore an object:

package main

import (
  "fmt"
  "os"
  "time"
  "strings"
  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
  fmt.Println("Error:", err)
  os.Exit(-1)
}

func main() {
    // 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. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
    HandleError(err)
  }

   // Specify the name of the bucket. Example: examplebucket. 
   bucketName := "examplebucket"
   // Specify the full path of the object. The full path of the object cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
   objectName := "exampledir/exampleobject.txt"
   // Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt. 
   localFilename := "D:\\localpath\\examplefile.txt"

  // Create a bucket of the Archive storage class. 
  err = client.CreateBucket(bucketName, oss.StorageClass(oss.StorageArchive))
  if err != nil {
    HandleError(err)
  }
  // Enable versioning for the bucket. 
  config := oss.VersioningConfig{Status: "Enabled"}
  err = client.SetBucketVersioning(bucketName, config)
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  archiveBucket, err := client.Bucket(bucketName)
  if err != nil {
    HandleError(err)
  }

  var retHeader http.Header
  // Upload the string. Use oss.GetResponseHeader to obtain the returned header. 
  err = archiveBucket.PutObject("yourObjectName", strings.NewReader("yourObjectValue"), oss.GetResponseHeader(&retHeader))
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Obtain the version ID of the uploaded object. 
  versionId := oss.GetVersionId(retHeader)

  // Check whether the object is of the Archive storage class. 
  meta, err := archiveBucket.GetObjectDetailedMeta(objectName, oss.VersionId(versionId))
  if err != nil {
    HandleError(err)
  }

  if meta.Get("X-Oss-Storage-Class") == string(oss.StorageArchive) {
    err = archiveBucket.RestoreObject(objectName, oss.VersionId(versionId))
    if err != nil {
      HandleError(err)
    }

    // Wait until the Archive object is restored. 
    meta, err = archiveBucket.GetObjectDetailedMeta(objectName, oss.VersionId(versionId))
    for meta.Get("X-Oss-Restore") == "ongoing-request=\"true\"" {
      fmt.Println("x-oss-restore:" + meta.Get("X-Oss-Restore"))
      time.Sleep(1000 * time.Second)
      meta, err = archiveBucket.GetObjectDetailedMeta(objectName, oss.VersionId(versionId))
    }
  }

  // Download the restored Archive object. 
  err = archiveBucket.GetObjectToFile(objectName, localFilename, oss.VersionId(versionId))
  if err != nil {
    HandleError(err)
  }
}

References

For more information about the API operation that you can call to restore an object, see RestoreObject.