All Products
Search
Document Center

Tablestore:Initialize an OTSClient instance

Last Updated:Sep 26, 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 obtain an endpoint of a Tablestore instance, install Tablestore SDK for Go, 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 Go

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

API operations

// 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. 
// 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. 
// 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. 
// securityToken: the STS token that is used to access the Tablestore instance. You can use Alibaba Cloud STS to obtain an STS token. The STS token has a validity period. You must obtain a new token when the existing token expires. 
// config: the custom configuration items of Tablestore. You can specify custom configuration items based on your business requirements. 

func NewClient(endPoint, instanceName, accessKeyId, accessKeySecret string, options ...ClientOption) *TableStoreClient             

func NewClientWithConfig(endPoint, instanceName, accessKeyId, accessKeySecret string, securityToken string, config *TableStoreConfig, options ...ClientOption) *TableStoreClient 

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.

endpoint := "yourEndpoint"
instanceName := "yourInstance"
accessKeyId := os.Getenv("OTS_AK_ENV")
accessKeySecret := os.Getenv("OTS_SK_ENV")

//initialize tableStore client
client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)

//do something

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.

endpoint := "yourEndpoint"
instanceName := "yourInstance"
accessKeyId := os.Getenv("OTS_AK_ENV")
accessKeySecret := os.Getenv("OTS_SK_ENV")
securityToken := os.Getenv("OTS_SESSION_TOKEN")
config := tablestore.NewDefaultTableStoreConfig();

//initialize tableStore client
client := tablestore.NewClientWithConfig(endpoint, instanceName, accessKeyId, accessKeySecret, securityToken, config)

//do something

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.

API operations

// 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. 
// 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. 
// securityToken: the STS token that is used to access the Tablestore instance. You can use Alibaba Cloud STS to obtain an STS token. The STS token has a validity period. You must obtain a new token when the existing token expires. 
// config: the custom configuration items of Tablestore. You can specify custom configuration items based on your business requirements. 

func NewTimeseriesClient(endPoint, instanceName, accessKeyId, accessKeySecret string , options ...TimeseriesClientOption) *TimeseriesClient

func NewTimeseriesClientWithConfig(endPoint, instanceName, accessKeyId, accessKeySecret string, securityToken string, config *TableStoreConfig, timeseriesConfiguration *TimeseriesConfiguration, options ...TimeseriesClientOption) *TimeseriesClient

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.

endpoint := "yourEndpoint"
instanceName := "yourInstance"
accessKeyId := os.Getenv("OTS_AK_ENV")
accessKeySecret := os.Getenv("OTS_SK_ENV")

//initialize tableStore client
client := tablestore.NewTimeseriesClient(endpoint, instanceName, accessKeyId, accessKeySecret)

//do something

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.

endpoint := "yourEndpoint"
instanceName := "yourInstance"
accessKeyId := os.Getenv("OTS_AK_ENV")
accessKeySecret := os.Getenv("OTS_SK_ENV")
securityToken := os.Getenv("OTS_SESSION_TOKEN")
config := tablestore.NewDefaultTableStoreConfig();
timeseriesConfig := tablestore.	NewTimeseriesConfiguration();

//initialize tableStore client
client := tablestore.NewTimeseriesClientWithConfig(endpoint, instanceName, accessKeyId, accessKeySecret, securityToken, config, timeseriesConfig)

//do something

FAQ

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