All Products
Search
Document Center

Object Storage Service:Use a custom domain name (Node.js SDK)

Last Updated:Mar 20, 2026

After you map a custom domain name to a bucket by adding a CNAME record, configure the OSS Node.js SDK to use that domain as the endpoint. This lets you access objects through your own domain without changing existing URLs.

Prerequisites

Before you begin, ensure that you have:

  • Mapped your custom domain name to a bucket by adding a CNAME record. For instructions, see Access OSS using a custom domain name

  • An Alibaba Cloud account with an AccessKey ID and AccessKey secret

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set

Configure the SDK to use a custom domain name

Set endpoint to your custom domain name and cname to true when initializing the OSS client. The cname: true flag tells the SDK to treat the endpoint as a custom domain rather than an OSS-native endpoint.

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

const client = new OSS({
  // Set endpoint to your custom domain name
  endpoint: 'http://img.example.com',
  // Read credentials from environment variables to avoid hardcoding secrets
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set region to the region where your bucket is located, for example, cn-hangzhou
  region: 'yourRegion',
  // Required when using a custom domain name as the endpoint
  cname: true,
});

Replace yourRegion with the region ID of your bucket, such as cn-hangzhou for the China (Hangzhou) region.

Limitations

Important

A custom domain name is mapped to a specific bucket. The listBuckets operation is not supported when using a custom domain name as the endpoint.

What's next