All Products
Search
Document Center

Object Storage Service:Restore objects (Go SDK V2)

Last Updated:Sep 25, 2025

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-hangzhou as 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:RestoreObject permission. 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(&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() {
	// 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

Use the CleanRestoredObject method to delete object replicas

When a Cold Archive or Deep Cold Archive object is restored, a replica is created for access. You are charged for the temporary storage of this replica until the restored state expires. If you want to end the restored state early and stop incurring storage fees for the replica, you can call the CleanRestoredObject operation. After the operation is complete, the object returns to the frozen state and cannot be read directly.

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 to store command-line parameters.
var (
	region     string // The region where the OSS bucket is located.
	bucketName string // The bucket name.
	objectName string // The object name.
)

// The init function initializes the command-line flag parser and sets the default value and help message for each parameter.
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() {
	// Parse command-line parameters.
	flag.Parse()

	// Check if the required parameter region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Check if the required parameter bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the required parameter 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 (read from environment variables) and the specified region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to delete the replica of the restored object.
	request := &oss.CleanRestoredObjectRequest{
		Bucket: oss.Ptr(bucketName), // Set the bucket name.
		Key:    oss.Ptr(objectName), // Set the object name.
	}

	// Send the request to delete the replica of the restored object.
	result, err := client.CleanRestoredObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to clean restored object %v", err)
	}

	// Print the result.
	log.Printf("clean restored object result:%#v\n", result)
}

References (Go SDK V2)