Restore files (Python SDK V1)

Updated at:

Use the OSS Python SDK to restore an Archive object to a readable state. In buckets with versioning enabled, each object version can have a different storage class. By default, RestoreObject restores the current version. To restore a specific version, pass its version ID.

Prerequisites

Before you begin, ensure that you have:

How it works

Restoring an Archive object is an asynchronous operation:

  1. Call restore_object() to initiate the restore request.

  2. Poll the object's metadata using head_object() and check the x-oss-restore header.

  3. When the header value changes from ongoing-request="true" to another value, the object is ready to read.

Restore an Archive object

The following example checks whether an object is in the Archive storage class, restores a specific version, and polls until the restore completes.

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

# Get access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Set the endpoint for the region where your bucket is located.
# Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# The region parameter is required for V4 signatures.
region = "cn-hangzhou"

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

# Specify the version to restore.
params = {"versionId": "yourObjectVersionId"}

# Replace exampledir/exampleobject.txt with the name of the Archive object.
object_name = "exampledir/exampleobject.txt"

# Check the storage class before initiating restore.
meta = bucket.head_object(object_name, params=params)
if meta.resp.headers["x-oss-storage-class"] == oss2.BUCKET_STORAGE_CLASS_ARCHIVE:
    # Initiate the restore request.
    result = bucket.restore_object(object_name, params=params)
    print("Restore initiated. Version ID:", result.versionid)

    # Poll until restore completes.
    while True:
        meta = bucket.head_object(object_name, params=params)
        restore_status = meta.resp.headers["x-oss-restore"]
        print("Restore status:", restore_status)
        if restore_status == 'ongoing-request="true"':
            time.sleep(5)
        else:
            break

    print("Object is ready to read.")

Replace the following placeholders:

PlaceholderDescriptionExample
examplebucketName of your bucketmy-archive-bucket
exampledir/exampleobject.txtName of the Archive object to restorelogs/2024/archive.log
yourObjectVersionIdVersion ID of the object version to restoreCAEQHhiBgID.....

Usage notes

  • The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information, see Regions and endpoints.

  • To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Initialization.

References