O Object Storage Service (OSS) criptografa os dados enviados no servidor, recurso conhecido como criptografia no lado do servidor. Ao fazer upload de dados para o OSS, o serviço criptografa o conteúdo e o armazena de forma persistente. Durante o download, o OSS descriptografa os dados e retorna o conteúdo original. Além disso, o sistema adiciona um cabeçalho à resposta para indicar que a criptografia foi aplicada no servidor.
Observações
Antes de configurar a criptografia no lado do servidor, certifique-se de compreender esse recurso. Para mais informações, consulte Criptografia no lado do servidor.
Este tópico utiliza o endpoint público da região China (Hangzhou). Caso precise acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, utilize um endpoint interno. Para mais detalhes sobre regiões e endpoints do OSS, consulte Regiões e endpoints.
Os exemplos abaixo demonstram a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação via credenciais do Security Token Service (STS), consulte Criar uma instância OssClient.
Para configurar a criptografia no lado do servidor em um bucket, você precisa da permissão
oss:PutBucketEncryption. Para consultar as configurações de criptografia no lado do servidor de um bucket, é necessária a permissãooss:GetBucketEncryption. Já para excluir essas configurações, utilize a permissãooss:DeleteBucketEncryption. Para mais informações, consulte Conceder uma política personalizada.
Configurar a criptografia do bucket
O código a seguir define o método de criptografia padrão para um bucket. Após a configuração, todos os objetos enviados ao bucket sem um método de criptografia específico serão criptografados automaticamente com o método padrão definido:
#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;
}
Obter a configuração de criptografia do bucket
O exemplo abaixo recupera a configuração de criptografia do bucket:
#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;
}
Excluir a configuração de criptografia do bucket
Utilize o código a seguir para remover a configuração de criptografia do bucket:
#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;
}
Referências
Para definir a criptografia no lado do servidor por meio da API, consulte PutBucketEncryption.
Para recuperar a configuração de criptografia no lado do servidor por meio da API, consulte GetBucketEncryption.
Para excluir a configuração de criptografia no lado do servidor por meio da API, consulte DeleteBucketEncryption.