All Products
Search
Document Center

Tablestore:Initialize an OTSClient instance

Last Updated:Sep 26, 2024

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

After you create a Tablestore instance, you must obtain an endpoint of the instance. Then, you can use the endpoint to access the instance.

An endpoint is a domain name that is used to access a Tablestore instance in a region. For example, https://sun.cn-hangzhou.ots.aliyuncs.com is the public endpoint that is used to access the instance named sun in the China (Hangzhou) region over HTTPS. For more information, see Endpoints.

  1. Activate the Tablestore service if it is not activated. For more information, see Activate Tablestore.

  2. Create a Tablestore instance. For more information, see Create an instance.

  3. Obtain an endpoint of the created instance.

    1. Log on to the Tablestore console.

    2. On the Overview page, find the instance that you created and click the name of the instance.

    3. In the Instance Access URL section of the Instance Details tab, view the endpoints of the instance.

      image

Install Tablestore SDK for Node.js

For more information, see Installation.

Configure access credentials

To access Tablestore, you must have a valid AccessKey pair to verify your identity. For more information, see 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

Warning

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

Note

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

Note

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?