All Products
Search
Document Center

Tablestore:Initialize an OTSClient instance

Last Updated:Mar 12, 2024

TableStoreClient is a client for Tablestore. TableStoreClient provides various methods for you to manage tables and perform read and write operations on a single row or multiple rows. If you want to manage tables and perform read and write operations on a single row or multiple rows in the Wide Column model, you need to initialize a TableStoreClient instance. If you want to manage time series tables, query time series, and read and write time series data in the TimeSeries model, you need to initialize a TimeseriesClient instance.

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 Go.

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.

  1. 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

    1. Create an Alibaba Cloud account on the Alibaba Cloud official website.

    2. 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

    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. 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.

    3. 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)

    1. 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.

    2. Use the temporary access credentials to access Tablestore.

  2. 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.

    • Configure environment variables in Linux and macOS

      Run the following commands to configure environment variables. Replace <ACCESS_KEY_ID> with your AccessKey ID and <ACCESS_KEY_SECRET> with your AccessKey secret.

      export OTS_AK_ENV=<ACCESS_KEY_ID>
      export OTS_SK_ENV=<ACCESS_KEY_SECRET>
    • Configure environment variables in Windows

      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.

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.

  1. If the Tablestore service is not activated, activate the service. For more information, see Step 1: Activate Tablestore.

  2. Create a Tablestore instance. For more information, see Step 2: 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. On the Instance Details tab, view the endpoints of the instance in the Instance Access URL section.

      fig_endpoint

Install Tablestore SDK for Go

For more information, see Installation.

Initialize a TableStoreClient instance

If you want to use Tablestore SDK for Go to manage tables in the Wide Column model, you must create a TableStoreClient instance and call the operations of the TableStoreClient 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 identify.

API operation

// Initialize a TableStoreClient instance. 
// endPoint: the endpoint that is used to access the Tablestore instance. The endpoint must start with https:// or http://. Example: https://instance.cn-hangzhou.ots.aliyun.com:80. 
// accessKeyId: the AccessKey ID that is used to access the Tablestore instance. To obtain an AccessKey ID, you can visit the official website of Alibaba Cloud or contact an administrator. 
// accessKeySecret: the AccessKey secret that is used to access the Tablestore instance. To obtain an AccessKey secret, you can visit the official website of Alibaba Cloud or contact an administrator. 
// instanceName: the name of the Tablestore instance that you want to access. You can create an instance in the Tablestore console or contact an administrator to obtain the name of an existing instance. 
func NewClient(endPoint, instanceName, accessKeyId, accessKeySecret string, options ...ClientOption) *TableStoreClient                   

Sample code

access_key_id := os.Getenv("OTS_AK_ENV")
access_key_secret := os.Getenv("OTS_SK_ENV")
client := tablestore.NewClient("your_instance_endpoint",  "your_instance_name", access_key_id, access_key_secret)          

Initialize a TimeseriesClient instance

If you want to use Tablestore SDK for Go to manage tables in the TimeSeries model, you must create a TimeseriesClient instance and call the operations of the TimeseriesClient instance to access Tablestore. The TimeseriesClient instance must be separately initialized for the TimeSeries model.

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 identify.

API operation

// Initialize a TimeseriesClient instance. 
// endPoint: the endpoint that is used to access the Tablestore instance. The endpoint must start with https:// or http://. Example: https://instance.cn-hangzhou.ots.aliyun.com:80. 
// accessKeyId: the AccessKey ID that is used to access the Tablestore instance. To obtain an AccessKey ID, you can visit the official website of Alibaba Cloud or contact an administrator. 
// accessKeySecret: the AccessKey secret that is used to access the Tablestore instance. To obtain an AccessKey secret, you can visit the official website of Alibaba Cloud or contact an administrator. 
// instanceName: the name of the Tablestore instance that you want to access. You can create an instance in the Tablestore console or contact an administrator to obtain the name of an existing instance. 
func NewTimeseriesClient(endPoint, instanceName, accessKeyId, accessKeySecret string , options ...TimeseriesClientOption) *TimeseriesClient

Sample code

access_key_id := os.Getenv("OTS_AK_ENV")
access_key_secret := os.Getenv("OTS_SK_ENV")
timeseriesClient := tablestore.NewTimeseriesClient("your_instance_endpoint",  "your_instance_name", access_key_id, access_key_secret)

FAQ

What do I do if the Signature mismatch error is reported when I use Tablestore SDKs?