All Products
Search
Document Center

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

Last Updated:Nov 29, 2025

After you map a custom domain name to a specific bucket by adding a CNAME record, you can use the domain name to access OSS resources. This topic describes how to use a custom domain name.

Map a custom domain name

For example, if your custom domain name is example.com and you previously accessed all images using a URL format such as http://img.example.com/x.jpg, you can map the custom domain name after you migrate your resources to OSS. This lets you continue accessing the images using the original address. For more information, see Access OSS using a custom domain name.

Use a custom domain name

To use a custom domain name as the Endpoint with the SDK, set the cname parameter to true.

const OSS = require('ali-oss')

const client = new OSS({
  // Use a custom domain name as the Endpoint.
  endpoint: 'http://img.example.com', 
  // Obtain access credentials from environment variables. Before you run this sample 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 yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou.
  region: 'yourRegion',
  cname: true
});
Important

A custom domain name is mapped to a specific bucket. Therefore, you cannot use the list_buckets operation when you use a custom domain name as the endpoint.