Example of Encryption
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference
Encrypting an Object to Be Uploaded
Sample code:
// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// Generate an encryption key.
System.Security.Cryptography.Aes aesEncryption = System.Security.Cryptography.Aes.Create();
aesEncryption.KeySize = 256;
aesEncryption.GenerateKey();
string customerkey = Convert.ToBase64String(aesEncryption.Key);
// Upload an object.
try
{
// When uploading an object, use the SSE-KMS algorithm to encrypt the object.
SseKmsHeader kms = new SseKmsHeader();
kms.Algorithm = SseKmsAlgorithmEnum.Kms;
PutObjectRequest request1 = new PutObjectRequest
{
BucketName = "bucketname",
Key = "objectname1",
FilePath = "filePath1",
SseHeader = kms,
};
client.PutObject(request1);
// When uploading an object, use the SSE-C algorithm to encrypt the object.
PutObjectRequest request2 = new PutObjectRequest
{
BucketName = "bucketname",
Key = "objectname2",
FilePath = "filePath2",
SseHeader = new SseCHeader()
{
Algorithm = SseCAlgorithmEnum.Aes256,
KeyBase64 = customerkey
}
};
client.PutObject(request2);
}
catch (ObsException ex)
{
Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}
Decrypting a To-Be-Download Object
Sample code:
// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// Download an object.
try
{
// When downloading an object, use the SSE-C algorithm to decrypt it.
GetObjectRequest request = new GetObjectRequest
{
BucketName = "bucketname",
Key = "objectname2",
// The key used here must be the one used for uploading the object.
SseCHeader = new SseCHeader()
{
Algorithm = SseCAlgorithmEnum.Aes256,
KeyBase64 = "customerkey"
}
};
client.GetObject(request);
}
catch (ObsException ex)
{
Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}
Last Article: Encryption Description
Next Article: Troubleshooting
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.