Todos los productos
Search
Centro de documentos

Object Storage Service:Cifrado del lado del servidor (OSS SDK for Python 1.0)

Última actualización: Sep 22, 2026

Object Storage Service (OSS) cifra los datos cargados directamente en el servidor. A esta función se le conoce como cifrado del lado del servidor. Cuando usted carga datos en OSS, el servicio los cifra y después almacena de forma persistente la versión cifrada. Al descargar datos desde OSS, el servicio los descifra y devuelve la información original. Además, se agrega un encabezado a la respuesta para indicar que los datos se cifraron en el servidor.

Notas

  • Antes de configurar el cifrado del lado del servidor, familiarícese con esta función. Para obtener más información, consulte Server-side encryption.

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

  • Las credenciales de acceso de este tema se obtienen a partir de variables de entorno. Para saber cómo configurar estas credenciales, 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 configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.

  • Para configurar el cifrado del lado del servidor en un bucket, es obligatorio contar con el permiso oss:PutBucketEncryption. Para consultar la configuración de cifrado del bucket, requiere el permiso oss:GetBucketEncryption. Por último, para eliminar dicha configuración, necesita el permiso oss:DeleteBucketEncryption. Para obtener más información, consulte Grant a custom policy.

Configurar el cifrado del lado del servidor para un bucket

A continuación se muestra un ejemplo de código para configurar un método de cifrado predeterminado en un bucket. Una vez establecido, todos los objetos que se carguen en el bucket sin especificar un método de cifrado utilizarán automáticamente este valor predeterminado.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ServerSideEncryptionRule
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
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 the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Create encryption configurations for the bucket. In this example, AES-256 encryption is used. 
rule = ServerSideEncryptionRule()
rule.sse_algorithm = oss2.SERVER_SIDE_ENCRYPTION_AES256
# Specify the CMK ID. The CMK ID can be specified only if you use the OSS-KMS encryption method. If you want to use a specified CMK for encryption, enter the CMK ID. If you want to use the default CMK managed by KMS for encryption, leave this parameter empty. If you use AES-256 encryption, you must leave this parameter empty. 
rule.kms_master_keyid = ""

# Apply the encryption configurations to the bucket. 
result = bucket.put_bucket_encryption(rule)

# Display the returned HTTP status code. 
print('http response code:', result.status)

Consultar la configuración de cifrado del lado del servidor de un bucket

El siguiente fragmento de código ilustra cómo consultar la configuración de cifrado del lado del servidor de un bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
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 the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Query the server-side encryption configurations of the bucket. 
result = bucket.get_bucket_encryption()
# Display the obtained server-side encryption configurations. 
print('sse_algorithm:', result.sse_algorithm)
print('kms_master_keyid:', result.kms_master_keyid) # If the encryption method is AES256, set kms_master_keyid to None.

Eliminar la configuración de cifrado del lado del servidor de un bucket

Utilice el siguiente código de ejemplo para eliminar la configuración de cifrado del lado del servidor de un bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
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 the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Delete the server-side encryption configurations of the bucket. 
result = bucket.delete_bucket_encryption()
# Display the returned HTTP status code. 
print('http status:', result.status)

Referencias

  • Para revisar el código de ejemplo completo sobre el cifrado del lado del servidor, visite GitHub.

  • Para obtener más información sobre la operación de API que configura el cifrado del lado del servidor, consulte PutBucketEncryption.

  • Si desea conocer los detalles de la operación de API para consultar la configuración de cifrado del lado del servidor, consulte GetBucketEncryption.

  • La operación de API correspondiente para eliminar la configuración de cifrado del lado del servidor se describe en DeleteBucketEncryption.