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. Before you can use the TimeSeries model, you need to initialize the TimeseriesClient.
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:
Log on to the Tablestore console.
Click the name of the instance to go to the Instance Management page.
On the Instance Details tab, you can view the endpoints of the instance in the Instance Access URL section.
For more information, see Endpoints.
Configure an AccessKey pair
Before connecting to Tablestore, you need a valid AccessKey pair to verify your identity. Tablestore supports the following types of AccessKey pairs:
To prevent security risks caused by the leakage of the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user that is granted the permissions to access Tablestore and use the AccessKey pair of the RAM user to access Tablestore.
The AccessKey pair of an Alibaba Cloud account. To obtain the AccessKey pair of an Alibaba Cloud account, perform the following steps:
On the Alibaba Cloud official website, sign in to Alibaba Cloud.
Create an AccessKey pair that consists of 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 the AccessKey pair of a RAM user, perform the following steps:
Log on to the RAM console by using an Alibaba Cloud account. Then, create a RAM user or find an existing RAM user.
Grant the RAM user the permissions to access Tablestore.
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). The following process shows how to obtain temporary access credentials from STS:
The application server uses RAM or STS to obtain temporary access credentials and send them to you. The access credentials consist of a temporary AccessKey ID, a temporary AccessKey secret, and a security token.
You can use the access credentials to access Tablestore.
Configure environment variables
Configure environment variables based on your operating system.
The OTS_AK_ENV environment variable indicates the AccessKey ID of an Alibaba Cloud account or a RAM user. The OTS_SK_ENV environment variable indicates the AccessKey secret of an Alibaba Cloud account or a RAM user. Specify the AccessKey pair based on your requirements.
Configure environment variables in Linux and macOS
Run the following command to configure environment variables. 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 and the OTS_SK_ENV environment variable to the AccessKey Secret. Then, restart Windows for the configuration to take effect.
Initialize the TableStoreClient
After you obtain the AccessKey ID and AccessKey secret, you can initialize the TableStoreClient. The following sample code shows how to initialize the TableStoreClient.
Operation
// Initialize a TableStoreClient instance. // endPoint specifies the endpoint that is used to access the Tablestore instance and must start with https:// or http://. Example: https://instance.cn-hangzhou.ots.aliyun.com:80. // accessKeyId specifies 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 specifies 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 specifies the name of the 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
Example
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 the TimeseriesClient
After you obtain the AccessKey ID and AccessKey secret, you can perform the following steps to initialize the TimeseriesClient:
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 save the AccessKey ID and AccessKey Secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources that belong to your account may be compromised. In this example, the AccessKey pair is specified in the environment variables, thus identifies verification.
Operation
// Initialize a TimeseriesClient instance. // endPoint specifies the endpoint that is used to access the Tablestore instance and must start with https:// or http://. Example: https://instance.cn-hangzhou.ots.aliyun.com:80. // accessKeyId specifies 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 specifies 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 specifies the name of the 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
Example
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)