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:
The
oss:RestoreObjectpermission. For details, see Grant custom permissions to a RAM userThe region ID and bucket name of the object to restore
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_idis not set, the restore request targets the latest version of the object.
Method definition
restore_object(request: RestoreObjectRequest, **kwargs) → RestoreObjectResultRequest 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
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | str | Yes | The bucket name. |
key | str | Yes | The object name. |
version_id | str | No | The version ID of the object. If not set, the latest version is restored. |
restore_request | RestoreRequest | No | The restore options. See RestoreRequest. |
For the full parameter reference, see RestoreObjectRequest.
RestoreRequest
| Parameter | Type | Required | Description |
|---|---|---|---|
days | int | Yes | The number of days the object stays in the restored state. For details, see Restore objects. |
tier | str | No | The restore priority for Cold Archive or Deep Cold Archive objects. Valid values: Expedited, Standard, Bulk. Default: Standard. For details, see Restore objects. |
Return value
| Type | Description |
|---|---|
RestoreObjectResult | The restore result. See RestoreObjectResult. |
Response fields
| Field | Description |
|---|---|
status_code | HTTP status code of the response. |
request_id | The unique ID of the request. |
version_id | The version ID of the restored object. |
restore_priority | The 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
For the complete sample, see restore_object.py.
To learn more about object restore, see Restore objects.