All Products
Search
Document Center

Object Storage Service:Restore objects

Last Updated:Sep 06, 2023

In a versioning-enabled bucket, the storage classes of different versions of an object can be different. By default, when you call the RestoreObject operation to restore an object, the current version of the object is restored. You can specify a version ID in the request to restore the specified version of an object.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 Initialization.

  • To restore an object, you must have the oss:RestoreObject permission. For more information, see Attach a custom policy to a RAM user.

Sample code

The following sample code provides an example on how to restore an object:

# -*- 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.ProviderAuth(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. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# 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

References

For more information about the API operation that you can call to restore an object, see RestoreObject.