All Products
Search
Document Center

Object Storage Service:Restore files (Python SDK V1)

Last Updated:Nov 28, 2025

For a bucket with versioning enabled, different versions of an object can have different storage classes. The RestoreObject operation restores the current version of an object by default. You can also restore a specific version of an object by specifying its version ID.

Usage notes

  • 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 using OSS SDK for Python 1.0.

  • 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 code provides an example of how to restore a file:

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

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

# Set the endpoint to the one for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Set the region that corresponds to the endpoint, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

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

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

# Replace 'yourArchiveObjectName' with the name of an Archive object. If the object does not exist, call the put_object operation to create it.
object_name = 'yourArchiveObjectName'

# Obtain the metadata of the specified object version.
meta = bucket.head_object(object_name, params=params)
# Check if 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)
    # View the version ID of the restored object.
    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 restored state.
         if meta.resp.headers['x-oss-restore'] == 'ongoing-request="true"':
             time.sleep(5)
         else:
             break

References

For more information about the API operation used to restore a file, see RestoreObject.