バージョン管理が有効なバケットでは、オブジェクトの異なるバージョンのストレージクラスが異なる場合があります。 既定では、RestoreObject操作を呼び出してオブジェクトを復元すると、オブジェクトの現在のバージョンが復元されます。 リクエストでバージョンIDを指定して、指定したバージョンのオブジェクトを復元できます。
ノート
このトピックのサンプルコードでは、中国 (杭州) リージョンのリージョンID
cn-hangzhou
を使用しています。 デフォルトでは、パブリックエンドポイントはバケット内のリソースにアクセスするために使用されます。 バケットが配置されているのと同じリージョンにある他のAlibaba Cloudサービスを使用してバケット内のリソースにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。
オブジェクトを復元するには、
oss:RestoreObject
権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。
サンプルコード
次のコードは、アーカイブ、コールドアーカイブ、またはディープコールドアーカイブオブジェクトを復元する方法の例を示しています。
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // Region in which the bucket is located.
bucketName string // Name of the bucket.
objectName string // Name of the object.
)
// Specify the init function used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the name of the object is specified.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSSClient instance.
client := oss.NewClient(cfg)
// Create a request to restore the object.
request := &oss.RestoreObjectRequest{
Bucket: oss.Ptr(bucketName), // Name of the bucket.
Key: oss.Ptr(objectName), // Name of the object.
VersionId: oss.Ptr("yourVersionId"), // Specify the actual version ID.
RestoreRequest: &oss.RestoreRequest{
Days: 3, // Set the duration of the restored state to 3 days.
},
}
// Send the request to restore the object.
result, err := client.RestoreObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to restore object %v", err)
}
// Display the result.
log.Printf("restore object result:%#v\n", result)
}
関連ドキュメント
オブジェクトの復元に使用する完全なサンプルコードについては、GitHubサンプルを参照してください。
オブジェクトを復元するために呼び出すことができるAPI操作の詳細については、「RestoreObject」をご参照ください。
関連する操作の詳細については、「オブジェクトの復元」をご参照ください。