OTSClient is the 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 PHP to initiate a request, you need to initialize an OTSClient instance and modify default configuration items of OTSClientConfig as required.
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.
|http://sun.cn-hangzhou.ots.aliyuncs.com||The public endpoint that is used to access the sun instance in the China (Hangzhou) region over HTTP.|
|https://sun.cn-hangzhou.ots.aliyuncs.com||The public endpoint that is used to access the sun instance in the China (Hangzhou) region over HTTPS.|
To query the endpoints of your Tablestore instance, perform the following steps:
- Log on to the Tablestore console.
- On the Overview page, click the name of an instance.
- In the Instance Access URL section of the Instance Details tab, view the endpoints of the instance.
Configure an AccessKey pair
To access Tablestore, you need to have an AccessKey pair that consists of an AccessKey ID and AccessKey secret to verify your identity. Tablestore supports the following types of AccessKey pairs:
The AccessKey pair of an Alibaba Cloud account. To obtain an AccessKey pair, perform the following steps:
The AccessKey pair of a RAM user that is authorized to access Tablestore. To obtain an AccessKey pair, perform the following steps:
Log on to the RAM console by using an Alibaba Cloud account. Then, create a RAM user or find an existing RAM user.
Grant the RAM user the permissions to access Tablestore.
After the RAM user is granted the permissions to access Tablestore, you can use the AccessKey pair of the RAM user to access Tablestore.
Temporary access credentials that are obtained from Security Token Service (STS). To obtain temporary access credentials, perform the following steps:
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.
You can use the 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
After you obtain the AccessKey ID and AccessKey secret, you can perform the following operations to initialize an OTSClient instance:
Use the endpoint of a Tablestore instance to create a client.
$accessKeyId = getenv('OTS_AK_ENV'), $accessKeySecret = getenv('OTS_SK_ENV'), $otsClient = new Aliyun\OTS\OTSClient(array( 'EndPoint' => "<your endpoint>", 'AccessKeyID' => $accessKeyId, 'AccessKeySecret' => $accessKeySecret, 'InstanceName' => "<your instance name>" ));
Configure the OTSClient instance.
To modify the default configurations of the OTSClient instance, import the required parameters such as the proxy, connection time-out time, and the maximum number of connections when you create the OTSClient instance. The following table lists the parameters you can configure when you create the OTSClient instance.
The maximum latency allowed to connect to Tablestore.
The STS token for temporary access.
The maximum latency allowed for the response to each request.
2.0 seconds. We recommend that you set this parameter to a large value when large volumes of data are transmitted.
The retry policy.
DefaultRetryPolicy. A value of null indicates that RetryPolicy is disabled.
The function to process error logs, which is used to display the logs of errors returned by Tablestore.
defaultOTSErrorLogHandler. A value of null indicates that ErrorLogHandler is disabled.
The function to process debug logs, which is used to display the logs of normal requests and responses.
defaultOTSDebugLogHandler. A value of null indicates that defaultOTSDebugLogHandler is disabled.
Install the OpenSSL PHP extension.