As políticas de retenção do Object Storage Service (OSS) oferecem proteção WORM (Write Once, Read Many) para impedir a modificação ou exclusão de dados. Para evitar que qualquer usuário, inclusive o proprietário do recurso, modifique ou exclua objetos em um bucket do OSS durante um período determinado, configure uma política de retenção no bucket. Durante o período de retenção, você só pode fazer upload e ler objetos no bucket. Após o término desse prazo, é possível modificar ou excluir os objetos.
Observações
Antes de configurar políticas de retenção, familiarize-se com esse recurso. Para mais informações, consulte Retention policies.
Este tópico utiliza o endpoint público da região China (Hangzhou). Caso precise acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, utilize um endpoint interno. Para obter detalhes sobre regiões e endpoints do OSS, consulte Regions and endpoints.
Neste tópico, as credenciais de acesso são obtidas por meio de variáveis de ambiente. Para saber como configurar essas credenciais, consulte Configure access credentials using OSS SDK for Python 1.0.
O exemplo apresentado demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para outras configurações, como uso de domínio personalizado ou autenticação via Security Token Service (STS), consulte Initialization.
Criar uma política de retenção
O código a seguir mostra como criar uma política de retenção:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Get access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Create a retention policy and set the object retention period to 1 day.
result = bucket.init_bucket_worm(1)
# View the retention policy ID.
print(result.worm_id)
Cancelar uma política de retenção desbloqueada
O código a seguir mostra como cancelar uma política de retenção desbloqueada:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Get access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Cancel the unlocked retention policy.
bucket.abort_bucket_worm()
Bloquear uma política de retenção
O código a seguir mostra como bloquear uma política de retenção:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Get access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Lock the retention policy.
bucket.complete_bucket_worm('<yourWormId>')
Obter uma política de retenção
O código a seguir mostra como recuperar uma política de retenção:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Get access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Get the retention policy.
result = bucket.get_bucket_worm()
# View the retention policy ID.
print(result.worm_id)
# View the retention policy status. The status is "InProgress" if the policy is not locked and "Locked" if the policy is locked.
print(result.state)
# View the retention period of objects.
print(result.retention_period_days)
# View the creation time of the retention policy.
print(result.creation_date)
Estender o período de retenção dos objetos
O código a seguir mostra como estender o período de retenção dos objetos em uma política de retenção bloqueada:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Get access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Extend the retention period of objects in the locked retention policy.
bucket.extend_bucket_worm('<yourWormId>', 2)
Referências
Para visualizar um exemplo completo de código sobre políticas de retenção, acesse o exemplo no GitHub.
Para mais informações sobre a operação de API para criar uma política de retenção, consulte InitiateBucketWorm.
Para mais informações sobre a operação de API para cancelar uma política de retenção desbloqueada, consulte AbortBucketWorm.
Para mais informações sobre a operação de API para bloquear uma política de retenção, consulte CompleteBucketWorm.
Para mais informações sobre a operação de API para recuperar uma política de retenção, consulte GetBucketWorm.
Para mais informações sobre a operação de API para estender o período de retenção de objetos, consulte ExtendBucketWorm.