When client-side encryption is enabled, objects are locally encrypted before they are uploaded to Object Storage Service (OSS). Only the owner of the customer master key (CMK) can decrypt the objects. This improves data security during data transmission and storage.
Precautions
-
In this topic, the region ID
cn-hangzhouof China (Hangzhou) is used as an example. By default, the public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use the internal endpoint. For more information about the mapping between regions and endpoints of OSS, see OSS regions and endpoints. -
When you use client-side encryption, you must ensure the integrity and validity of the customer master key (CMK).
-
When you copy or migrate encrypted data, you are responsible for the integrity and validity of object metadata.
Method definition
OSS SDK for Python V2 supports the following methods to use CMKs:
-
RSA-based CMKs managed by yourself
The SDK provides the default Rivest-Shamir-Adleman (RSA) implementation. If you provide a CMK, you must pass the public key and private key to the SDK.
-
Custom CMKs
If the RSA-based CMK method cannot meet your requirements, you can implement encryption and decryption based on custom CMKs.
You can use the preceding encryption methods to prevent data leaks and protect your data on the client. Even if your data is leaked, it cannot be decrypted by others.
For more information about the principles of client-side encryption in OSS, see Client-side encryption in the OSS User Guide.
To use client-side encryption, you must create an instance for the client for client-side encryption and call related API operations. Your objects are automatically encrypted and decrypted as part of the request.
class EncryptionClient:
...
def __init__(self,client: Client, master_cipher: MasterCipher, decrypt_master_ciphers: Optional[List[MasterCipher]] = None)
Request parameters
|
Parameter |
Type |
Description |
|
client |
*Client |
An instance of a non-encryption client. |
|
master_cipher |
MasterCipher |
The CMK instance for encrypting and decrypting data keys. |
|
decrypt_master_ciphers |
List[MasterCipher] |
The CMK instance for decrypting data keys. |
The following table lists the operations provided by EncryptionClient:
|
Basic operation |
Description |
|
get_object_meta |
Queries standard HTTP metadata headers of the object. |
|
head_object |
Queries all metadata information of the object. |
|
get_object |
Downloads and decrypts the object. |
|
put_object |
Uploads and encrypts the object. |
|
initiate_multipart_upload |
Initiates a multipart upload and the multipart encryption context (EncryptionMultiPartContext). |
|
upload_part |
Initiates a multipart upload and automatically performs encryption. When you call the API operation, you must set the context for encryption in multipart upload. |
|
complete_multipart_upload |
Combines all parts into a complete object after all parts are uploaded. |
|
abort_multipart_upload |
Cancels a multipart upload task and deletes all parts uploaded by the multipart upload task. |
|
list_parts |
Lists all parts that were uploaded by the multipart upload task. |
Use RSA-based CMKs
Use custom CMKs
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 OSS SDK for Python V2, see Operation Guide.
-
For the complete sample code on how to use an RSA-based CMK to encrypt objects in simple uploads and decrypt objects in simple downloads, see Github sample.
-
For the complete sample code on how to use a KMS-based CMK to encrypt objects in simple uploads and decrypt objects in simple downloads, see Github sample.