Todos os produtos
Search
Central de documentação

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

Última atualização: Sep 22, 2026

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

Observações

  • Chame 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) por meio de outros serviços da Alibaba Cloud na mesma região do OSS, use um endpoint interno. Para obter mais informações sobre as regiões e endpoints compatíveis com o OSS, consulte Regions and endpoints.

  • Neste tópico, as credenciais de acesso são obtidas de variáveis de ambiente. Para obter mais informações sobre como configurar credenciais de acesso, consulte Configure access credentials.

  • Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de um domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Configure a client (Go SDK V1).

  • Para restaurar objetos, você deve ter a permissão oss:RestoreObject. Para obter mais informações, consulte Grant a custom policy.

Restaurar um objeto Archive

O exemplo de código a seguir demonstra 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 exemplo de código a seguir demonstra 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 obter mais informações sobre a operação de API que pode ser chamada para restaurar objetos Archive, Cold Archive e Deep Cold Archive, consulte RestoreObject.