All Products
Search
Document Center

Object Storage Service:Server-side encryption (Python SDK V1)

Last Updated:Nov 28, 2025

Object Storage Service (OSS) can encrypt uploaded data on the server. This is called server-side encryption. When you upload data to OSS, OSS encrypts the uploaded data and then persistently stores the encrypted data. When you download data from OSS, OSS decrypts the data and returns the decrypted data. In addition, a header is added to the response to declare that the data is encrypted on the server.

Usage notes

  • Before you configure server-side encryption, make sure that you understand this feature. For more information, see Server-side encryption.

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To configure server-side encryption for a bucket, you must have the oss:PutBucketEncryption permission. To query the server-side encryption configurations of a bucket, you must have the oss:GetBucketEncryption permission. To delete the server-side encryption configurations of a bucket, you must have the oss:DeleteBucketEncryption permission. For more information, see Attach a custom policy to a RAM user.

Configure bucket encryption

The following sample code provides examples on how to configure a default encryption method for a bucket. After you configure the default encryption method, all objects that are uploaded to the bucket without specifying an encryption method are encrypted by using the default encryption method.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ServerSideEncryptionRule
# 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 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 where the Endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Specify the bucket name, for example, examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Create a bucket encryption configuration. This example uses AES256 encryption.
rule = ServerSideEncryptionRule()
rule.sse_algorithm = oss2.SERVER_SIDE_ENCRYPTION_AES256
# Set the KMS key ID. You can set this parameter if you use KMS for encryption. To use a specified key for encryption, enter the specified CMK ID. If you use an OSS-managed CMK for encryption, leave this parameter empty. This parameter must be empty when you use AES256 for encryption.
rule.kms_master_keyid = ""

# Configure bucket encryption.
result = bucket.put_bucket_encryption(rule)

# View the HTTP return code.
print('http response code:', result.status)

Get the bucket encryption configuration

The following sample code provides an example on how to query the server-side encryption configurations of a bucket:

# -*- 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 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 where the Endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Specify the bucket name, for example, examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Get the bucket encryption configuration.
result = bucket.get_bucket_encryption()
# Print the obtained encryption configuration.
print('sse_algorithm:', result.sse_algorithm)
print('kms_master_keyid:', result.kms_master_keyid) # If the bucket is encrypted using AES256, kms_master_keyid is None.

Delete the bucket encryption configuration

The following sample code provides an example on how to delete the server-side encryption configurations of a bucket:

# -*- 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 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 where the Endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Specify the bucket name, for example, examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Delete the bucket encryption configuration.
result = bucket.delete_bucket_encryption()
# View the HTTP return code.
print('http status:', result.status)

References

  • For the complete sample code for server-side encryption, see GitHub.

  • For more information about the API operation used to configure server-side encryption, see PutBucketEncryption.

  • For more information about the API operation used to retrieve the server-side encryption configuration, see GetBucketEncryption.

  • For more information about the API operation used to delete the server-side encryption configuration, see DeleteBucketEncryption.