You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Manage Bucket Configuration
Managing Custom Domains (Node.js SDK)
Copy page
Download PDF
Managing Custom Domains (Node.js SDK)
After you upload a file to TOS, you can access or download it using the file URL automatically generated by TOS. If you want to use your own domain name to access files stored in TOS, you can bind your own domain name to the bucket. After binding, the file can be accessed using the corresponding domain name.​
Set up custom domain rules​
warning​
  • A domain name can only be bound to one bucket.​
  • A single bucket can bind up to 20 domain names.​
Sample code​
The following code sets the custom domain name rules for the bucket.​
​
// Import SDK, if the TOS Node.JS SDK version is less than 2.5.2, please change "TosClient" to "TOS" in the code below.​
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk';​
​
// Create a client.​
const client = new TosClient({​
accessKeyId: process.env['TOS_ACCESS_KEY'],​
accessKeySecret: process.env['TOS_SECRET_KEY'],​
region: "Provide your region", // Fill in the region where the Bucket is located. For example, if it is in China North 2 (Beijing), fill in "Provide your region" as cn-beijing.​
endpoint: "Provide your endpoint", // Enter the domain address.​
});​
​
function handleError(error) {​
if (error instanceof TosClientError) {​
console.log('Client Err Msg:', error.message);​
console.log('Client Err Stack:', error.stack);​
} else if (error instanceof TosServerError) {​
console.log('Request ID:', error.requestId);​
console.log('Response Status Code:', error.statusCode);​
console.log('Response Header:', error.headers);​
console.log('Response Err Code:', error.code);​
console.log('Response Err Msg:', error.message);​
} else {​
console.log('unexpected exception, message: ', error);​
}​
}​
​
const bucketName = 'node-sdk-test-bucket';​
async function main() {​
try {​
// Scene 2: Redirecting requests according to the rules.​
const { requestId: putCustomDomainRequestId } = await client.putBucketCustomDomain({​
bucket: bucketName,​
customDomainRule: {​
Domain: 'www.example.com',​
},​
});​
console.log('putBucketCustomDomain Request ID: ', putCustomDomainRequestId);​
} catch (error) {​
handleError(error);​
}​
}​
​
main();​
​
List custom domain name rules​
warning​
To enumerate custom domain name rules for buckets, you must be the bucket owner by default.​
Sample code​
The following code enumerates custom domain name rules for buckets.​
​
// Import SDK, if the TOS Node.JS SDK version is less than 2.5.2, please change "TosClient" to "TOS" in the code below.​
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk';​
​
// Create a client.​
const client = new TosClient({​
accessKeyId: process.env['TOS_ACCESS_KEY'],​
accessKeySecret: process.env['TOS_SECRET_KEY'],​
region: "Provide your region", // Fill in the region where the Bucket is located. For example, if it is in China North 2 (Beijing), fill in "Provide your region" as cn-beijing.​
endpoint: "Provide your endpoint", // Enter the domain address.​
});​
​
function handleError(error) {​
if (error instanceof TosClientError) {​
console.log('Client Err Msg:', error.message);​
console.log('Client Err Stack:', error.stack);​
} else if (error instanceof TosServerError) {​
console.log('Request ID:', error.requestId);​
console.log('Response Status Code:', error.statusCode);​
console.log('Response Header:', error.headers);​
console.log('Response Err Code:', error.code);​
console.log('Response Err Msg:', error.message);​
} else {​
console.log('unexpected exception, message: ', error);​
}​
}​
​
const bucketName = 'node-sdk-test-bucket';​
async function main() {​
try {​
const { data, requestId: getCustomDomainRequestId } = await client.getBucketCustomDomain({​
bucket: bucketName,​
});​
console.log('getBucketCustomDomain Request ID: ', getCustomDomainRequestId);​
console.log('getBucketCustomDomain Rules Length: ', data.CustomDomainRules.length);​
// View custom domain​
for (const rule of data.CustomDomainRules) {​
console.log('getBucketCustomDomain Rule Domain:', rule.Domain);​
}​
} catch (error) {​
handleError(error);​
}​
}​
​
main();​
​
Delete custom domain rules​
warning​
To remove a custom domain name rule for a bucket, you must be the bucket owner by default.​
Sample code​
The following code is used to delete the custom domain name rule for the bucket.​
​
// Import SDK, if the TOS Node.JS SDK version is less than 2.5.2, please change "TosClient" to "TOS" in the code below.​
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk';​
​
// Create a client.​
const client = new TosClient({​
accessKeyId: process.env['TOS_ACCESS_KEY'],​
accessKeySecret: process.env['TOS_SECRET_KEY'],​
region: "Provide your region", // Fill in the region where the Bucket is located. For example, if it is in China North 2 (Beijing), fill in "Provide your region" as cn-beijing.​
endpoint: "Provide your endpoint", // Enter the domain address.​
});​
​
function handleError(error) {​
if (error instanceof TosClientError) {​
console.log('Client Err Msg:', error.message);​
console.log('Client Err Stack:', error.stack);​
} else if (error instanceof TosServerError) {​
console.log('Request ID:', error.requestId);​
console.log('Response Status Code:', error.statusCode);​
console.log('Response Header:', error.headers);​
console.log('Response Err Code:', error.code);​
console.log('Response Err Msg:', error.message);​
} else {​
console.log('unexpected exception, message: ', error);​
}​
}​
​
const bucketName = 'node-sdk-test-bucket';​
async function main() {​
try {​
const { requestId: deleteCustomDomainRequestId } = await client.deleteBucketCustomDomain({​
bucket: bucketName,​
// customDomain is required, set the custom domain name to be deleted.​
customDomain: 'Provide your custom domain',​
});​
console.log('deleteBucketCustomDomain Request ID: ', deleteCustomDomainRequestId);​
} catch (error) {​
handleError(error);​
}​
}​
​
main();​
​
​
Last updated: 2025.09.22 18:35:45