In a bucket with versioning enabled, different versions of an object can have different storage classes. The RestoreObject API operation restores the current version of an object by default. You can specify a version ID to restore a specific version of the object.
Notes
The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) as an example. By default, the public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about the mappings between OSS regions and endpoints, see Regions and endpoints.The examples in this topic show how to obtain access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To restore an object, you must have the
oss:RestoreObjectpermission. For more information, see Attach a custom policy to a RAM user.
Sample code
You can use the following code to restore Archive, Cold Archive, or Deep Cold Archive objects:
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // The region.
bucketName string // The bucket name.
objectName string // The object name.
)
// The init function is used to initialize command-line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
// Parse command-line parameters.
flag.Parse()
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the object name is empty.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configurations and set the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to restore the object.
request := &oss.RestoreObjectRequest{
Bucket: oss.Ptr(bucketName), // The bucket name.
Key: oss.Ptr(objectName), // The object name.
VersionId: oss.Ptr("yourVersionId"), // Specify the actual version ID.
RestoreRequest: &oss.RestoreRequest{
Days: 3, // Set the number of days for which the restored state is retained to 3.
},
}
// Send the request to restore the object.
result, err := client.RestoreObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to restore object %v", err)
}
// Print the result of restoring the object.
log.Printf("restore object result:%#v\n", result)
}
References
For the complete sample code for restoring objects, see GitHub sample.
For more information about the API operation for restoring objects, see RestoreObject.
For more information about operations related to restoring objects, see Restore objects.