Object Storage Service (OSS) peut chiffrer les données téléchargées sur le serveur. Cette fonctionnalité est appelée chiffrement côté serveur. Lorsque vous téléchargez des données vers OSS, le service chiffre ces données avant de les stocker de manière persistante. Lors du téléchargement de données depuis OSS, le service déchiffre les données 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 le point de terminaison 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 point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez la page Régions et points de terminaison.
Cette rubrique explique comment créer une instance OSSClient avec un point de terminaison OSS. Pour connaître d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants Security Token Service (STS), reportez-vous à la section Initialisation (C# SDK V1).
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, l'autorisationoss:GetBucketEncryptionest requise. Enfin, pour supprimer ces configurations, vous devez détenir l'autorisationoss:DeleteBucketEncryption. Pour en savoir plus, consultez la procédure Accorder une stratégie personnalisée.
Configurer le chiffrement côté serveur pour un bucket
L'exemple de code suivant montre comment définir une méthode de chiffrement par défaut pour un bucket. Une fois cette configuration appliquée, tous les objets téléchargés dans le bucket sans spécification de méthode de chiffrement seront automatiquement chiffrés selon la méthode par défaut.
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Configure server-side encryption for the bucket.
var request = new SetBucketEncryptionRequest(bucketName, "KMS", null);
client.SetBucketEncryption(request);
Console.WriteLine("Set bucket:{0} Encryption succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
Interroger les configurations de chiffrement côté serveur d'un bucket
Le code ci-dessous illustre la récupération des configurations de chiffrement côté serveur associées à un bucket :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Query the server-side encryption configurations of the bucket.
var result = client.GetBucketEncryption(bucketName);
Console.WriteLine("Get bucket:{0} Encryption succeeded ", bucketName);
Console.WriteLine("SSEAlgorithm: {0}", result.SSEAlgorithm);
Console.WriteLine("KMSMasterKeyID: {0}", result.KMSMasterKeyID);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
Supprimer les configurations de chiffrement côté serveur d'un bucket
Voici un exemple de suppression des configurations de chiffrement côté serveur d'un bucket :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Delete the server-side encryption configurations of the bucket.
client.DeleteBucketEncryption(bucketName);
Console.WriteLine("Delete bucket:{0} Encryption succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
Références
Pour obtenir des détails sur l'opération API permettant de configurer le chiffrement côté serveur, consultez PutBucketEncryption.
La documentation de l'opération API dédiée à l'interrogation des configurations de chiffrement côté serveur est disponible ici : GetBucketEncryption.
Enfin, pour supprimer ces configurations via l'API, reportez-vous à DeleteBucketEncryption.