You must restore Archive, Cold Archive, and Deep Cold Archive objects before you can read them. When an object is restored, a temporary replica is created. This replica exists alongside the original object and is automatically deleted after its restoration period expires. This topic describes how to use the OSS Go SDK to restore Archive, Cold Archive, and Deep Cold Archive objects.
Notes
Only Archive, Cold Archive, and Deep Cold Archive objects support the RestoreObject method.
The sample code in this topic uses the China (Hangzhou) region ID
cn-hangzhouas 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 regions and endpoints that OSS supports, see OSS regions and endpoints.This topic provides an example of how to 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 Grant custom access policies to RAM users.
Method definition
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, which can be used to set the total timeout for the request. |
request | *RestoreObjectRequest | The request parameters for the operation. 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 number of the object to restore. If you do not set this parameter, the latest version of the object is specified by default. |
restoreRequest | *RestoreRequest | The restore request parameters. |
The following table describes the parameters of RestoreRequest.
Parameter | Type | Description |
days | int32 | The duration of the restored state. For more information, see Restore objects. |
tier | *string | The time it takes to restore the object. For more information, see Restore objects. |
Return values
Return value | Type | Description |
result | *RestoreObjectResult | The return value of the operation. This is valid when `err` is nil. For more information, see RestoreObjectResult. |
err | error | The status of the request. If the request fails, `err` is not nil. |
Sample code
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 storage region.
bucketName string // The bucket name.
objectName string // The object name.
)
// The init function initializes 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 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 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.
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)
}
// Print the result of the restore operation.
log.Printf("restore object result:%#v\n", result)
}
Scenarios
References (Go SDK V2)
For the complete sample code for restoring objects, see GitHub example.
For more information about the API operation for restoring objects, see RestoreObject.
For more information, see Restore objects.