All Products
Search
Document Center

Tablestore:Initialization

Last Updated:Aug 02, 2023

Client is the client for Tablestore. Client provides a series of methods for you to manage tables and perform read and write operations on one or more rows.

Obtain an endpoint

An endpoint is a domain name that is used to access a Tablestore instance in a region. To query an endpoint, perform the following steps:

  1. Log on to the Tablestore console.
  2. Click the name of the instance to go to the Instance Details tab.
    On the Instance Details tab, you can view the endpoints of the instance in the Instance Access URL section.
Note For more information about endpoints, see Endpoints.

Configure an AccessKey pair

To access Tablestore, you need to have an AccessKey pair that consists of an AccessKey ID and AccessKey secret to verify your identity. Tablestore supports the following types of AccessKey pairs:

  • The AccessKey pair of an Alibaba Cloud account. To obtain an AccessKey pair, perform the following steps:

    1. Sign up to Alibaba Cloud on the Alibaba Cloud official website.

    2. Create an AccessKey ID and an AccessKey secret. For more information, see Obtain an AccessKey pair.

  • The AccessKey pair of a RAM user that is authorized to access Tablestore. To obtain an AccessKey pair, perform the following steps:

    1. Log on to the RAM console by using an Alibaba Cloud account. Then, create a RAM user or find an existing RAM user.

    2. Grant the RAM user the permissions to access Tablestore.

    3. After the RAM user is granted the permissions to access Tablestore, you can use the AccessKey pair of the RAM user to access Tablestore.

  • Temporary access credentials that are obtained from Security Token Service (STS). To obtain temporary access credentials, perform the following steps:

    1. The application server uses RAM or STS to obtain access credentials that consist of a temporary AccessKey ID, an AccessKey secret, and a token. After the access credentials are obtained, the application server sends them to you.

    2. You can use the access credentials to access Tablestore.

Configure environment variables

Configure environment variables based on the operating system.

Tablestore use the OTS_AK_ENV environment variable to indicate the AccessKey ID of an Alibaba Cloud account or a RAM user and the OTS_SK_ENV environment variable to indicate the AccessKey secret of an Alibaba Cloud account or a RAM user. Specify the AccessKey pair based on your business requirements.

  • Configure environment variables in Linux and macOS

    Run the following command to configure environment variables: In the preceding command, replace<access_key_id> with your actual AccessKey ID and <access_key_secret> with your actual AccessKey secret.

    export OTS_AK_ENV=<access_key_id>
    export OTS_SK_ENV=<access_key_secret>
  • Configure environment variables in Windows

    Create an environment variable file and add the OTS_AK_ENV and OTS_SK_ENV environment variables to the file. Then, set the OTS_AK_ENV environment variable to the AccessKey ID that you prepared and the OTS_SK_ENV environment variable to the AccessKey secret that you prepared. Then, restart the Windows operating system for the configuration to take effect.

Initialize an OTSClient instance

After you obtain the AccessKey ID and AccessKey secret, you can perform the following operations to initialize a Tablestore client instance:

Important Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account because the 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 save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources that belong to your account may be compromised. In this example, an AccessKey pair that is specified by using environment variables is used for identify verification to describe how to initialize a client.
var OTS_AK_ENV = process.env.OTS_AK_ENV;
var OTS_SK_ENV = process.env.OTS_SK_ENV;
var client = new TableStore.Client({
    accessKeyId: OTS_AK_ENV,
    accessKeySecret: OTS_SK_ENV,
    endpoint: '<your endpoint>',
    instancename: '<your instance name>',
    maxRetries:20,// The default number of retry attempts is 20. You can ignore this parameter. 
});