Todos os produtos
Search
Central de documentação

Object Storage Service:Manage versioning (OSS SDK for Python 1.0)

Última atualização: Sep 22, 2026

O status de versionamento de um bucket se aplica a todos os objetos nele contidos. Se você ativar o versionamento para um bucket, poderá recuperar qualquer versão anterior de um objeto caso o sobrescreva ou exclua acidentalmente.

Um bucket pode estar em um dos seguintes status de versionamento: desativado (padrão), ativado ou suspenso. Para obter mais informações sobre o versionamento, consulte Versioning.

Observações

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

  • Neste tópico, as credenciais de acesso são obtidas a partir de variáveis de ambiente. Configure as credenciais de acesso. Para obter mais informações, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tópico demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como o uso de um domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Initialization.

  • Defina o status de versionamento de um bucket. Para isso, você deve ter a permissão oss:PutBucketVersioning. Visualize as informações sobre o status de versionamento de um bucket. Para isso, você deve ter a permissão oss:GetBucketVersioning. Para obter mais informações, consulte Grant a custom access policy to a RAM user.

Configure o versionamento para um bucket

Ative ou suspenda o versionamento de um bucket. O código a seguir apresenta um exemplo:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import BucketVersioningConfig
# 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)

# Initialize versioning configurations for the bucket. 
config = BucketVersioningConfig()
# Set the versioning state to enabled or suspended. 
config.status = oss2.BUCKET_VERSIONING_ENABLE

# Configure versioning for the bucket. 
result = bucket.put_bucket_versioning(config)
# View the HTTP status code. 
print('http response code:', result.status)

Consulte o status de versionamento de um bucket

Consulte o status de versionamento de um bucket. O código a seguir fornece um exemplo:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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)

# Query the versioning state of the bucket. 
versioning_info = bucket.get_bucket_versioning()
# Display the versioning state of the bucket. If versioning has been enabled, Enabled or Suspended is returned. If versioning has not been enabled, None is returned. 
print('bucket versioning status:', versioning_info.status)

Referências

  • Para o código de exemplo completo, acesse o GitHub e configure o versionamento de um bucket.

  • Para obter mais informações sobre a operação de API, consulte PutBucketVersioning e configure o versionamento de um bucket.

  • Para obter mais informações sobre a operação de API que você pode chamar para consultar o status de versionamento de um bucket, consulte GetBucketVersioning.