OTSClient is a client for Tablestore. OTSClient provides various methods for you to manage tables and perform read and write operations on a single row or multiple rows. To use Tablestore SDK for Node.js to initiate a request, you must initialize an OTSClient instance and modify the default configurations of the OTSClient instance based on your business requirements.
Preparations
Before you initialize an OTSClient instance, you must obtain an endpoint of a Tablestore instance, install Tablestore SDK for Node.js, and then configure access credentials.
Obtain the endpoint of a Tablestore instance
Install Tablestore SDK for Node.js
Configure access credentials
Initialize an OTSClient instance
To use Tablestore SDK for Node.js, you must first create an OTSClient instance. Then, you can call the operations of the OTSClient instance to access Tablestore.
Examples
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. We recommend that you do not hard-code the AccessKey ID and AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. In the examples, the AccessKey pair and security token are saved in environment variables.
Use an AccessKey pair
Before you run the sample code, make sure that the OTS_AK_ENV
and OTS_SK_ENV
environment variables are configured. For more information, see Configure access credentials.
var accessKeyId = process.env.OTS_AK_ENV;
var secretAccessKey = process.env.OTS_SK_ENV;
var endpoint = 'yourEndpoint';
var instancename = 'yourInstance';
var client = new TableStore.Client({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
endpoint: endpoint,
instancename: instancename,
maxRetries:20,// The maximum number of retries. Default value: 20. You can leave this parameter empty.
});
The following table describes the parameters.
Parameter | Example | Description |
endpoint | https://myinstance.cn-hangzhou.ots.aliyuncs.com | The endpoint that is used to access the Tablestore instance. For more information, see Obtain an endpoint of a Tablestore instance. |
accessKeyId | process.env.OTS_AK_ENV | The AccessKey pair that is obtained from environment variables. Make sure that the environment variables are configured. |
secretAccessKey | process.env.OTS_SK_ENV | |
instancename | myinstance | The name of the instance. For more information, see Instances. |
maxRetries | 20 | The maximum number of retries allowed when an error occurs. |
Use STS
Before you run the sample code, make sure that the OTS_AK_ENV
, OTS_SK_ENV
, and OTS_SESSION_TOKEN
environment variables are configured. For more information, see Configure access credentials.
var accessKeyId = process.env.OTS_AK_ENV;
var secretAccessKey = process.env.OTS_SK_ENV;
var stsToken = process.env.OTS_SESSION_TOKEN;
var endpoint = 'yourEndpoint';
var instancename = 'yourInstance';
var client = new TableStore.Client({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
stsToken: stsToken,
endpoint: endpoint,
instancename: instancename,
});
The following table describes the parameters.
Parameter | Example | Description |
endpoint | https://myinstance.cn-hangzhou.ots.aliyuncs.com | The endpoint that is used to access the Tablestore instance. For more information, see Obtain an endpoint of a Tablestore instance. |
accessKeyId | process.env.OTS_AK_ENV | The AccessKey pair and security token that are obtained from environment variables. Make sure that the environment variables are configured. |
accessKeySecret | process.env.OTS_SK_ENV | |
stsToken | process.env.OTS_SESSION_TOKEN | |
instancename | myinstance | The name of the instance. For more information, see Instances. |
FAQ
What do I do if the Signature mismatch error is reported when I use Tablestore SDKs?