A Tablestore client is a client for Tablestore. A Tablestore client provides various methods that you can use 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 a Tablestore client instance and modify the default configurations of ClientConfig based on your business requirements.
Preparations
Before you initialize a Tablestore client, you must obtain an endpoint of the Tablestore instance that you want to access, install Tablestore SDK for Node.js, and configure access credentials.
Obtain an endpoint of the Tablestore instance
Install Tablestore SDK for Node.js
Configure access credentials
Initialize a Tablestore client
To use Tablestore SDK for Node.js, you must first create a Tablestore client. Then, you can call the operations of the client to access Tablestore.
Examples
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. 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 security token and AccessKey pair are saved to 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 the 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 Tablestore instance that you want to access. For more information, see Instances. |
maxRetries | 20 | The maximum number of retries allowed when an error occurs. |
Use Security Token Service
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 the 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 Tablestore instance that you want to access. For more information, see Instances. |