Restore a file (Go SDK V1)
Use the RestoreObject operation to restore an Archive object so it becomes accessible for download. In a versioning-enabled bucket, different versions of an object can have different storage classes. By default, RestoreObject targets the current version of an object. Pass a version ID to restore a specific version instead.
How it works
Restoring an Archive object requires three steps:
Call
bucket.GetObjectDetailedMetato read theX-Oss-Storage-Classheader and confirm the object is in the Archive storage class.Call
bucket.RestoreObjectto submit the restore request.Poll
bucket.GetObjectDetailedMetauntilX-Oss-Restoreno longer returnsongoing-request="true", which indicates the restore is complete.
| API | Operation | Description |
|---|---|---|
bucket.RestoreObject | Restore object | Restores an Archive object so it can be downloaded |
bucket.GetObjectDetailedMeta | Get object metadata | Reads X-Oss-Storage-Class and X-Oss-Restore headers to check storage class and restore status |
Prerequisites
Before you begin, ensure that you have:
Configured access credentials as environment variables
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET. For configuration steps, see Configure access credentials.The
oss:RestoreObjectpermission on the target object. For permission setup, see Attach a custom policy to a RAM user.
Usage notes
The examples use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For a full list of endpoints, see Regions and endpoints.
The OSSClient instance in these examples is created with an OSS endpoint. To create one using a custom domain name or Security Token Service (STS), see Configure OSSClient instances.
Example code
The following example restores an Archive object by version ID:
package main
import (
"log"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
var clientOptions []oss.ClientOption
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
endpoint := "http://oss-cn-hangzhou.aliyuncs.com"
// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions = append(clientOptions, oss.SetCredentialsProvider(&provider))
clientOptions = append(clientOptions, oss.Region("cn-hangzhou"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New(endpoint, "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the bucket name.
bucketName := "examplebucket"
// Specify the full path of the Archive object. Do not include the bucket name. Example: exampledir/exampleobject.txt.
objectName := "exampledir/exampleobject.txt"
// Specify the version ID of the object.
versionId := "yourObjectVersionId"
// Create a bucket instance.
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
}
// Check whether the object is of the Archive storage class.
meta, err := bucket.GetObjectDetailedMeta(objectName, oss.VersionId(versionId))
if err != nil {
log.Fatalf("Failed to get object detailed meta: %v", err)
}
if meta.Get("X-Oss-Storage-Class") == string(oss.StorageArchive) {
log.Println("Object is an archive type, initiating restore process...")
err = bucket.RestoreObject(objectName, oss.VersionId(versionId))
if err != nil {
log.Fatalf("Failed to restore object: %v", err)
}
// Wait for the restore operation to complete.
for {
meta, err = bucket.GetObjectDetailedMeta(objectName, oss.VersionId(versionId))
if err != nil {
log.Fatalf("Failed to get object detailed meta during restore: %v", err)
}
restoreStatus := meta.Get("X-Oss-Restore")
log.Printf("x-oss-restore: %s", restoreStatus)
if restoreStatus != "ongoing-request=\"true\"" {
break
}
log.Println("Restore in progress, waiting...")
time.Sleep(1000 * time.Second)
}
log.Println("Object restored successfully.")
} else {
log.Println("Object is not an archive type, no need to restore.")
}
}References
For the full API specification, see RestoreObject.