Restore objects (Go SDK V2)
Objects in the Archive, Cold Archive, and Deep Cold Archive storage classes must be restored before you can read them. Restoring an object creates a temporary replica, which exists alongside the original archived object. After the restoration period expires, the replica is automatically deleted. This topic explains how to use the Object Storage Service (OSS) Go SDK to restore objects from the Archive, Cold Archive, and Deep Cold Archive storage classes.
Usage notes
You can call the RestoreObject method only for objects in the Archive, Cold Archive, or Deep Cold Archive storage class.
The examples in this topic use the China (Hangzhou) region with the region ID
cn-hangzhou. The examples use a public endpoint by default. If you want to access OSS from another Alibaba Cloud service in the same region, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.The examples in this topic read 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.
Method signature
func (c *Client) RestoreObject(ctx context.Context, request *RestoreObjectRequest, optFns ...func(*Options)) (*RestoreObjectResult, error)Request parameters
Parameter | Type | Description |
ctx | context.Context | The request context, used to set a request's total timeout duration. |
request | *RestoreObjectRequest | The request parameters. For more information, see RestoreObjectRequest. |
optFns | ...func(*Options) | (Optional) The operation-level configuration parameters. For more information, see Options. |
The following table describes the common parameters of RestoreObjectRequest.
Parameter | Type | Description |
bucket | *string | The bucket name. |
key | *string | The object name. |
versionId | *string | The version ID of the object to restore. If you do not specify this parameter, OSS restores the latest version of the object. |
restoreRequest | *RestoreRequest | The restore request parameters. |
The following table describes the parameters of RestoreRequest.
Parameter | Type | Description |
days | int32 | The number of days for which the object remains in the restored state. For more information, see Restore objects. |
jobParameters | *JobParameters | The parameters that specify the restore priority. The Tier field specifies the time required to restore the object. For more information, see Restore objects. |
Response parameters
Parameter | Type | Description |
result | *RestoreObjectResult | The return value of the API. It is valid when |
err | error | An error object that is non-nil if the request fails. |
Examples
Use the following code to restore objects from the Archive, Cold Archive, or Deep Cold Archive 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 for command-line arguments.
var (
region string // The OSS region.
bucketName string // The bucket name.
objectName string // The object name.
)
// init initializes the command-line flags.
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 flags.
flag.Parse()
// Check if the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check if the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check if the object name is empty.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configuration, 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.
RestoreRequest: &oss.RestoreRequest{
Days: 3, // Set the restoration period 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)
}
// Print the result.
log.Printf("restore object result:%#v\n", result)
}
Use cases
References
For the complete sample code for restoring an object, see GitHub.
For more information about the API operation for restoring an object, see RestoreObject.
For more information about how to restore objects, see Restore objects.