You must restore an archive object before you read it. Do not call restoreObject for non-archive objects.

The state conversion process of an archive object is as follows:

  • An archive object is in the frozen state.
  • After you submit it for restoration, the server restores the object. The object is in the restoring state.
  • One minute later, the object enters the restored state and can be read. The restored state lasts for one day by default, and can be prolonged to a maximum of seven days. Once this period ends, the object returns to the frozen state.

For detailed information about archive storage class, see Introduction to storage classes For detailed information about related status code, see RestoreObject. For the complete code of restoring an archive object, see GitHub.

Run the following code to restore an object:

package main

import (
	"fmt"
	"os"
	"time"
	"strings"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		HandleError(err)
	}

	bucketName := "<yourBucketName>"
	objectName := "<yourObjectName>"
	localFilename := "<yourLocalFilename>"

	// Create a bucket.
	err = client.CreateBucket(bucketName, oss.StorageClass(oss.StorageArchive))
	if err ! = nil {
		HandleError(err)
	}

	// Obtain the bucket.
	archiveBucket, err := client.Bucket(bucketName)
	if err ! = nil {
		HandleError(err)
	}

	// Upload the archived object.
	var val = "More than just cloud."
	err = archiveBucket.PutObject(objectName, strings.NewReader(val))
	if err ! = nil {
		HandleError(err)
	}

	// Check whether the object is an archive object.
	meta, err := archiveBucket.GetObjectDetailedMeta(objectName)
	if err ! = nil {
		HandleError(err)
	}

	fmt.Println("X-Oss-Storage-Class : ", meta.Get("X-Oss-Storage-Class"))
	if meta.Get("X-Oss-Storage-Class") == string(oss.StorageArchive) {
		// Restore the archive object for read.
		err = archiveBucket.RestoreObject(objectName)
		if err ! = nil {
			HandleError(err)
		}

		// Wait until the object is restored.
		meta, err = archiveBucket.GetObjectDetailedMeta(objectName)
		for meta.Get("X-Oss-Restore") == "ongoing-request=\"true\"" {
			fmt.Println("x-oss-restore:" + meta.Get("X-Oss-Restore"))
			time.Sleep(1000 * time.Second)
			meta, err = archiveBucket.GetObjectDetailedMeta(objectName)
		}
	}

	// Download the restored object.
	err = archiveBucket.GetObjectToFile(objectName, localFilename)
	if err ! = nil {
		HandleError(err)
	}

	// Restore the object again.
	err = archiveBucket.RestoreObject(objectName)

	fmt.Println("ArchiveSample completed")
}