Todos los productos
Search
Centro de documentos

Object Storage Service:Cifrado del lado del servidor (OSS SDK for C# 1.0)

Última actualización: Sep 25, 2026

Object Storage Service (OSS) cifra los datos cargados directamente en el servidor. A este proceso se le conoce como cifrado del lado del servidor. Cuando usted carga datos en OSS, el servicio los cifra y después almacena de forma persistente la versión cifrada. Al descargar datos desde OSS, el servicio los descifra y devuelve la información original. Además, agrega un encabezado a la respuesta para indicar que los datos se cifraron en el servidor.

Notas

  • Antes de configurar el cifrado del lado del servidor, asegúrese de conocer bien esta función. Para obtener más información, consulte Server-side encryption.

  • En este tema se utiliza el endpoint público de la región China (Hangzhou). Si necesita acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization (C# SDK V1).

  • Para configurar el cifrado del lado del servidor de un bucket, es obligatorio contar con el permiso oss:PutBucketEncryption. Para consultar la configuración de cifrado del lado del servidor de un bucket, es obligatorio tener el permiso oss:GetBucketEncryption. Para eliminar dicha configuración, se requiere el permiso oss:DeleteBucketEncryption. Para obtener más información, consulte Grant a custom policy.

Configurar el cifrado del lado del servidor para un bucket

A continuación se muestra un ejemplo de código para configurar un método de cifrado predeterminado en un bucket. Una vez establecido este método, todos los objetos que se carguen al bucket sin especificar un cifrado particular se protegerán automáticamente con la configuración predeterminada.

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);
}

Consultar la configuración de cifrado del lado del servidor de un bucket

El siguiente ejemplo de código muestra cómo consultar la configuración de cifrado del lado del servidor de 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);
}

Eliminar la configuración de cifrado del lado del servidor de un bucket

El siguiente ejemplo de código muestra cómo eliminar la configuración de cifrado del lado del servidor de 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);
}

Referencias

  • Para obtener más información sobre la operación de API que permite configurar el cifrado del lado del servidor, consulte PutBucketEncryption.

  • Si desea conocer detalles sobre la operación de API utilizada para consultar la configuración de cifrado del lado del servidor, consulte GetBucketEncryption.

  • La operación de API correspondiente para eliminar la configuración de cifrado del lado del servidor se describe en DeleteBucketEncryption.