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.

Obtain an endpoint

An endpoint is a domain name that is used to access a Tablestore instance in a region. The following table describes the examples of endpoints in the supported formats.

ExampleDescription
http://sun.cn-hangzhou.ots.aliyuncs.comThe public endpoint that is used to access the sun instance in the China (Hangzhou) region over HTTP.
https://sun.cn-hangzhou.ots.aliyuncs.comThe public endpoint that is used to access the sun instance in the China (Hangzhou) region over HTTPS.
Important You can connect to Tablestore by using public endpoints or private endpoints. For more information, see Endpoints.

To query the endpoints of your Tablestore instance, perform the following steps:

  1. Log on to the Tablestore console.
  2. On the Overview page, click the name of an instance.
  3. In the Instance Access URL section of the Instance Details tab, view the endpoints of the instance.
    fig_endpoint

Configure an AccessKey pair

To access Tablestore, you must have an AccessKey pair that consists of an AccessKey ID and AccessKey secret to verify your identity. The following types of AccessKey pairs are supported:

  • The AccessKey pair of an Alibaba Cloud account. To obtain the AccessKey pair of an Alibaba Cloud account, take the following steps:
    1. On the Alibaba Cloud official website,Create Your Alibaba Cloud Account.
    2. Create an AccessKey ID and an AccessKey secret. For more information, see Obtain an AccessKey pair.
  • The AccessKey pair of a RAM user who is granted with permissions to access Tablestore. To obtain the AccessKey pair of a RAM user, take the following steps:
    1. Use your Alibaba Cloud account to log on to RAM. Create a RAM user or use an existing RAM user.
    2. Use your Alibaba Cloud account to authorize the RAM user to access Tablestore.
    3. After the RAM user is authorized, you can use the AccessKey pair of the RAM user to access Tablestore.
  • Temporary access credentials obtained from STS. To obtain temporary access credentials from STS, take the following steps:
    1. The application server uses RAM or STS to obtain access credentials that consist of a temporary AccessKey ID, an AccessKey secret, and a token. After the access credentials are obtained, the application server sends them to you.
    2. You can use the received access credentials to access Tablestore.

Configure environment variables

Configure environment variables based on the operating system.

Tablestore use the OTS_AK_ENV environment variable to indicate the AccessKey ID of an Alibaba Cloud account or a RAM user and the OTS_SK_ENV environment variable to indicate the AccessKey secret of an Alibaba Cloud account or a RAM user. Specify the AccessKey pair based on your business requirements.

  • Configure environment variables in Linux and macOS

    Run the following command to configure environment variables: In the preceding command, 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 that you prepared and the OTS_SK_ENV environment variable to the AccessKey secret that you prepared. Then, restart the Windows operating system for the configuration to take effect.

Initialize an OTSClient instance

Important Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account because the 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 resources that belong to your account may be compromised. In this example, an AccessKey pair that is specified by using environment variables is used for identify verification to describe how to initialize a client.
  • Operations
    """
    Initialize an OTSClient instance. 
    end_point specifies the endpoint that is used to access the Tablestore instance. The endpoint must start with https://. Example: https://instance.cn-hangzhou.ots.aliyun.com:80 
    access_key_id specifies the AccessKey ID that is used to access the Tablestore instance. You can visit the Alibaba Cloud official website or contact an administrator to obtain an AccessKey ID. 
    access_key_secret specifies the AccessKey secret that is used to access the Tablestore instance. You can visit the Alibaba Cloud official website or contact an administrator to obtain an AccessKey secret. 
    instance_name 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. 
    sts_token specifies 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 if the existing token expires. 
    encoding specifies the method that is used to encode the request parameter string. The default value is utf8. 
    socket_timeout specifies the timeout period in seconds for each socket connection in the connection pool. It can be an integer or a float number. Default value: 50. 
    max_connection specifies the maximum number of connections in a connection pool. Default value: 50. 
    logger_name is used to display the DEBUG log in the request or display the ERROR log when an error occurs. 
    retry_policy defines the retry policy. The default retry policy is DefaultRetryPolicy. You can define a retry policy based on a RetryPolicy class. For more information, see the code in DefaultRetryPolicy. 
     """
     class OTSClient(object):
        def __init__(self, endpoint, access_key_id, access_key_secret, instance_name, **kwargs):                   
  • Examples
    #########    Specify the name of the log file and the retry policy.     #########
    # The name of the log file is table_store.log. The retry policy is WriteRetryPolicy, which defines the retry attempts to write data to the table when a write operation fails. 
    access_key_id = os.getenv("OTS_AK_ENV")
    access_key_secret = os.getenv("OTS_SK_ENV")
    ots_client = OTSClient('endpoint', access_key_id, access_key_secret, 'instance_name', logger_name = 'table_store.log',  retry_policy = WriteRetryPolicy())
    
    #########    Use STS     #########
    ots_client = OTSClient('endpoint', 'STS.K8h*******GB77', 'CkuDj******Wn6', 'instance_name', sts_token = 'CAISjgJ1q6Ft5B2y********OFcsLLuw==')                    

Import the Tablestore package

Note If you do not import the Tablestore package, the NameError: name 'OTSClient' is not defined error message appears. After you import the Tablestore package, you can run the code. For more information, see What are the examples of code used to perform ListTable in Tablestore SDK for Python?.
When you use Tablestore SDK for Python to invoke functions, you must import the Tablestore package to the code. You can run the following code to import the Tablestore package:
# Import the Tablestore package. 
from tablestore import *
# You must import the Tablestore package when you obtain the AccessKey pair by using environment variables. 
import os

HTTPS

  • HTTPS is supported in Tablestore SDK for Python V2.0.8 or later.
  • OpenSSL 0.9.8j or later is required. We recommend that you use OpenSSL 1.0.2d.
  • The Tablestore SDK for Python V2.0.8 release package contains a certifi package that can be installed and used. To update the root certificate, download the latest root certificate from Root Certificate.