All Products
Search
Document Center

Tablestore:Initialize a Tablestore client

Last Updated:Oct 10, 2025

A Tablestore client provides various methods to perform operations on tables and data in Tablestore. This topic describes how to use Tablestore SDK for PHP to initialize a Tablestore client.

Important

This topic uses the AccessKey pair of an Alibaba Cloud account as an example. To initialize the client using other credentials, see Use the AccessKey pair of a RAM user to access Tablestore and Use temporary access credentials from STS to access Tablestore.

Preparations

Before initializing a Tablestore client, obtain instance information, install the Tablestore SDK, and configure access credentials.

Obtain information about the instance that you want to access

  • Region ID: The region ID of the instance. For example, the region ID of China (Hangzhou) is cn-hangzhou.

  • Instance name and endpoint: Each Tablestore instance has a unique endpoint. Your application must specify the endpoint to perform operations on tables and data. To obtain the endpoint, do as follows:

    1. Log on to the Tablestore console.

    2. In the top navigation bar, select a resource group and a region.

    3. On the Overview page, click the instance alias or click Manage Instance in the Actions column.

    4. On the Instance Details tab, view the instance name and endpoint.

      Important

      By default, Internet access is disabled for new instances. To access resources in the instance over the Internet, you must enable Internet access for the instance.

Install the Tablestore SDK

For more information, see Install Tablestore SDK for PHP.

Configure access credentials

Create an AccessKey pair for your Alibaba Cloud account or RAM user, and then configure the AccessKey pair as environment variables.

Note

After completing the configuration, restart or refresh your development environment, such as your IDE, command-line interface, desktop application, or background service, to load the latest system environment variables. For more information, see Configure access credentials.

Linux

  1. Run the following commands in the command-line interface to append environment variable settings to the ~/.bashrc file.

    echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc
    echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc
  2. Run the following command to allow the changes to take effect:

    source ~/.bashrc
  3. Run the following commands to check whether the environment variables take effect:

    echo $TABLESTORE_ACCESS_KEY_ID
    echo $TABLESTORE_ACCESS_KEY_SECRET

macOS

  1. Run the following command in the terminal to check the default Shell type.

    echo $SHELL
  2. Perform operations based on the default Shell type.

    Zsh
    1. Run the following commands to append environment variable settings to the ~/.zshrc file.

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc
    2. Run the following command to allow the changes to take effect:

      source ~/.zshrc
    3. Run the following commands to check whether the environment variables take effect:

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET
    Bash
    1. Run the following commands to append environment variable settings to the ~/.bash_profile file.

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile
    2. Run the following command to allow the changes to take effect:

      source ~/.bash_profile
    3. Run the following commands to check whether the environment variables take effect:

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET

Windows

CMD
  1. Run the following commands in CMD to set environment variables.

    setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID"
    setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"
  2. After restarting CMD, run the following commands to check whether the environment variables take effect:

    echo %TABLESTORE_ACCESS_KEY_ID%
    echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
  1. Run the following command in PowerShell:

    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
  2. Run the following commands to check whether the environment variables take effect:

    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)

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.

Important

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
// The path is a relative path. Change the path as needed.
require (__DIR__ . '/../../vendor/autoload.php');

use Aliyun\OTS\OTSClient as OTSClient;

// Replace yourInstanceName with the name of your instance.
$instanceName = "yourInstanceName";
// Replace yourEndpoint with the endpoint of your instance.
$endpoint = "yourEndpoint";
// Obtain the AccessKey ID and AccessKey secret from system variables.
$accessKeyId = getenv('TABLESTORE_ACCESS_KEY_ID');
$accessKeySecret = getenv('TABLESTORE_ACCESS_KEY_SECRET');

// Initialize the Tablestore client.
$client = new OTSClient(array(
    'EndPoint' => $endpoint,
    'AccessKeyID' => $accessKeyId,
    'AccessKeySecret' => $accessKeySecret,
    'InstanceName' => $instanceName,
));

// List the data tables in the instance and print the list to the console.
$response = $client->listTable (array ());
print json_encode ($response);

FAQ