The Tablestore client is a client of Tablestore. It offers a range of methods for operating on tables and data within Tablestore. This topic explains how to initialize a Tablestore client in Python.
In the examples in this topic, the AccessKey pair of an Alibaba Cloud account is used. You can also use the AccessKey pair of a Resource Access Management (RAM) user or temporary access credentials obtained from Security Token Service (STS) to initialize a Tablestore client. For more information, see Use the AccessKey pair of a RAM user to access Tablestore and Use temporary access credentials obtained from STS to access Tablestore.
Preparations
Before you initialize a Tablestore client, you must obtain information about the Tablestore instance that you want to access, install Tablestore SDK for Java, and configure access credentials.
Obtain instance information
Region ID: the ID of the region in which the instance resides. For example, the ID of the China (Hangzhou) region is cn-hangzhou. For more information about regions, see Regions.
Instance name and endpoint: Each Tablestore instance has an endpoint. You must specify an endpoint before you can perform operations on the data and tables in Tablestore. You can perform the following steps to obtain the endpoint of an instance:
Log on to the Tablestore console.
In the top navigation bar, select a resource group and a region.
On the Overview page, click the name of the instance that you want to manage or click Manage Instance in the Actions column of the instance.
On the Instance Details tab, view the name and URLs of the instance.
Install Tablestore SDK
Run the following command to use pip to install Tablestore SDK for Python:
sudo pip install tablestore
For detailed instructions on installing the Tablestore SDK, see Install Tablestore SDK.
Configure access credentials
To use Tablestore SDK for Java to initiate a request to access Tablestore, you must configure access credentials. Alibaba Cloud verifies your identity and access permissions based on the access credentials.
In the examples in this topic, the AccessKey pair of an Alibaba Cloud account is used to describe how to configure access credentials. For more information, see How do I obtain an AccessKey pair?
If you save access credentials to the code, information leaks may occur. We recommend that you save access credentials to system environment variables.
Windows
Run the command prompt as an administrator and run the following commands:
# Specify the AccessKey ID.
setx TABLESTORE_ACCESS_KEY_ID your_access_key_id /m
# Specify the AccessKey secret.
setx TABLESTORE_ACCESS_KEY_SECRET your_access_key_secret /m
macOS/Linux/Unix
# Specify the AccessKey ID.
export TABLESTORE_ACCESS_KEY_ID=your_access_key_id
# Specify the AccessKey secret.
export TABLESTORE_ACCESS_KEY_SECRET=your_access_key_secret
For more information on configuring access credentials, see Configure access credentials.
Initialize a client
To access Tablestore services, you must first initialize a client and then use the client's methods. The Tablestore Python SDK provides the wide table model client, OTSClient.
To access Tablestore resources via the HTTPS protocol, it is advisable to use Python SDK version 6.x.x or later, and to ensure your OpenSSL version is at least 0.9.8j, with OpenSSL 1.0.2d being the preferred version.
The Tablestore Python SDK deployment package includes the certifi package, allowing for immediate installation and use. Should you require an updated root certificate, you can download the latest version from Root Certificate.
Wide table model
The example code below demonstrates how to initialize a Tablestore client, retrieve all table names in the instance, and print them to the console:
# -*- coding: utf-8 -*-
import os
from tablestore import OTSClient
# yourInstanceName: Fill in your instance name
instance_name = "yourInstanceName"
# yourEndpoint: Fill in your instance access address
endpoint = "yourEndpoint"
# Obtain AccessKey ID and AccessKey Secret from environment variables
access_key_id = os.getenv("TABLESTORE_ACCESS_KEY_ID")
access_key_secret = os.getenv("TABLESTORE_ACCESS_KEY_SECRET")
# Initialize Tablestore client
client = OTSClient(endpoint, access_key_id, access_key_secret, instance_name)
# List the data tables in the instance and print them to the console
resp = client.list_table()
for table_name in resp:
print(table_name)
You can also use the Credentials tool to read access credentials from environment variables. For more information, see Appendix 1: Use the Credentials tool to read access credentials.
FAQ
-
Signature mismatch exception occurs when using Tablestore SDK
-
Request denied by instance ACL policies exception occurs when accessing Tablestore by using SDK
-
Request denied because this instance can only be accessed from the binded VPC exception occurs when accessing Tablestore by using SDKRequest denied because this instance can only be accessed from the bound VPC exception occurs when accessing Tablestore using the SDK
Appendix 1: Use the credentials tool to read access credentials
-
Install the alibabacloud_credentials package by running the following command:
pip install alibabacloud_credentials
-
Set up the environment variables.
Configure the environment variables
ALIBABA_CLOUD_ACCESS_KEY_ID
andALIBABA_CLOUD_ACCESS_KEY_SECRET
, which correspond to the AccessKey ID and AccessKey Secret of an Alibaba Cloud account, respectively. -
Read the access credentials.
Use the Credentials tool to read access credentials from environment variables as shown in the example code below:
# -*- coding: utf-8 -*- from alibabacloud_credentials.client import Client as CredClient # Use CredClient to obtain AccessKey ID and AccessKey Secret from environment variables cred = CredClient() access_key_id = cred.get_credential().access_key_id access_key_secret = cred.get_credential().access_key_secret