All Products
Search
Document Center

Tablestore:Initialize an OTSClient instance

Last Updated:Sep 26, 2024

OTSClient is the 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 PHP to initiate a request, you must initialize an OTSClient instance and modify the default configurations in OTSClientConfig based on your business requirements.

Usage notes

If you want to access Tablestore resources over HTTPS, you must install the OpenSSL PHP extension.

Preparations

Before you initialize an OTSClient instance, you must obtain an endpoint of a Tablestore instance, install Tablestore SDK for PHP, and then configure access credentials.

Obtain an endpoint of a Tablestore instance

After you create a Tablestore instance, you must obtain an endpoint of the instance. Then, 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. Activate the Tablestore service if it is not activated. For more information, see Activate Tablestore.

  2. Create a Tablestore instance. For more information, see 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. In the Instance Access URL section of the Instance Details tab, view the endpoints of the instance.

      image

Install Tablestore SDK for PHP

For more information, see Install Tablestore SDK for PHP.

Configure access credentials

To access Tablestore, you must have a valid AccessKey pair to verify your identity. For more information, see Configure access credentials.

Initialize an OTSClient instance

To use Tablestore SDK for PHP, you must first create an OTSClient instance. Then, you can call the operations of the OTSClient instance to access Tablestore.

Examples

Use an AccessKey pair

Note

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.

$accessKeyId = getenv('OTS_AK_ENV');
$accessKeySecret = getenv('OTS_SK_ENV');
$otsClient = new Aliyun\OTS\OTSClient(array(
    'EndPoint' => "<yourEndpoint>",
    'AccessKeyID' => $accessKeyId,
    'AccessKeySecret' => $accessKeySecret,
    'InstanceName' => "<yourInstance>"
));

The following table describes the parameters.

Parameter

Example

Description

EndPoint

https://myinstance.cn-hangzhou.ots.aliyuncs.com

The endpoint that is used to access the Tablestore instance. For more information, see Obtain an endpoint of a Tablestore instance.

AccessKeyID

getenv('OTS_AK_ENV')

The AccessKey pair that is obtained from environment variables. Make sure that the environment variables are configured.

AccessKeySecret

getenv('OTS_SK_ENV')

InstanceName

myinstance

The name of the instance. For more information, see Instances.

ConnectionTimeout

2.0

The maximum latency allowed to connect to Tablestore, in seconds. Default value: 2.0.

SocketTimeout

2.0

The maximum latency allowed for the response to each request, in seconds. Default value: 2.0.

We recommend that you set this parameter to a large value when large volumes of data are transmitted.

RetryPolicy

DefaultRetryPolicy

The retry policy. Default value: DefaultRetryPolicy.

To disable the retry policy, set this parameter to null.

DebugLogHandler

defaultOTSDebugLogHandler

The function to process debug logs, which is used to display the logs of normal requests and responses. Default value: defaultOTSDebugLogHandler.

To disable the function, set this parameter to null.

ErrorLogHandler

defaultOTSErrorLogHandler

The function to process error logs, which is used to display the logs of errors returned from the Tablestore server. Default value: defaultOTSErrorLogHandler.

To disable the function, set this parameter to null.

Use STS

Note

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.

$accessKeyId = getenv('OTS_AK_ENV');
$accessKeySecret = getenv('OTS_SK_ENV');
$securityToken = getenv('OTS_SESSION_TOKEN');
$otsClient = new Aliyun\OTS\OTSClient(array(
    'EndPoint' => "<yourEndpoint>",
    'AccessKeyID' => $accessKeyId,
    'AccessKeySecret' => $accessKeySecret,
    'InstanceName' => "<yourInstance>",
    'StsToken' => $securityToken
));

The following table describes the parameters.

Parameter

Example

Description

EndPoint

https://myinstance.cn-hangzhou.ots.aliyuncs.com

The endpoint that is used to access the Tablestore instance. For more information, see Obtain an endpoint of a Tablestore instance.

AccessKeyID

getenv('OTS_AK_ENV')

The AccessKey pair and security token that are obtained from environment variables. Make sure that the environment variables are configured.

AccessKeySecret

getenv('OTS_SK_ENV')

StsToken

getenv('OTS_SESSION_TOKEN')

InstanceName

myinstance

The name of the instance. For more information, see Instances.

ConnectionTimeout

2.0

The maximum latency allowed to connect to Tablestore, in seconds. Default value: 2.0.

SocketTimeout

2.0

The maximum latency allowed for the response to each request, in seconds. Default value: 2.0.

We recommend that you set this parameter to a large value when large volumes of data are transmitted.

RetryPolicy

DefaultRetryPolicy

The retry policy. Default value: DefaultRetryPolicy.

To disable the retry policy, set this parameter to null.

DebugLogHandler

defaultOTSDebugLogHandler

The function to process debug logs, which is used to display the logs of normal requests and responses. Default value: defaultOTSDebugLogHandler.

To disable the function, set this parameter to null.

ErrorLogHandler

defaultOTSErrorLogHandler

The function to process error logs, which is used to display the logs of errors returned from the Tablestore server. Default value: defaultOTSErrorLogHandler.

To disable the function, set this parameter to null.

FAQ

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