Todos los productos
Search
Centro de documentos

Object Storage Service:Política de retención (SDK de Python V1)

Última actualización: Sep 09, 2026

Las políticas de retención de Object Storage Service (OSS) ofrecen protección de tipo Write Once, Read Many (WORM) para evitar que los datos se modifiquen o se eliminen. Si necesita impedir que todos los usuarios, incluidos los propietarios de los recursos, modifiquen o eliminen objetos de un bucket de OSS durante un periodo específico, configure una política de retención para dicho bucket. Antes de que venza el periodo de retención, solo podrá cargar y leer objetos en el bucket. Una vez que venza este periodo, podrá modificar o eliminar los objetos.

Notas

  • Antes de configurar las políticas de retención, asegúrese de conocer bien esta función. Para obtener más información, consulte Retention policies.

  • En este tema se utiliza el endpoint público de la región China (Hangzhou). Si desea acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.

  • En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para conocer configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.

Crear una política de retención

A continuación se muestra el código para crear una política de retención:

# -*- 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 una política de retención no bloqueada

A continuación se muestra el código para cancelar una política de retención no 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)

# Cancel the unlocked retention policy.
bucket.abort_bucket_worm()

Bloquear una política de retención

A continuación se muestra el código para bloquear una política de retención:

# -*- 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>')

Obtener una política de retención

A continuación se muestra el código para obtener una política de retención:

# -*- 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)

Ampliar el periodo de retención de los objetos

A continuación se muestra el código para ampliar el periodo de retención de los objetos en una política de retención 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)

Referencias

  • Para consultar un ejemplo de código completo sobre políticas de retención, consulte el ejemplo de GitHub.

  • Para obtener más información sobre la operación de API que permite crear una política de retención, consulte InitiateBucketWorm.

  • Para obtener más información sobre la operación de API que permite cancelar una política de retención no bloqueada, consulte AbortBucketWorm.

  • Para obtener más información sobre la operación de API que permite bloquear una política de retención, consulte CompleteBucketWorm.

  • Para obtener más información sobre la operación de API que permite obtener una política de retención, consulte GetBucketWorm.

  • Para obtener más información sobre la operación de API que permite ampliar el periodo de retención de los objetos, consulte ExtendBucketWorm.