You must restore Archive, Cold Archive, and Deep Cold Archive objects before you can read them. This topic describes how to restore these objects.
Usage notes
The RestoreObject method can be used only for Archive, Cold Archive, and Deep Cold Archive objects.
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
To restore a file, you must have the
oss:RestoreObjectpermission. For more information about how to grant permissions to a Resource Access Management (RAM) user, see Grant custom permissions to a RAM user.
Restore an Archive object
The following code shows how to restore an Archive object:
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this 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.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
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 bucket name. Example: examplebucket.
bucketName := "examplebucket"
// Specify the full path of the object. Do not include the bucket name. Example: exampledir/exampleobject.txt.
objectName := "exampledir/exampleobject.txt"
// Obtain the bucket instance.
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")
}
Restore a Cold Archive or Deep Cold Archive object
The following code shows how to restore a Cold Archive or Deep Cold Archive object:
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this 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.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
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 bucket name. Example: examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
}
// Configure restore parameters.
var restoreConfig oss.RestoreConfiguration
// Select a restoration speed tier:
// The object is restored within 1 hour.
// restoreConfig.Tier = string(oss.RestoreExpedited)
// The object is restored within 2 to 5 hours.
// restoreConfig.Tier = string(oss.RestoreStandard)
// The object is restored within 5 to 12 hours.
restoreConfig.Tier = string(oss.RestoreBulk)
// Set the number of days for which the restored object remains accessible. The value can be from 1 to 365.
restoreConfig.Days = 1
// Restore the Cold Archive or Deep Cold Archive object.
// Set yourObjectName to the full path of the object. Do not include the bucket name. 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")
}
References
For more information about the API operation used to restore Archive, Cold Archive, and Deep Cold Archive objects, see RestoreObject.