Todos os produtos
Search
Central de documentação

Object Storage Service:Restore objects (OSS SDK for Go 1.0)

Última atualização: Jul 03, 2026

Restaure os objetos Archive, Cold Archive e Deep Cold Archive antes de lê-los.

Observações

  • Use a operação RestoreObject apenas para restaurar objetos Archive, Cold Archive e Deep Cold Archive.

  • Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o Object Storage Service (OSS) com outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre as regiões e endpoints compatíveis com o OSS, consulte Regiões e endpoints.

  • As credenciais de acesso deste exemplo vêm de variáveis de ambiente. Para configurar as credenciais de acesso, consulte Configurar credenciais de acesso.

  • Este exemplo cria uma instância OSSClient com um endpoint do OSS. Para outras configurações, como uso de domínio personalizado ou autenticação via Security Token Service (STS), consulte Configurar um cliente (Go SDK V1).

  • A permissão oss:RestoreObject é obrigatória para restaurar objetos. Para mais informações, consulte Conceder uma política personalizada.

Restaurar um objeto Archive

O código de exemplo a seguir mostra como restaurar um objeto Archive:

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
	objectName := "exampledir/exampleobject.txt"

	// Create a bucket.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Restore the Archive object. 
	err = bucket.RestoreObject(objectName)
	if err != nil {
		log.Fatalf("Failed to restore object '%s': %v", objectName, err)
	}

	log.Println("ArchiveSample completed")
}

Restaurar um objeto Cold Archive ou Deep Cold Archive

O código de exemplo a seguir mostra como restaurar um objeto Cold Archive ou Deep Cold Archive:

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Configure restoration parameters.
	var restoreConfig oss.RestoreConfiguration

	// Specify the restoration mode.
	// Specify that the object is restored within 1 hour. 
	// restoreConfig.Tier = string(oss.RestoreExpedited)
	// Specify that the object is restored within 2 to 5 hours. 
	// restoreConfig.Tier = string(oss.RestoreStandard)
	// Specify that the object is restored within 5 to 12 hours. 
	restoreConfig.Tier = string(oss.RestoreBulk)

	// Specify the duration for which the object remains in the restored state. Unit: days. Valid values: 1 to 365. 
	restoreConfig.Days = 1

	// Restore the Cold Archive or Deep Cold Archive object. 
	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
	objectName := "exampledir/exampleobject.txt"
	err = bucket.RestoreObjectDetail(objectName, restoreConfig)
	if err != nil {
		log.Fatalf("Failed to restore object '%s': %v", objectName, err)
	}

	log.Println("ArchiveSample completed")
}

Referência

Para mais informações sobre a operação de API usada para restaurar objetos Archive, Cold Archive e Deep Cold Archive, consulte RestoreObject.