Todos los productos
Search
Centro de documentos

Object Storage Service:Restaurar objetos (OSS SDK for Go 1.0)

Última actualización: Sep 22, 2026

Debe restaurar los objetos Archive, Cold Archive y Deep Cold Archive antes de poder leerlos.

Notas

  • Llame a la operación RestoreObject únicamente para restaurar objetos Archive, Cold Archive y Deep Cold Archive.

  • En este tema se utiliza el endpoint público de la región China (Hangzhou). Si desea acceder a Object Storage Service (OSS) mediante otros servicios de Alibaba Cloud en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints compatibles con OSS, consulte Regiones y endpoints.

  • En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, consulte Configurar credenciales de acceso.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Configurar un cliente (Go SDK V1).

  • Para restaurar objetos, necesita el permiso oss:RestoreObject. Para obtener más información, consulte Otorgar una política personalizada.

Restaurar un objeto Archive

A continuación se muestra un ejemplo de código para restaurar un 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 un objeto Cold Archive o Deep Cold Archive

A continuación se muestra un ejemplo de código para restaurar un objeto Cold Archive o 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")
}

Referencia

Para obtener más información sobre la operación de API que permite restaurar objetos Archive, Cold Archive y Deep Cold Archive, consulte RestoreObject.