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

Torch Object Storage

Copy page
Download PDF
Node.js-Vectors
Initialize client (TOS Vectors Node.js SDK)
Copy page
Download PDF
Initialize client (TOS Vectors Node.js SDK)
After initializing the TosVectorsClient instance, you can send HTTP/HTTPS requests to the TOS Vectors service. This article describes how to initialize the Node.js SDK.​
The TOS Vectors Node.js client provides a set of interfaces to interact with the TOS Vectors service and manage resources such as buckets and vectors. When initializing the client, you must provide the AccessKey ID, AccessKey Secret, and Region information. During initialization, you can set optional parameters such as connection timeout, retry count on timeout, and maximum idle connections.​
Prerequisites​
Configure long-term access credentials​
Before initializing the SDK, you need to configure long-term access credentials. This chapter explains how to configure long-term access credentials on different operating systems.​
​
macOS
Linux
Windows
​
  1. Open Terminal and run this command to open the file.​
​
nano ~/.bash_profile​
​
  1. Add your access key ID and secret access key at the end of the file.​
​
export TOS_ACCESS_KEY=AKTPYmI1Z****​
export TOS_SECRET_KEY=T1dJM01UU****​
​
  1. Save the file and exit.​
  1. Run this command to apply the configuration.​
​
source ~/.bash_profile​
​
  1. Run this command to verify the configuration.​
​
echo $TOS_ACCESS_KEY​
echo $TOS_SECRET_KEY​
​
The following output confirms a successful configuration.​
​
AKTPYmI1Z****​
T1dJM01UU****​
​
​
Create TosVectorsClient​
Initialize with required parameters​
The following code shows how to initialize TosVectorsClient using required TOS parameters, including AccessKey ID, AccessKey Secret, Endpoint, and Region.​
tip​
​
import { TosVectorsClient } from '@volcengine/tos-sdk';​
​
// Create client​
const client = new TosVectorsClient({​
accessKeyId: process.env['TOS_ACCESS_KEY'], ​
accessKeySecret: process.env['TOS_SECRET_KEY'], ​
region: "Provide your region", // Specify the region where the bucket is located. For example, if your bucket is in China (Beijing), replace "Provide your region" with "cn-beijing".​
endpoint: "Provide your endpoint", // Enter the domain address​
});​
​
Initialize with optional parameters​
The TOS Vectors Node.js SDK provides various optional parameters to customize the TosVectorsClient configuration, such as setting STS, HTTP request timeout, request retry policies, and more.​
The following code demonstrates how to customize the HTTP connection timeout for TosVectorsClient. See the timeout configuration section below for specific scenarios.​
​
import { TosVectorsClient } from '@volcengine/tos-sdk';​
​
const connectionTimeout = 10000;​
​
// Create client​
const client = new TosVectorsClient({​
accessKeyId: process.env['TOS_ACCESS_KEY'], ​
accessKeySecret: process.env['TOS_SECRET_KEY'], ​
region: "Provide your region", // Specify the region where the bucket is located. For example, for North China 2 (Beijing), enter \"cn-beijing\" as the region.​
endpoint: "Provide your endpoint", // Enter the domain address​
connectionTimeout,​
});​
​
console.log(client);​
​
Common configuration scenarios​
Configure timeout settings​
You can configure the network request timeout by adding optional parameters when initializing TosVectorsClient. The TOS Vectors Node.js SDK currently provides the following timeout parameters for HTTP request timeout configuration:​
  • requestTimeout: HTTP request timeout in milliseconds, with a default value of 120,000 (120 seconds).​
  • connectionTimeout: timeout for establishing an HTTP connection in milliseconds, default is 10,000 (10 seconds).​
  • The following code shows how to configure the timeout when initializing TosVectorsClient.​
​
// Import SDK. When the TOS Node.JS SDK version is below 2.5.2, change TosVectorsClient below to TOS import​
import { TosVectorsClient } from '@volcengine/tos-sdk';​
​
const connectionTimeout = 10000;​
const requestTimeout = 120000;​
​
// Create client​
const client = new TosVectorsClient({​
accessKeyId: process.env['TOS_ACCESS_KEY'], ​
accessKeySecret: process.env['TOS_SECRET_KEY'], ​
region: "Provide your region", // Specify the region where the bucket is located. For example, if the region is China North 2 (Beijing), enter "cn-beijing" for "Provide your region".​
endpoint: "Provide your endpoint",// Specify the domain address​
connectionTimeout,​
requestTimeout,​
});​
​
console.log(client);​
​
Configure retry strategy​
Network fluctuations or short-term TOS Vectors service errors (such as returning a 500 HTTP status code) may cause requests to the TOS service to fail. To address this, the SDK includes a retry mechanism with exponential backoff based on the configured number of retries. The default retry count is 3.​
Idempotent operation interfaces retry using exponential backoff upon failure; non-idempotent interfaces throw the corresponding exception immediately. You can add optional parameters to configure the number of retries when initializing the TosVectorsClient.​
The following code shows how to set the SDK retry count.​
​
// Import SDK; if the TOS Node.js SDK version is below 2.5.2, change TosVectorsClient below to TOS import​
import { TosVectorsClient } from '@volcengine/tos-sdk';​
// Set the maximum retry count to 3; adjust as needed​
const maxRetryCount = 3;​
​
// Create client​
const client = new TosVectorsClient({​
accessKeyId: process.env['TOS_ACCESS_KEY'], ​
accessKeySecret: process.env['TOS_SECRET_KEY'], ​
region: "Provide your region", // Specify the region where the bucket is located.; For example, if your bucket is in China (Beijing), replace "Provide your region" with "cn-beijing".;​
endpoint: "Provide your endpoint", // Enter domain address;​
maxRetryCount,​
});​
​
Initialization parameter list​
​
Parameter​
Type​
Required​
Example value​
Description​
accessKeyId​
String​
Yes​
AKTPYmI1Z****​
Access Key ID, Secret Key ID, default valuenull ​
accessKeySecret​
String​
Yes​
T1dJM01UU****==​
AccessKey Secret, private access key, default valuenull ​
region​
String​
Yes​
cn-beijing​
Region of the TOS server, default valuenull ​
endpoint​
String​
Optional. If empty, determined by the region parameter​
tosvectors-cn-beijing.volces.com​
access domain name, determined by the region parameter if left empty​
stsToken​
String​
No​
STSkeyJBY2NvdW50SW************​
Security token in temporary access credentials, default valuenull ​
enableVerifySSL​
boolean​
No​
true​
Specifies whether to enable SSL certificate verification. The default value is False.​
requestTimeout​
number​
No​
120000​
HTTP request timeout in milliseconds, default is 120000.​
connectionTimeout​
number​
No​
10000​
The connection timeout, in milliseconds. The default value is 10000.​
idleConnectionTime​
number​
No​
60000​
If an idle HTTP connection in the connection pool exceeds this timeout, the connection is closed. The unit is milliseconds, and the default value is 60000.​
maxConnections​
number​
No​
1024​
HTTP connection pool max connections, default is 1024.​
maxRetryCount​
number​
No​
3​
The maximum number of retries for a failed request. The default value is 3.​
proxyHost​
String​
No​
localhost​
Proxy server host address, currently only HTTP protocol is supported, default valuenull ​
proxyPort​
number​
No​
8080​
The port number of the proxy server.​
​
​
Last updated: 2026.02.06 18:01:46