All Products
Search
Document Center

Object Storage Service:Server-side encryption (Node.js SDK)

Last Updated:Nov 29, 2025

Object Storage Service (OSS) can encrypt uploaded data on the server. This is called server-side encryption. When you upload data to OSS, OSS encrypts the uploaded data and then persistently stores the encrypted data. When you download data from OSS, OSS decrypts the data and returns the decrypted data. In addition, a header is added to the response to declare that the data is encrypted on the server.

Configure bucket encryption

The following sample code provides examples on how to configure a default encryption method for a bucket. After you configure the default encryption method, all objects that are uploaded to the bucket without specifying an encryption method are encrypted by using the default encryption method.

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

Get bucket encryption configuration

The following sample code provides an example on how to query the server-side encryption configurations of a 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();

Delete bucket encryption configuration

The following sample code provides an example on how to delete the server-side encryption configurations of a 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();

References

  • For complete sample code for server-side encryption, see GitHub examples.

  • For more information about the API operation used to set server-side encryption, see PutBucketEncryption.

  • For more information about the API operation used to retrieve the server-side encryption configuration, see GetBucketEncryption.

  • For more information about the API operation used to delete the server-side encryption configuration, see DeleteBucketEncryption.