Todos os produtos
Search
Central de documentação

Object Storage Service:Restore objects (OSS SDK for Python 1.0)

Última atualização: Sep 22, 2026

Você deve restaurar os objetos Archive, Cold Archive e Deep Cold Archive antes de acessá-los. Este tópico descreve como restaurá-los.

Observações

  • A operação RestoreObject só pode ser chamada em objetos Archive, Cold Archive e Deep Cold Archive.

  • Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre as regiões e os endpoints do OSS, consulte Regions and endpoints.

  • Neste tópico, o código obtém credenciais de acesso de variáveis de ambiente. Para saber como configurar as credenciais de acesso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para outras configurações, como usar um domínio personalizado ou autenticar com credenciais do Security Token Service (STS), consulte Initialization.

  • Para restaurar objetos, você deve ter a permissão oss:RestoreObject. Para obter mais detalhes, consulte Grant a custom policy.

Restaurar um objeto Archive

O código a seguir mostra como restaurar um objeto Archive:

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

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify the full path of the Archive object. Do not include the bucket name in the full path. 
objectName = 'yourArchiveObjectName'
# Specify the duration for which the object remains in the restored state. Default value: 1. Unit: day. In this example, the duration is 2 days. 
days = 2
restore_config = RestoreConfiguration(days=days)
# Initiate a RestoreObject request. 
bucket.restore_object(objectName, input=restore_config)       

Restaurar um objeto Cold Archive

O exemplo de código a seguir demonstra como restaurar um objeto Cold Archive:

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import (RestoreJobParameters,
                         RestoreConfiguration,
                         RESTORE_TIER_EXPEDITED,
                         RESTORE_TIER_STANDARD,
                         RESTORE_TIER_BULK)

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify the full path of the Deep Cold Archive object. Do not include the bucket name in the full path. 
object_name = "yourColdArchiveObjectName"

# Specify the duration for which the object remains in the restored state. Default value: 1. Unit: day. In this example, the duration is 2 days.
days = 2

# Specify the restoration mode. In this example, the restoration mode is RESTORE_TIER_BULK.
# RESTORE_TIER_EXPEDITED: The object is restored within 1 hour. 
# RESTORE_TIER_STANDARD: The object is restored within 2 to 5 hours. 
# RESTORE_TIER_BULK: The object is restored within 5 to 12 hours. 
job_parameters = RestoreJobParameters(RESTORE_TIER_BULK)

restore_config = RestoreConfiguration(days=days, job_parameters=job_parameters)

# Initiate a RestoreObject request. 
bucket.restore_object(object_name, input=restore_config)

Restaurar um objeto Deep Cold Archive

O código a seguir exemplifica como restaurar um objeto Deep Cold Archive:

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import (RestoreJobParameters,
                         RestoreConfiguration,
                         RESTORE_TIER_EXPEDITED,
                         RESTORE_TIER_STANDARD)

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify the full path of the Deep Cold Archive object. Do not include the bucket name in the full path. 
object_name = "yourDeepColdArchiveObjectName"

# Specify the duration for which the object remains in the restored state. Default value: 1. Unit: day. In this example, the duration is 2 days. 
days = 2

# Specify the restoration mode. In this example, the restoration mode is RESTORE_TIER_STANDARD. 
# RESTORE_TIER_EXPEDITED: The object is restored within 12 hours. 
# RESTORE_TIER_STANDARD: The object is restored within 48 hours. 
job_parameters = RestoreJobParameters(RESTORE_TIER_STANDARD)

restore_config = RestoreConfiguration(days=days, job_parameters=job_parameters)

# Initiate a RestoreObject request. 
bucket.restore_object(object_name, input=restore_config)

Referências

  • Para acessar o código de exemplo completo usado para restaurar objetos Archive, Cold Archive e Deep Cold Archive, visite o GitHub.

  • Para obter mais informações sobre a operação de API que você pode chamar para restaurar objetos Archive, Cold Archive e Deep Cold Archive, consulte RestoreObject.