Use the RestoreObject API to make an archived object accessible. By default, the current version is restored. To restore a specific version, set the VersionId parameter in the request.
Notes
The
oss:RestoreObjectpermission is required. For more information, see Grant a custom policy.The sample code uses the region ID
cn-hangzhou(China (Hangzhou)). The public endpoint is used by default. To access the bucket from another Alibaba Cloud service in the same region, switch to the internal endpoint. For more information about OSS regions and endpoints, see Regions and Endpoints.Access credentials are read from environment variables. For configuration instructions, see Configure access credentials (Go SDK V1).
Sample code
The following example restores an Archive, Cold Archive, or Deep Cold Archive object. RestoreObjectRequest accepts an optional VersionId — omit it to restore the current version, or set it to target a specific version in a versioning-enabled bucket. The Days field controls how long the object remains in the restored state before returning to its archived storage class.
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 // Region in which the bucket is located.
bucketName string // Name of the bucket.
objectName string // Name of the object.
)
// Specify the init function 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 name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the name of the object is specified.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSSClient instance.
client := oss.NewClient(cfg)
// Create a request to restore the object.
request := &oss.RestoreObjectRequest{
Bucket: oss.Ptr(bucketName), // Name of the bucket.
Key: oss.Ptr(objectName), // Name of the object.
VersionId: oss.Ptr("yourVersionId"), // Specify the actual version ID.
RestoreRequest: &oss.RestoreRequest{
Days: 3, // Set the duration of the restored state to 3 days.
},
}
// 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)
}
// Display the result.
log.Printf("restore object result:%#v\n", result)
}
References
For the complete sample code, visit the GitHub sample.
For the API reference, see RestoreObject.
For related operations, see Restore objects.