All Products
Search
Document Center

Tablestore:Initialize an OTSClient instance

最終更新日:Mar 12, 2024

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

Usage notes

  • HTTPS is supported in Tablestore SDK for Python V2.0.8 and later. If you want to access Tablestore resources over HTTPS, use Tablestore SDK for Python V2.0.8 or later, and make sure that the OpenSSL version is at least 0.9.8j. We recommend that you use OpenSSL 1.0.2d.

    The release package for Tablestore SDK for Python V2.0.8 contains the certifi package that can be directly installed and used. To update the root certificate, download the latest root certificate from the official website of certifi.

  • Before you run the code, you must import the Tablestore package.

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

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 Python

Initialize an OTSClient instance

To use Tablestore SDK for Python, you must first create an OTSClient instance. Then, you can call the methods of the OTSClient 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.

Syntax

"""
Initialize an OTSClient instance. 
end_point: 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: 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: 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: 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. 
sts_token: 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: the method that is used to encode the request parameter string. Default value: utf8. 
socket_timeout: the timeout period in seconds for each socket connection in the connection pool. The value can be an integer or a floating-point number. Default value: 50. 
max_connection: the maximum number of connections in a connection pool. Default value: 50. 
logger_name: the name of the log file that records the DEBUG log in the request or the ERROR log when an error occurs. 
retry_policy: 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. 
ssl_version: the transport layer security (TLS) version that is used for HTTPS connections. Default value: None. 
 """
 class OTSClient(object):
    def __init__(self, endpoint, access_key_id, access_key_secret, instance_name, **kwargs): 
      

Example

access_key_id = os.getenv("OTS_AK_ENV")
access_key_secret = os.getenv("OTS_SK_ENV")

#########    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. 
ots_client = OTSClient('endpoint', access_key_id, access_key_secret, 'instance_name', logger_name = 'table_store.log',  retry_policy = WriteRetryPolicy())

#########    Set the TLS version that is used for HTTPS connections to TLS 1.2.     #########
# Note: The specified TLS version takes effect only when you use an HTTPS endpoint. 
ots_client = OTSClient('endpoint', access_key_id, access_key_secret, 'instance_name', ssl_version=ssl.PROTOCOL_TLSv1_2)

#########    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 message appears. In this case, you must import the Tablestore package before you 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 from environment variables. 
import os
# You must import the Tablestore package when you access the Tablestore instance by using the specified TLS version. 
import ssl

FAQ

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