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 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 version of OpenSSL is 0.9.8j or later. 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 you can directly install and use. To update the root certificate, download the latest root certificate from the official website of certifi.
ImportantTablestore SDK for Python V6.0.0 or later supports Python 3 and no longer supports Python 2.
We recommend that you use the following Python 3 versions: Python 3.8, Python 3.9, Python 3.10, Python 3.11, and Python 3.12.
If you want to use Python 2, we recommend that you use Tablestore SDK for Python V5.4.4 or earlier.
Before you run the code, import the Tablestore package.
Preparations
Before you initialize an OTSClient instance, you must obtain an endpoint of a Tablestore instance, install Tablestore SDK for Python, and then configure access credentials.
Obtain an endpoint of a Tablestore instance
Install Tablestore SDK for Python
Configure access credentials
Initialize an OTSClient instance
To use Tablestore SDK for Python, you must first create an OTSClient instance. Then, you can call the operations of the OTSClient instance to access Tablestore.
API operations
"""
Initialize an OTSClient instance.
end_point: the endpoint of the Tablestore instance that you want to access. The endpoint must start with https://. Example: https://instance.cn-hangzhou.ots.aliyun.com.
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 a Tablestore instance in the Tablestore console or contact an administrator to obtain the name of an existing Tablestore 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 when 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 specify a retry policy based on the 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, end_point, access_key_id, access_key_secret, instance_name, **kwargs):
Examples
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.
Use an AccessKey pair
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.
end_point = 'yourEndpoint'
instance_name = 'yourInstance'
access_key_id = os.getenv("OTS_AK_ENV")
access_key_secret = os.getenv("OTS_SK_ENV")
# Specify the log file name and retry policy.
# The name of the log file is table_store.log. The retry policy is WriteRetryPolicy, which specifies the retry attempts to write data to the table when a write operation fails.
ots_client = OTSClient(end_point, access_key_id, access_key_secret, instance_name, logger_name='table_store.log', retry_policy=WriteRetryPolicy())
# Set the TLS version for HTTPS connections to TLSv1.2.
# Note: The specified TLS version takes effect only if you use an HTTPS endpoint.
ots_client = OTSClient(end_point, access_key_id, access_key_secret, instance_name, ssl_version=ssl.PROTOCOL_TLSv1_2)
Use STS
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.
end_point = 'yourEndpoint'
instance_name = 'yourInstance'
access_key_id = os.getenv("OTS_AK_ENV")
access_key_secret = os.getenv("OTS_SK_ENV")
sts_token = os.getenv("OTS_SESSION_TOKEN")
ots_client = OTSClient(end_point, access_key_id, access_key_secret, instance_name, sts_token=sts_token)
Use the credentials tool
Before you run the sample code, make sure that you perform the following steps:
Run the following command to install the alibabacloud_credentials package:
pip install alibabacloud_credentials
Configure environment variables.
If you want to use an AccessKey pair, make sure that the
ALIBABA_CLOUD_ACCESS_KEY_ID
andALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables are configured.If you want to use STS, make sure that the
ALIBABA_CLOUD_ACCESS_KEY_ID
,ALIBABA_CLOUD_ACCESS_KEY_SECRET
, andALIBABA_CLOUD_SECURITY_TOKEN
environment variables are configured.
end_point = 'yourEndpoint'
instance_name = 'yourInstance'
# Use CredClient to read credentials from environment variables.
cred = CredClient()
access_key_id = cred.get_access_key_id()
access_key_secret = cred.get_access_key_secret()
ots_client = OTSClient(end_point, access_key_id, access_key_secret, instance_name)
Import the Tablestore package
If you do not import the Tablestore package, the NameError: name 'OTSClient' is not defined
error message appears. To prevent this issue, 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 into 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?