Todos los productos
Search
Centro de documentos

Object Storage Service:Cifrado del lado del servidor (SDK de Node.js)

Última actualización: Sep 24, 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 los almacena de forma persistente. Al descargarlos, OSS descifra la información y devuelve los datos originales. Además, agrega un encabezado a la respuesta para indicar que los datos se cifraron en el servidor.

Configurar el cifrado de 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, todos los objetos que se carguen en ese bucket sin especificar un método de cifrado usarán automáticamente el método predeterminado.

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function putBucketEncryption() {
  try {
    // Configure the encryption method for the bucket.    

    const result = await client.putBucketEncryption("bucket-name", {
      SSEAlgorithm: "AES256", // This example shows how to set AES256 encryption. If you use KMS encryption, you must add the KMSMasterKeyID property.
      // KMSMasterKeyID: "yourKMSMasterKeyId". Set the KMS key ID. You can set this parameter if the encryption method is KMS. If the value of SSEAlgorithm is KMS and you use a specified key for encryption, you must enter the key ID. Otherwise, this parameter must be empty.
    });
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

putBucketEncryption();

Obtener la configuración de cifrado 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:

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function getBucketEncryption() {
  try {
    const result = await client.getBucketEncryption("bucket-name");
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

getBucketEncryption();

Eliminar la configuración de cifrado 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:

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function deleteBucketEncryption() {
  try {
    // Delete the encryption configuration of the bucket.
    const result = await client.deleteBucketEncryption("bucket-name");
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

deleteBucketEncryption();

Referencias

  • Para consultar el código de ejemplo completo sobre el cifrado del lado del servidor, consulte ejemplos en GitHub.

  • Para obtener más información sobre la API utilizada para configurar el cifrado del lado del servidor, consulte PutBucketEncryption.

  • Si necesita detalles sobre la API que permite recuperar la configuración de cifrado del lado del servidor, consulte GetBucketEncryption.

  • Para conocer más acerca de la API con la que se elimina la configuración de cifrado del lado del servidor, consulte DeleteBucketEncryption.