OSS client-side encryption lets you encrypt data locally before you upload it to OSS. This ensures that only authorized key holders can decrypt the data and enhances data security during transmission and storage.
Precautions
The sample code in this topic uses the China (Hangzhou) region ID,
cn-hangzhou, and the public endpoint by default. If you want to access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about the mappings between OSS regions and endpoints, see Regions and endpoints.The examples in this topic read access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
When you use the client-side encryption feature, you are responsible for the integrity of the master key.
When you copy or migrate encrypted data, you are responsible for the integrity of the encryption metadata.
Method definition
Go SDK V2 supports the following methods to use a master key:
Use a user-managed master key (RSA)
The SDK provides a default implementation of RSA. You must provide the public key and private key of the master key to the SDK as parameters.
Use a custom master key
If the RSA master key method does not meet your requirements, you can implement custom encryption and decryption behavior for the master key. This topic uses Alibaba Cloud KMS 3.0 as an example to show how to customize the encryption and decryption of the master key.
Using these two encryption methods can effectively protect client data from leakage. Even if the encrypted data is compromised, it cannot be decrypted to reveal the raw data.
For more information about the principles of OSS client-side encryption, see Client-side encryption.
To use client-side encryption, you must first instantiate an encryption client and then call the client's interfaces to perform operations. Your objects are automatically encrypted and decrypted as part of the request.
type EncryptionClient struct {
...
}
func NewEncryptionClient(c *Client, masterCipher crypto.MasterCipher, optFns ...func(*EncryptionClientOptions)) (eclient *EncryptionClient, err error)Request parameters
Parameter | Type | Description |
c | *Client | A non-encrypted client instance |
masterCipher | crypto.MasterCipher | The master key instance, which is used to encrypt and decrypt data keys. |
optFns | ...func(*EncryptionClientOptions) | (Optional) The configuration options for the encryption client. |
The following table describes the EncryptionClientOptions options.
Parameter | Type | Description |
MasterCiphers | []crypto.MasterCipher | The master key instance group, which is used to decrypt data keys. |
Return values
Return value | Type | Description |
eclient | *EncryptionClient | The encryption client instance. This parameter is valid only when err is nil. |
err | error | The status of creating the encryption client. If the operation fails, err is not nil. |
The following table lists the EncryptionClient interfaces.
Basic interface | Description |
GetObjectMeta | Obtains some metadata of an object. |
HeadObject | Obtains some metadata of an object. |
GetObject | Downloads an object and automatically decrypts it. |
PutObject | Uploads an object and automatically encrypts it. |
InitiateMultipartUpload | Initializes a multipart upload event and a sharding encryption context (EncryptionMultiPartContext). |
UploadPart | Initializes a multipart upload event. You can call this interface to upload part data and automatically encrypt the data. When you call this interface, you must set the sharding encryption context. |
CompleteMultipartUpload | After all part data is uploaded, you can call this interface to merge the parts into a file. |
AbortMultipartUpload | Cancels a multipart upload event and deletes the corresponding part data. |
ListParts | Lists all successfully uploaded parts that belong to a specified upload event. |
Advanced interface | Description |
NewDownloader | Creates a download manager instance. |
NewUploader | Creates an upload manager instance. |
OpenFile | Creates a ReadOnlyFile instance. |
Helper interface | Description |
Unwrap | Obtains a non-encrypted client instance. You can use this instance to access other basic interfaces. |
Use an RSA master key
Use a custom master key
References
For more information about the principles of OSS client-side encryption, see Client-side encryption.
For more information about the Go SDK user guide for client-side encryption, see User Guide.
For the complete sample code for performing a simple upload and download of an object using an RSA master key, see GitHub sample.
For the complete sample code for performing a simple upload and download of an object using a KMS master key, see GitHub sample.