All Products
Search
Document Center

Object Storage Service:Restore an object

Last Updated:Sep 13, 2023

You must restore an Archive object or a Cold Archive object before you can read the object in Object Storage Service (OSS). This topic describes how to restore an Archive object or a Cold Archive object.

Usage notes

  • The RestoreObject operation can be called only on Archive and Cold Archive objects.

  • 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 Common examples of RAM policies.

Restore an Archive object

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

package main

import (
    "fmt"
    "os"
    "strings"
    "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 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 {
        HandleError(err)
    }

    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Specify the full path of the object. Do not include the bucket name in the full path. 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 the bucket. 
    err = client.CreateBucket(bucketName, oss.StorageClass(oss.StorageArchive))
    if err != nil {
        HandleError(err)
    }
    
    archiveBucket, err := client.Bucket(bucketName)
    if err != nil {
        HandleError(err)
    }

    // Upload the local file. 
    var val = "More than just cloud."
    err = archiveBucket.PutObject(objectName, strings.NewReader(val))
    if err != nil {
        HandleError(err)
    }

    // Query the storage class of the object. 
    meta, err := archiveBucket.GetObjectDetailedMeta(objectName)
    if err != nil {
        HandleError(err)
    }
    fmt.Println("X-Oss-Storage-Class : ", meta.Get("X-Oss-Storage-Class"))

    // Restore the Archive object. 
    err = archiveBucket.RestoreObject(objectName)
    if err != nil {
        HandleError(err)
    }

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

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

    // Perform the restoration operation on the object again. The duration for which the object remains in the restored state is extended by 24 hours. 
    err = archiveBucket.RestoreObject(objectName)
    if err != nil {
        HandleError(err)
    }

    fmt.Println("ArchiveSample completed")
}

Restore a Cold Archive object

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

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. Example: examplebucket. 
    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    var restoreConfig oss.RestoreConfiguration

    // Specify that the object is restored within 1 hour. 
    // restoreConfig.Tier = string(oss.RestoreExpedited)
    // Specify that the object is restored within 2 to 5 hours. 
    // restoreConfig.Tier = string(oss.RestoreStandard)
    // Specify that the object is restored within 5 to 12 hours. 
    // restoreConfig.Tier = string(oss.RestoreBulk)

    // Specify the duration for which the object remains in the restored state. Unit: days. Valid values: 1 to 365. 
    restoreConfig.Days = 1
    // Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
    err = bucket.RestoreObjectDetail("exampledir/exampleobject.txt", restoreConfig)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
}

References

For more information about the API operation that you can call to restore Archive and Cold Archive objects, see RestoreObject.