すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:オブジェクトを復元する

最終更新日:Nov 04, 2024

バージョン管理が有効なバケットでは、オブジェクトの異なるバージョンのストレージクラスが異なる場合があります。 既定では、RestoreObject操作を呼び出してオブジェクトを復元すると、オブジェクトの現在のバージョンが復元されます。 リクエストでバージョンIDを指定して、指定したバージョンのオブジェクトを復元できます。

使用上の注意

  • このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。

  • このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。

  • このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。

  • オブジェクトを復元するには、oss:RestoreObject権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。

サンプルコード

次のサンプルコードは、オブジェクトを復元する方法の例を示しています。

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Add the versionId field to param. 
params = dict()
params['versionId'] = 'yourObjectVersionId'

# Specify the name of an Archive object. If no Archive objects are stored in the bucket, call the put_object operation to create an Archive object. 
object_name = 'yourArchiveObjectName'

# Query the metadata of the specified version of the object. 
meta = bucket.head_object(object_name, params=params)
# Check whether the storage class of the object is Archive. 
if meta.resp.headers['x-oss-storage-class'] == oss2.BUCKET_STORAGE_CLASS_ARCHIVE:
     # Restore the specified version of the object. 
     result = bucket.restore_object(object_name, params=params)
     # Display the version ID of the object that you want to restore. 
     print('restore object version id:', result.versionid)

     while True:
         meta = bucket.head_object(object_name, params=params)
         print('x-oss-restore:', meta.resp.headers['x-oss-restore'])
         # Check the restoration status of the object. 
         if meta.resp.headers['x-oss-restore'] == 'ongoing-request="true"':
             time.sleep(5)
         else:
             break

関連ドキュメント

オブジェクトを復元するために呼び出すことができるAPI操作の詳細については、「RestoreObject」をご参照ください。