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 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, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To configure server-side encryption for a bucket, you must have the
oss:PutBucketEncryptionpermission. To query the server-side encryption configurations of a bucket, you must have theoss:GetBucketEncryptionpermission. To delete the server-side encryption configurations of a bucket, you must have theoss:DeleteBucketEncryptionpermission. For more information, see Attach a custom policy to a RAM user.
Configure bucket encryption
The following code shows how to set the default encryption method for a bucket. After the configuration is complete, all objects that are uploaded to the bucket without a specified encryption method are encrypted using the bucket's default encryption method:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to 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. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Set the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network and other resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
SetBucketEncryptionRequest setrequest(BucketName);
setrequest.setSSEAlgorithm(SSEAlgorithm::KMS);
/* Set server-side encryption using KMS. */
auto outcome = client.SetBucketEncryption(setrequest);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "SetBucketEncryption fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network and other resources. */
ShutdownSdk();
return 0;
}Get the bucket encryption configuration
The following code shows how to retrieve the bucket encryption configuration:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to 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. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Set the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network and other resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Get the server-side encryption configuration. */
GetBucketEncryptionRequest request(BucketName);
auto outcome = client.GetBucketEncryption(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "GetBucketEncryption fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network and other resources. */
ShutdownSdk();
return 0;
}Delete the bucket encryption configuration
The following code shows how to delete the bucket encryption configuration:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to 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. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Set the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network and other resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Delete the server-side encryption configuration. */
DeleteBucketEncryptionRequest request(BucketName);
auto outcome = client.DeleteBucketEncryption(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "DeleteBucketEncryption fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network and other resources. */
ShutdownSdk();
return 0;
}References
For information about the API operation to set server-side encryption, see PutBucketEncryption.
For information about the API operation to retrieve the server-side encryption configuration, see GetBucketEncryption.
For information about the API operation to delete the server-side encryption configuration, see DeleteBucketEncryption.