All Products
Search
Document Center

Tablestore:Initialize an OTSClient instance

Last Updated:Sep 26, 2024

OTSClient is a client for Tablestore. OTSClient provides various methods that you can use to manage tables and perform read and write operations on a single row or multiple rows. To use Tablestore SDK for .NET to initiate a request, you must initialize an OTSClient instance and modify the default configurations in OTSClientConfig based on your business requirements.

Usage notes

Tablestore SDK for .NET supports multithreading. We recommend that you use the same OTSClient object to run multiple threads of a task.

Preparations

Before you initialize an OTSClient instance, you must obtain an endpoint of a Tablestore instance, install Tablestore SDK for .NET, and then configure access credentials.

Obtain an 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 .NET

For more information, see Install Tablestore SDK for .NET.

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 an OTSClient instance

To use Tablestore SDK for .NET, you must first create an OTSClient instance. Then, you can call the operations of the OTSClient instance to access Tablestore.

API operations

/// <summary>
/// The function used to create an OTSClient instance. 
/// </summary>
/// <param name="endPoint">The endpoint of the Tablestore instance, which must start with https://. Example: https://instance.cn-hangzhou.ots.aliyun.com:80 </param>
/// <param name="accessKeyID"> The AccessKey ID that is used to access the Tablestore instance. You can obtain an AccessKey ID on the Alibaba Cloud official website. </param>
/// <param name="accessKeySecret"> The AccessKey secret that is used to access the Tablestore instance. You can obtain an AccessKey secret on the Alibaba Cloud official website. </param>
/// <param name="instanceName"> The name of the Tablestore instance. You can obtain the instance name in the Tablestore console. </param>
public OTSClient(string endPoint, string accessKeyID, string accessKeySecret, string instanceName);

/// <summary>
/// Create an OTSClient instance by configuring the instance of the OTSClientConfig class on the client. 
/// </summary>
/// <param name="config">The client configurations for the instance.</param>
public OTSClient(OTSClientConfig config);                   

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 security token and AccessKey pair are saved in environment variables.

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.

// Create an OTSClientConfig object. 
public static string Endpoint = "yourEndpoint";
public static string InstanceName = "yourInstance";
public static string AccessKeyId = Environment.GetEnvironmentVariable("OTS_AK_ENV");
public static string AccessKeySecret = Environment.GetEnvironmentVariable("OTS_SK_ENV");
var config = new OTSClientConfig(Endpoint, AccessKeyId, AccessKeySecret, InstanceName);

// Disable logging. By default, logging is enabled. 
config.OTSDebugLogHandler = null;
config.OTSErrorLogHandler = null;

// Create an OtsClient object by using OTSClientConfig. 
var otsClient = new OTSClient(config);

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, see Obtain an endpoint of a Tablestore instance.

AccessKeyId

Environment.GetEnvironmentVariable("OTS_AK_ENV")

The AccessKey pair that is obtained from environment variables. Make sure that the environment variables are configured.

AccessKeySecret

Environment.GetEnvironmentVariable("OTS_SK_ENV")

InstanceName

myinstance

The name of the instance. For more information, see Instances.

APIVersion

2015-12-31

The version of the Tablestore protocol. Default value: 2015-12-31.

ConnectionLimit

300

The maximum number of connections to Tablestore. Default value: 300.

RetryPolicy

DefaultRetryPolicy

The retry policy. Default value: DefaultRetryPolicy. You can also specify a custom retry policy.

To disable the retry policy, set this parameter to null.

OTSDebugLogHandler

null

The function to process debug logs, which is used to display the logs of normal requests and responses. Default value: defaultOTSDebugLogHandler.

To disable the function, set this parameter to null.

OTSErrorLogHandler

null

The function to process error logs, which is used to display the logs of errors returned from the Tablestore server. Default value: defaultOTSErrorLogHandler.

To disable the function, set this parameter to null.

FAQ

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