All Products
Search
Document Center

Object Storage Service:Restore objects using OSS SDK for Go 2.0

Last Updated:Mar 20, 2026

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:

Usage 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 (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 classReplica expirationCleanRestoredObject support
ArchiveExpires after the days value you setNot supported
Cold ArchiveExpires after the days value you setSupported
Deep Cold ArchiveExpires after the days value you setSupported

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

ParameterTypeDescription
ctxcontext.ContextThe request context, which can be used to set the total timeout for the request.
request*RestoreObjectRequestThe 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

ParameterTypeDescription
bucket*stringThe bucket name.
key*stringThe object name.
versionId*string(Optional) The version of the object to restore. Defaults to the latest version if not set.
restoreRequest*RestoreRequestThe restore request parameters.

RestoreRequest parameters

ParameterTypeDescription
daysint32The number of days the restored state lasts. For more information, see Restore objects.
tier*stringThe time it takes to restore the object. For more information, see Restore objects.

Return values

Return valueTypeDescription
result*RestoreObjectResultThe return value of the operation. Valid when err is nil. For more information, see RestoreObjectResult.
errerrorThe 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(&region, "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(&region, "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)