All Products
Search
Document Center

Object Storage Service:Restore objects by using OSS SDK for Go

Last Updated:Jan 08, 2024

In a versioned bucket, the storage classes of different versions of an object can be different. By default, when you perform the RestoreObject operation on 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.

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 restore an object, you must have the oss:RestoreObject permission. For more information, see Attach a custom policy to a RAM user.

Examples

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

package main

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

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

func main() {
  // Obtain access credentials from the 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 {
    HandleError(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. 
  client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
  if err != nil {
    HandleError(err)
  }

  // Specify the name of the bucket. 
  bucketName := "examplebucket"
  // Specify the full path of an Archive object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
  objectName := "exampledir/exampleobject.txt"
  // Specify the version ID of the object. 
  versionId := "yourObjectVersionId"

  // Create a bucket. 
  bucket, err := client.Bucket(bucketName)
  if err != nil {
    HandleError(err)
  }

  // Check whether the storage class of the object is Archive. 
  meta, err := bucket.GetObjectDetailedMeta(objectName, oss.VersionId(versionId))
  if err != nil {
    HandleError(err)
  }
  if meta.Get("X-Oss-Storage-Class") == string(oss.StorageArchive) {
    err = bucket.RestoreObject(objectName, oss.VersionId(versionId))
    if err != nil {
      HandleError(err)
    }
    // Wait until the object is restored. 
    meta, err = bucket.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 = bucket.GetObjectDetailedMeta(objectName, oss.VersionId(versionId))
    }
  }
}

References

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