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 configure an AccessKey pair, obtain the endpoint of your Tablestore instance, and install Tablestore SDK for Node.js.
Configure an AccessKey pair
To access Tablestore, you must have a valid AccessKey pair to verify your identity. The following table describes three methods that you can use to obtain an AccessKey pair. To ensure the security of your AccessKey pair, we recommend that you configure the AccessKey pair in the environment variables of your operating system.
Obtain an AccessKey pair.
Important To prevent security risks caused by the leak of the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user that has the permissions to access Tablestore and use the AccessKey pair of the RAM user to access Tablestore.
Method | Procedure |
AccessKey pair of an Alibaba Cloud account | Create an Alibaba Cloud account on the Alibaba Cloud official website. Create an AccessKey pair that consists of an AccessKey ID and an AccessKey secret. For more information, see Create an AccessKey pair.
|
AccessKey pair of a RAM user that has the permissions to access Tablestore | Log on to the RAM console with an Alibaba Cloud account. Then, create a RAM user or find an existing RAM user. Use the Alibaba Cloud account to grant access permissions on Tablestore to the RAM user. For more information, see Use a RAM policy to grant permissions to a RAM user. Use the AccessKey pair of the RAM user to access Tablestore. For more information, see Create an AccessKey pair.
|
Temporary access credentials that are obtained from Security Token Service (STS) | Obtain temporary access credentials from the application server. The temporary access credentials consist of a temporary AccessKey ID, a temporary AccessKey secret, and a security token. The application server accesses RAM or STS to obtain the temporary access credentials and returns the temporary access credentials to you. Use the temporary access credentials to access Tablestore.
|
Run the following command to configure environment variables:
Tablestore uses the OTS_AK_ENV and OTS_SK_ENV environment variables to store an AccessKey pair. The OTS_AK_ENV environment variable stores the AccessKey ID of an Alibaba Cloud account or a RAM user. The OTS_SK_ENV environment variable stores the AccessKey secret of an Alibaba Cloud account or a RAM user. Configure the environment variables based on the AccessKey pair that you want to use.
Important After you configure the environment variables, you may need to restart the relevant services or development tools such as Integrated Development Environment (IDE) to ensure that the new settings are applied as expected.
Configure environment variables in Windows
You can configure environment variables by using the GUI, Command Prompt, or Windows PowerShell. For more information, see Configure environment variables in Linux, macOS, and Windows.
Use the GUI
In the System Variable section of the Environment Variable dialog box, add the OTS_AK_ENV and OTS_SK_ENV environment variables, and set the OTS_AK_ENV environment variable to the AccessKey ID and the OTS_SK_ENV environment variable to the AccessKey secret that you obtained. Then, save the configurations.
Use the Command Prompt
Open a Command Prompt window as an administrator and run the following commands to add environment variables in the operating system: In the preceding commands, /M
specifies system variables.
setx OTS_AK_ENV LT******************** /M
setx OTS_SK_ENV Ir**************************** /M
After you configure the environment variables, run the echo %OTS_AK_ENV%
and echo %OTS_SK_ENV%
commands to check whether the environment variables take effect. If the correct AccessKey pair is returned, the environment variables take effect.
Use Windows PowerShell
Start Windows PowerShell as an administrator.
Run the following commands in PowerShell to configure environment variables:
[System.Environment]::SetEnvironmentVariable('OTS_AK_ENV', 'LT********************', [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('OTS_SK_ENV', 'Ir****************************', [System.EnvironmentVariableTarget]::Machine)
Restart PowerShell and run the following commands in PowerShell to check whether the environment variables take effect:
If the correct AccessKey pair is returned, the environment variables take effect.
Get-ChildItem env:OTS_AK_ENV
Get-ChildItem env:OTS_SK_ENV
Configure environment variables in Linux and macOS
Note The first time you configure environment variables, you can use the touch ~/.bash_profile
command to create a configuration file. If a configuration file already exists, you can use the vim ~/.bash_profile
command to edit the file.
Create a file named .bash_profile
.
touch ~/.bash_profile
vim ~/.bash_profile
Configure environment variables in the .bash_profile
file. Then, save the settings and exit the configuration file.
# In the following command, replace <ACCESS_KEY_ID> with the AccessKey ID you obtained and <ACCESS_KEY_SECRET> with the AccessKey secret you obtained.
export OTS_AK_ENV=<ACCESS_KEY_ID>
export OTS_SK_ENV=<ACCESS_KEY_SECRET>
Make the configurations in the .bash_profile
configuration file take effect and check whether the configurations take effect.
# Make the configurations in the configuration file take effect.
source ~/.bash_profile
# Check whether the configurations take effect.
echo $ALIBABA_CLOUD_ACCESS_KEY_ID
If the correct AccessKey ID is returned, the configurations take effect.
Obtain the endpoint of a Tablestore instance
After you create a Tablestore instance, you must obtain an endpoint of the instance. This way, 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.
If the Tablestore service is not activated, activate the service. For more information, see Step 1: Activate Tablestore.
Create a Tablestore instance. For more information, see Step 2: Create an instance.
Obtain an endpoint of the created instance.
Log on to the Tablestore console.
On the Overview page, find the instance that you created and click the name of the instance.
On the Instance Details tab, view the endpoints of the instance in the Instance Access URL section.
Install Tablestore SDK for Node.js
For more information, see Installation.
Initialize an OTSClient instance
To use Tablestore SDK for Node.js, you must first create an OTSClient instance. Then, you can call the methods of the OTSClient instance to access Tablestore.
Important The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of 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 in your account is compromised. In the following examples, the AccessKey pair is configured in the environment variables to verify your identity.
Use the endpoint of a Tablestore instance to create an OTSClient instance.
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 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 about how to obtain the endpoint, see the Obtain the endpoint of a Tablestore instance section of this topic. |
accessKeyId | process.env.OTS_AK_ENV | The AccessKey pair that is used to access the Tablestore instance. Obtain the AccessKey pair by using environment variables. Make sure that the environment variables are configured. For more information, see the Configure an AccessKey pair section of this topic. |
accessKeySecret | process.env.OTS_SK_ENV |
instancename | myinstance | The name of the Tablestore instance that you want to access. For more information, see Instance. |
Configure the client.
To modify the default configurations of the OTSClient instance, import the required parameters such as the maximum number of retries when you create the OTSClient instance. The following table describes the parameters that you can configure when you create the OTSClient instance.
Parameter | Description |
maxRetries | The maximum number of retries allowed when an error occurs. Default value: 20. |