Restore files (Python SDK V1)
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:
An OSS bucket containing Archive objects
The
oss:RestoreObjectpermission. For details, see Attach a custom policy to a RAM userAccess credentials configured as environment variables. For details, see Configure access credentials using OSS SDK for Python 1.0
How it works
Restoring an Archive object is an asynchronous operation:
Call
restore_object()to initiate the restore request.Poll the object's metadata using
head_object()and check thex-oss-restoreheader.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:
| Placeholder | Description | Example |
|---|---|---|
examplebucket | Name of your bucket | my-archive-bucket |
exampledir/exampleobject.txt | Name of the Archive object to restore | logs/2024/archive.log |
yourObjectVersionId | Version ID of the object version to restore | CAEQHhiBgID..... |
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
RestoreObject — API reference for the restore operation