You must restore Archive, Cold Archive, and Deep Cold Archive objects before you can read them. When an object is restored, a temporary copy is created. This copy exists alongside the original object and is automatically deleted after the restored state expires. This topic describes how to use the OSS Python SDK to restore these objects.
Notes
The sample code in this topic uses a public endpoint in the China (Hangzhou) region (region ID:
cn-hangzhou) as an example. If you want to access OSS from another Alibaba Cloud product in the same region, use an internal endpoint. For more information about the endpoints for each OSS region, see OSS regions and endpoints.To restore an object, you must have the
oss:RestoreObjectpermission. For more information, see Grant custom permissions to a RAM user.
Method definition
restore_object(request: RestoreObjectRequest, **kwargs) → RestoreObjectResultRequest parameters
Parameter | Type | Description |
request | RestoreObjectRequest | The request parameters. For more information, see RestoreObjectRequest |
The following table describes the common parameters of RestoreObjectRequest.
Parameter | Type | Description |
bucket | str | The bucket name. |
key | str | The object name. |
version_id | str | The version ID of the object to restore. If you do not set this parameter, the latest version of the object is restored. |
restore_request | RestoreRequest | The restore request parameters. For more information, see RestoreRequest. |
The following table describes the parameters of RestoreRequest.
Parameter | Type | Description |
days | int | The duration for which the object remains in the restored state. For more information, see Restore objects. |
tier | str | The time it takes to restore the object. For more information, see Restore objects. |
Return values
Type | Description |
RestoreObjectResult | The return value. For more information, see RestoreObjectResult. |
For the complete method definition, see restore_object.py.
Sample code
You can use the following code to restore Archive, Cold Archive, or Deep Cold Archive objects:
import time
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="restore object sample")
# Add the required command-line arguments.
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():
# Parse the incoming command-line arguments.
args = parser.parse_args()
# Load authentication information from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default SDK configurations.
cfg = oss.config.load_default()
# Set the credential provider to environment variable-based authentication.
cfg.credentials_provider = credentials_provider
# Set the region.
cfg.region = args.region
# If an endpoint is provided, set the endpoint as a configuration item.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client.
client = oss.Client(cfg)
# Execute the request to restore the object.
result = client.restore_object(oss.RestoreObjectRequest(
bucket=args.bucket,
key=args.key,
restore_request=oss.RestoreRequest(
days=1,
# Optional: Set the restoration priority for Cold Archive or Deep Cold Archive objects. Valid values: Expedited, Standard, and Bulk. Default value: Standard.
# tier="Bulk",
)
))
# Print the result of the restore request.
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},'
)
# Loop to check whether the object is restored.
while True:
# Get the object header.
result = client.head_object(oss.HeadObjectRequest(
bucket=args.bucket,
key=args.key,
))
# Check the restored state.
if result.restore and result.restore != 'ongoing-request="true"':
print('Restore is successful')
break
# Check again after 5 seconds.
time.sleep(5)
print(result.restore)
# Program entry point.
if __name__ == "__main__":
main()
References
For the complete sample code to restore objects, see restore_object.py.
For more information about how to restore objects, see Restore objects.