All Products
Search
Document Center

Tablestore:Initialize an OTSClient instance

Last Updated:Jan 02, 2024

OTSClient is a 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 must initialize an OTSClient instance and modify the default configurations of the OTSClient instance 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 configure an AccessKey pair, obtain the endpoint of your Tablestore instance, and install Tablestore SDK for PHP.

Configure an AccessKey pair

To access Tablestore, you must have a valid AccessKey pair to verify your identity. Tablestore supports the following types of AccessKey pairs. To ensure the security of your AccessKey pair, we recommend that you configure the AccessKey pair in environment variables of your operating system.

  1. Obtain an AccessKey pair.

    Important

    To prevent security risks caused by the leakage of the AccessKey pair of your Alibaba Cloud account, we recommend that you create a Resource Access Management (RAM) user that is granted the permissions to access Tablestore and use the AccessKey pair of the RAM user to access Tablestore.

    AccessKey pair

    Procedure

    AccessKey pair of your Alibaba Cloud account

    1. On the Alibaba Cloud official website, create an Alibaba Cloud account.

    2. Create an AccessKey pair that consists of an AccessKey ID and an AccessKey secret. For more information, see Obtain an AccessKey pair.

    AccessKey pair of a RAM user that is granted the permissions to access Tablestore

    1. Log on to the RAM console by using an Alibaba Cloud account. Then, create a RAM user or find an existing RAM user.

    2. Use the Alibaba Cloud account to grant the access permissions on Tablestore to the RAM user. For more information, see Use a RAM policy to grant permissions to a RAM user.

    3. After you grant the permissions to the RAM user, use the AccessKey pair of the RAM user to access Tablestore. For more information, see Obtain an AccessKey pair.

    Temporary credentials that are obtained from Security Token Service (STS)

    1. Obtain temporary access credentials from the application server. The access credentials consist of a temporary AccessKey ID, a temporary AccessKey secret, and a security token. The application server accesses RAM or STS to obtain the temporary access credentials and return them to you.

    2. Use the temporary access credentials to access Tablestore.

  2. Configure environment variables.

    • Linux and macOS

      Run the following commands to configure environment variables. Replace<access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret.

      export OTS_AK_ENV=<access_key_id>
      export OTS_SK_ENV=<access_key_secret>
    • Windows

      Create an environment variable file and add the OTS_AK_ENV and OTS_SK_ENV environment variables to the file. Set the OTS_AK_ENV environment variable to your AccessKey ID and the OTS_SK_ENV environment variable to your AccessKey secret. Then, restart Windows to make the configurations take effect.

Obtain the endpoint of a Tablestore instance

After you create an instance, you must obtain the endpoint of 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 the "Step 1: Activate Tablestore" section of the Use the Wide Column model in the Tablestore console topic.

  2. Create an instance. For more information, see the "Step 2: Create an instance" section of the Use the Wide Column model in the Tablestore console topic.

  3. Obtain the 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. On the Instance Details tab, view the endpoints of the instance in the Instance Access URL section.

      fig_endpoint

Install Tablestore SDK for PHP

For more information, see Install Tablestore SDK for PHP.

Initialize an OTSClient instance

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

Important

The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of 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 the resources within your account is compromised. In this example, the AccessKey pair is configured in the environment variables to verify your identify.

  1. 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>"
    ));

    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 about how to obtain the endpoint, see the Obtain the endpoint of a Tablestore instance section of this topic.

    AccessKeyID

    getenv('OTS_AK_ENV')

    The AccessKey pair that is used to access the Tablestore instance. Obtain the AccessKey pair by using environment variables.

    Make sure that the environment variables are configured. For more information, see the Configure an AccessKey pair section of this topic.

    AccessKeySecret

    getenv('OTS_SK_ENV')

    InstanceName

    myinstance

    The name of the Tablestore instance that you want to access. For more information, see Instance.

  2. Configure the OTSClient instance.

    To modify the default configurations of the OTSClient instance, import the required parameters such as the proxy, connection timeout period, and maximum number of connections when you create the OTSClient instance. The following table describes the parameters that you can configure when you create the OTSClient instance.

    Parameter

    Description

    ConnectionTimeout

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

    StsToken

    The STS token for temporary access.

    This parameter is required if you use an STS token to access Tablestore. For more information about how to use an STS token, see Use an STS token.

    SocketTimeout

    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

    The retry policy. Default value: DefaultRetryPolicy.

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

    ErrorLogHandler

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

    To disable the function, set this parameter to null.

    DebugLogHandler

    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.

FAQ

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