Restore a file (Go SDK V1)
In a versioning-enabled bucket, different versions of an object can have different storage classes. The RestoreObject operation restores the current version of an object by default. You can also specify a version ID to restore a specific version of the object.
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 restore an object, you must have the
oss:RestoreObjectpermission. For more information, see Grant a custom policy.
Example code
The following code shows how to restore a file:
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 more information about the API operation used to restore a file, see RestoreObject.