Restore objects (Go SDK V2)

Updated at:
Copy as MD

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 IDcn-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 theoss:RestoreObject permission. 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 is nil. For details, see RestoreObjectResult.

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(&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 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

Delete a temporary replica

When you restore an object from the Cold Archive or Deep Cold Archive storage class, a temporary replica is created for access. You incur storage charges for this replica until the restoration period ends. If you want to end the restored state early and stop incurring charges for the replica, you can send a CleanRestoredObject request. After the request is complete, the object returns to the frozen state and cannot be directly accessed.

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 arguments.
var (
	region     string // The OSS region.
	bucketName string // The bucket name.
	objectName string // The object name.
)

// init initializes the command-line flag parser and sets default values and help messages.
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 flags.
	flag.Parse()

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

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

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

	// Load the default configuration, set the credential provider to read from environment variables, and set the region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to delete the temporary 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 temporary replica.
	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

  • 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.