Object Storage Service (OSS) chiffre les données sur le serveur. Cette fonctionnalité s'appelle le chiffrement côté serveur. Lorsque vous chargez des données vers OSS, le service les chiffre avant de les stocker de manière persistante. Lorsque vous téléchargez des données depuis OSS, le service les déchiffre et renvoie le contenu en clair. De plus, un en-tête est ajouté à la réponse pour indiquer que les données sont chiffrées côté serveur.
Notes
Avant de configurer le chiffrement côté serveur, assurez-vous de bien comprendre cette fonctionnalité. Pour plus d'informations, consultez la rubrique Chiffrement côté serveur.
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez la rubrique Régions et endpoints.
Cette rubrique explique comment créer une instance OssClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants Security Token Service (STS), consultez la rubrique Créer une instance OssClient.
Pour configurer le chiffrement côté serveur d'un bucket, vous devez disposer de l'autorisation
oss:PutBucketEncryption. Pour interroger les configurations de chiffrement côté serveur d'un bucket, vous devez disposer de l'autorisationoss:GetBucketEncryption. Pour supprimer les configurations de chiffrement côté serveur d'un bucket, vous devez disposer de l'autorisationoss:DeleteBucketEncryption. Pour plus d'informations, consultez la rubrique Accorder une politique personnalisée.
Configurer le chiffrement du bucket
Le code suivant montre comment définir la méthode de chiffrement par défaut pour un bucket. Une fois la configuration appliquée, tous les objets chargés dans le bucket sans méthode de chiffrement spécifiée seront chiffrés selon la méthode par défaut du 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;
}
Récupérer la configuration de chiffrement du bucket
Le code suivant montre comment récupérer la configuration de chiffrement du 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;
}
Supprimer la configuration de chiffrement du bucket
Le code suivant montre comment supprimer la configuration de chiffrement du 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;
}
Références
Pour plus d'informations sur l'opération API permettant de définir le chiffrement côté serveur, consultez la rubrique PutBucketEncryption.
Pour plus d'informations sur l'opération API permettant de récupérer la configuration de chiffrement côté serveur, consultez la rubrique GetBucketEncryption.
Pour plus d'informations sur l'opération API permettant de supprimer la configuration de chiffrement côté serveur, consultez la rubrique DeleteBucketEncryption.