Todos os produtos
Search
Central de documentação

Object Storage Service:Server-side encryption (C++ SDK)

Última atualização: Sep 22, 2026

O Object Storage Service (OSS) pode criptografar dados enviados no lado do servidor. Esse processo é chamado de criptografia no lado do servidor. Ao fazer upload de dados para o OSS, o service criptografa os dados e os armazena de forma persistente já criptografados. Ao baixar dados do OSS, o service descriptografa e retorna o conteúdo original. Além disso, a resposta inclui um cabeçalho que indica que os dados foram criptografados no servidor.

Observações

  • Antes de configure a criptografia no lado do servidor, certifique-se de compreender esse recurso. Para mais informações, consulte Server-side encryption.

  • Este tópico utiliza o endpoint público da região china (Hangzhou). Para acessar o oss a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.

  • Este tópico demonstra como crie uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Create an OssClient instance.

  • Para configure a criptografia no lado do servidor de um bucket, você precisa da permissão oss:PutBucketEncryption. Para consultar as configurações de criptografia de um bucket, é necessária a permissão oss:GetBucketEncryption. Para exclua as configurações de criptografia de um bucket, você precisa da permissão oss:DeleteBucketEncryption. Para mais informações, consulte Grant a custom policy.

configure a criptografia do bucket

O código a seguir mostra como defina o método de criptografia padrão de um bucket. Após a configuração, todos os objetos enviados ao bucket sem um método de criptografia especificado são criptografados com o método padrão 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);

    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 código a seguir mostra como obter 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;
}

exclua a configuração de criptografia do bucket

O código a seguir mostra como exclua 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 informações sobre a operação de API que defina a criptografia no lado do servidor, consulte PutBucketEncryption.

  • Para detalhes sobre a operação de API que obtém a configuração de criptografia no lado do servidor, consulte GetBucketEncryption.

  • Para saber mais sobre a operação de API que exclua a configuração de criptografia no lado do servidor, consulte DeleteBucketEncryption.