All Products
Search
Document Center

Object Storage Service:Restore objects (Python SDK V2)

Last Updated:Mar 20, 2026

Archive, Cold Archive, and Deep Cold Archive objects must be restored before you can read them. Restoring an object creates a temporary copy alongside the original; the copy is deleted automatically when the restored state expires. This topic shows how to restore these objects using the OSS Python SDK V2.

Prerequisites

Before you begin, ensure that you have:

Usage notes

  • The sample code in this topic uses the public endpoint for the China (Hangzhou) region (cn-hangzhou). To access OSS from another Alibaba Cloud product in the same region, use an internal endpoint instead. For a full list of endpoints, see OSS regions and endpoints.

  • If version_id is not set, the restore request targets the latest version of the object.

Method definition

restore_object(request: RestoreObjectRequest, **kwargs) → RestoreObjectResult

Request syntax

result = client.restore_object(
    oss.RestoreObjectRequest(
        bucket="string",           # required
        key="string",              # required
        version_id="string",       # optional
        restore_request=oss.RestoreRequest(
            days=int,
            tier="Expedited"|"Standard"|"Bulk",  # optional; Cold Archive and Deep Cold Archive only
        )
    )
)

Parameters

RestoreObjectRequest

ParameterTypeRequiredDescription
bucketstrYesThe bucket name.
keystrYesThe object name.
version_idstrNoThe version ID of the object. If not set, the latest version is restored.
restore_requestRestoreRequestNoThe restore options. See RestoreRequest.

For the full parameter reference, see RestoreObjectRequest.

RestoreRequest

ParameterTypeRequiredDescription
daysintYesThe number of days the object stays in the restored state. For details, see Restore objects.
tierstrNoThe restore priority for Cold Archive or Deep Cold Archive objects. Valid values: Expedited, Standard, Bulk. Default: Standard. For details, see Restore objects.

Return value

TypeDescription
RestoreObjectResultThe restore result. See RestoreObjectResult.

Response fields

FieldDescription
status_codeHTTP status code of the response.
request_idThe unique ID of the request.
version_idThe version ID of the restored object.
restore_priorityThe restore priority used for the request.

Sample code

The following example submits a restore request and polls until the object is ready to read.

import time
import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="restore object sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')
parser.add_argument('--key', help='The name of the object.', required=True)

def main():
    args = parser.parse_args()

    # Load credentials from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Configure the client.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client.
    client = oss.Client(cfg)

    # Submit the restore request.
    result = client.restore_object(oss.RestoreObjectRequest(
        bucket=args.bucket,
        key=args.key,
        restore_request=oss.RestoreRequest(
            days=1,
            # Optional: Set restore priority for Cold Archive or Deep Cold Archive objects.
            # Valid values: Expedited, Standard, Bulk. Default: Standard.
            # Expedited costs more; Bulk costs less.
            # tier="Bulk",
        )
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
          f' restore priority: {result.restore_priority},'
    )

    # Poll until the object is restored.
    while True:
        result = client.head_object(oss.HeadObjectRequest(
            bucket=args.bucket,
            key=args.key,
        ))
        if result.restore and result.restore != 'ongoing-request="true"':
            print('Restore is successful')
            break
        time.sleep(5)
        print(result.restore)

if __name__ == "__main__":
    main()

Set the --region, --bucket, and --key arguments to your actual region ID (for example, cn-hangzhou), bucket name, and object name before running the code. The --endpoint argument is optional; omit it to use the default public endpoint.

EnvironmentVariableCredentialsProvider loads your Alibaba Cloud credentials from environment variables. Set these variables in your environment before running the code.

References