With client-side encryption, you encrypt your data locally before uploading it to Object Storage Service (OSS). This ensures that only authorized key holders can decrypt the data, improving data security during transmission and storage.
Precautions
The sample code in this topic uses the China (Hangzhou) region ID
cn-hangzhouand its public endpoint as an example. If you want to access OSS from other Alibaba Cloud products in the same region, you must use the internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.When you use the client-side encryption feature, you are responsible for maintaining the security and integrity of the master key.
When you copy or migrate encrypted data, you are responsible for maintaining the integrity of the encryption metadata.
Method definition
Python SDK V2 supports the following two types of master keys:
User-managed RSA master keys
The SDK provides a default Rivest-Shamir-Adleman (RSA) implementation. To use this method, you must provide the public key and private key of the master key as parameters to the SDK.
Custom master keys
If the RSA master key method does not meet your requirements, you can implement your own encryption and decryption behavior for the master key.
These two encryption methods effectively prevent data leaks and protect the security of your client-side data. Even if the encrypted data is leaked, unauthorized parties cannot decrypt the raw data.
For more information about the principles of client-side encryption in OSS, see Client-side encryption.
To use client-side encryption, you must first instantiate an encryption client and then call the interfaces it provides. Your objects are then automatically encrypted and decrypted during the corresponding operations.
class EncryptionClient:
...
def __init__(self,client: Client, master_cipher: MasterCipher, decrypt_master_ciphers: Optional[List[MasterCipher]] = None)Request parameters
Parameter | Type | Description |
client | *Client | A non-encryption client instance. |
master_cipher | MasterCipher | The master key instance used to encrypt and decrypt data keys. |
decrypt_master_ciphers | List[MasterCipher] | The master key instances used to decrypt data keys. |
The following table lists the interfaces provided by EncryptionClient.
Interface | Description |
get_object_meta | Gets partial metadata of an object. |
head_object | Gets all metadata of an object. |
get_object | Downloads and automatically decrypts an object. |
put_object | Uploads and automatically encrypts an object. |
initiate_multipart_upload | Initializes a multipart upload event and a multipart encryption context (EncryptionMultiPartContext). |
upload_part | Initializes a multipart upload event. Call this interface to upload part data and automatically encrypt it. When you call this interface, you must set the multipart encryption context. |
complete_multipart_upload | After all parts are uploaded, call this interface to merge them into a single file. |
abort_multipart_upload | Cancels a multipart upload event and deletes the corresponding part data. |
list_parts | Lists all successfully uploaded parts for a specified upload event. |
Use an RSA master key
Use a custom master key
References
For more information about the principles of client-side encryption in OSS, see Client-side encryption.
For more information about client-side encryption operations in Python SDK V2, see User Guide.
For the complete sample code for using an RSA master key for simple object upload and download, see GitHub example.
For the complete sample code for using a KMS-based master key for simple object upload and download, see GitHub example.