After you add a CNAME record to map a custom domain name to an Object Storage Service (OSS) bucket, you can use the custom domain name to access resources in the bucket. This topic describes how to use a custom domain name to access a bucket.
Map a custom domain name
Assume that you have a website whose domain name is example.com. Users access images on the website by using URLs in the following format: http://img.example.com/x.jpg
. After you migrate the data on the website to an OSS bucket, you can map the domain name to the bucket to access the images by using the same URLs. For more information, see Map custom domain names.
Use a custom domain name
When you use the OSS SDK, you can use a custom domain name as the endpoint of a bucket to access the bucket. In this case, you must set cname
to true.
const OSS = require('ali-oss')
const client = new OSS({
// Use a custom domain name as the endpoint of a bucket to access the bucket.
endpoint: 'http://img.example.com',
// 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.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// 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.
region: 'yourRegion',
cname: true
});
If a custom domain name is mapped to a bucket, the domain name cannot be used to call the list_buckets operation.