Archive, Cold Archive, and Deep Cold Archive objects must be restored before they can be read. Restoration creates a temporary readable replica alongside the original object. The replica is automatically deleted when the restoration period expires.
Prerequisites
Before you begin, ensure that you have:
The
oss:RestoreObjectpermission. For more information, see Grant custom access policies to RAM users.Access credentials configured as environment variables. For more information, see Configure access credentials.
Usage notes
Only Archive, Cold Archive, and Deep Cold Archive objects support the
RestoreObjectmethod.The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) with the public endpoint. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information, see OSS regions and endpoints.
Restoration behavior by storage class
Restoration behavior differs across storage classes:
| Storage class | Replica expiration | CleanRestoredObject support |
|---|---|---|
| Archive | Expires after the days value you set | Not supported |
| Cold Archive | Expires after the days value you set | Supported |
| Deep Cold Archive | Expires after the days value you set | Supported |
For Cold Archive and Deep Cold Archive objects, use the tier parameter to control retrieval speed. For retrieval times and pricing by tier, see Restore objects.
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) Operation-level configuration parameters. For more information, see Options. |
RestoreObjectRequest parameters
| Parameter | Type | Description |
|---|---|---|
| bucket | *string | The bucket name. |
| key | *string | The object name. |
| versionId | *string | (Optional) The version of the object to restore. Defaults to the latest version if not set. |
| restoreRequest | *RestoreRequest | The restore request parameters. |
RestoreRequest parameters
| Parameter | Type | Description |
|---|---|---|
| days | int32 | The number of days the restored state lasts. 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. Valid when err is nil. For more information, see RestoreObjectResult. |
| err | error | The error status of the request. Non-nil if the request fails. |
Sample code
Restore an object
The following code submits a restore request for an Archive, Cold Archive, or Deep Cold Archive object. The Days parameter sets how long the object stays in the restored state.
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"
)
var (
region string
bucketName string
objectName string
)
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() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load credentials from environment variables and set the region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
request := &oss.RestoreObjectRequest{
Bucket: oss.Ptr(bucketName),
Key: oss.Ptr(objectName),
RestoreRequest: &oss.RestoreRequest{
Days: 3, // The object remains readable for 3 days.
},
}
result, err := client.RestoreObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to restore object %v", err)
}
log.Printf("restore object result:%#v\n", result)
}End the restored state early
When a Cold Archive or Deep Cold Archive object is restored, you are charged for the temporary replica until the restored state expires. To stop incurring these charges before the days value elapses, call CleanRestoredObject. After the call completes, the object returns to the frozen state and can no longer be read directly.
CleanRestoredObject applies only to Cold Archive and Deep Cold Archive objects. Archive objects do not support this operation.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"
)
var (
region string
bucketName string
objectName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located. Required.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket. Required.")
flag.StringVar(&objectName, "object", "", "The name of the object. Required.")
}
func main() {
flag.Parse()
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load credentials from environment variables and set the region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
request := &oss.CleanRestoredObjectRequest{
Bucket: oss.Ptr(bucketName),
Key: oss.Ptr(objectName),
}
result, err := client.CleanRestoredObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to clean restored object %v", err)
}
log.Printf("clean restored object result:%#v\n", result)
}References (Go SDK V2)
For the complete sample code, see GitHub example.
For the API reference, see RestoreObject.
For concepts and pricing, see Restore objects.