A Tablestore client is a client for Tablestore and provides various methods that you can use to perform operations on tables and data in Tablestore. This topic describes how to use Tablestore SDK for PHP to initialize a Tablestore client.
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 information about the instance that you want to access
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 for PHP
For more information, see Install Tablestore SDK for PHP.
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 information about how to configure access credentials, see Configure access credentials.
Initialize a client
You must initialize a client and call the methods provided by the client to access Tablestore. Tablestore SDK for Python provides a client for the Wide Column model.
If you want to access Tablestore resources over HTTPS, you must install the OpenSSL PHP extension.
Wide Column model
The following sample code provides an example on how to initialize a Tablestore client, query the list of data tables in an instance, and display the list in the Tablestore console:
<?php
// In this example, the path is a relative path. Modify the path based on your business requirements.
require (__DIR__ . '/../../vendor/autoload.php');
use Aliyun\OTS\OTSClient as OTSClient;
// Specify the name of the instance.
$instanceName = "yourInstanceName";
// Specify the endpoint of the instance.
$endpoint = "yourEndpoint";
// Obtain the AccessKey ID and AccessKey secret from the system environment variables.
$accessKeyId = getenv('TABLESTORE_ACCESS_KEY_ID');
$accessKeySecret = getenv('TABLESTORE_ACCESS_KEY_SECRET');
// Initialize a Tablestore client.
$otsClient = new OTSClient(array(
'EndPoint' => $endpoint,
'AccessKeyID' => $accessKeyId,
'AccessKeySecret' => $accessKeySecret,
'InstanceName' => $instanceName,
));
// Query the list of data tables in the instance and display the list in the Tablestore console.
$response = $otsClient->listTable (array ());
print json_encode ($response);