You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
PHP
Initialize Client (PHP SDK)
Copy page
Download PDF
Initialize Client (PHP SDK)
TosClient is the PHP client for TOS, providing a suite of interfaces for interacting with TOS services to manage resources such as buckets and objects.​
Prerequisites​
Configuring long-term access credentials​
Before initializing the SDK, you need to first configure long-term access credentials. This section describes the procedures for configuring long-term access credentials on different operating systems.​
​
macOS
Linux
Windows
​
  1. Open the terminal and execute the following command to open the file.​
​
nano ~/.bash_profile​
​
  1. Add AKSK information at the end of the document.​
​
export TOS_ACCESS_KEY=AKTPYmI1Z****​
export TOS_SECRET_KEY=T1dJM01UU****​
​
  1. Save the file and exit.​
  1. Execute the following command to make the configuration information take effect.​
​
source ~/.bash_profile​
​
  1. Execute the following command to verify the configuration information.​
​
echo $TOS_ACCESS_KEY​
echo $TOS_SECRET_KEY​
​
If the configuration is successful, the following example will be returned:​
​
AKTPYmI1Z****​
T1dJM01UU****​
​
​
Create TosClient​
  • Using region and endpoint​
tip​
  • Endpoint is the access domain name for TOS external services. For information on Endpoints supported by TOS, please refer to Regions and endpoints.​
​
<?php​
​
require_once __DIR__ . '/vendor/autoload.php';​
​
use Tos\TosClient;​
​
$region = 'your region';​
$endpoint = 'your endpoint';​
​
$ak = getenv('TOS_ACCESS_KEY'); ​
$sk = getenv('TOS_SECRET_KEY'); ​
​
try {​
$client = new TosClient([​
'region' => $region,​
'endpoint' => $endpoint,​
'ak' => $ak,​
'sk' => $sk,​
]);​
} catch (\RuntimeException $ex) {​
echo 'create TosClient failed, message: ' . $ex->getMessage() . PHP_EOL;​
}​
​
​
<?php​
​
require_once __DIR__ . '/vendor/autoload.php';​
​
use Tos\TosClient;​
​
$region = 'your region';​
$endpoint = 'your endpoint';​
$ak = 'your access key';​
$sk = 'your secret key';​
$securityToken = 'your security token';​
​
try {​
$client = new TosClient([​
'region' => $region,​
'endpoint' => $endpoint,​
'ak' => $ak,​
'sk' => $sk,​
'securityToken' => $securityToken​
]);​
} catch (\RuntimeException $ex) {​
echo 'create TosClient failed, message: ' . $ex->getMessage() . PHP_EOL;​
}​
​
Configuration initialization parameters​
You can configure TosClient during its creation by specifying initialization parameters. For the parameters that can be configured, please refer to the table below.​
​
Parameter​
Type​
Required ​
Example value​
Description​
region​
String​
Yes​
cn-beijing​
Region where the TOS server is located.​
endpoint​
String​
Optional; when left empty, the value is determined by the region parameter.​
https://tos-cn-beijing.volces.com/​
TOS server domain name: If no protocol is specified, HTTPS is used by default. The complete format is: https://{host}:{port}.​
ak​
String​
Mandatory for standard buckets; optional if the bucket allows anonymous access.​
AKTPYmI1ZGQwMDA0NjlhNGFkMzhjNzM0N2Q0OTQ3ZTV****​
The Access Key ID in the access key.​
sk​
String​
Mandatory for standard buckets; optional if the bucket allows anonymous access.​
T1dJM01UUXpOak0wTVdWak5EUmtOR0poWldJNU1HWmxaV1V5TkdReVl6****​
The Secret Access Key in the access key.​
securityToken​
String​
No​
3ZDA5MjY4NWYwMDQyYThlIiwiU2Vzc2lvbk5hbWUiOiJ0b3Nfcm9sZV9z************​
Temporarily authorized Security Token Service (STS).​
connectionTimeout​
float​
No​
30000​
Connection establishment timeout, unit: milliseconds; default value: 10,000 milliseconds.​
socketTimeout​
float​
No​
60000​
Socket read/write timeout period, unit: milliseconds; default is 30,000 milliseconds.​
enableVerifySSL​
bool​
No​
true​
Whether to enable SSL certificate verification; the default value is true.​
autoRecognizeContentType​
bool​
No​
true​
Whether MIME type detection is performed automatically; the default value is true.​
isCustomDomain​
bool​
No​
true​
Indicates whether the bucket name is prefixed to the Endpoint to access TOS via a virtual host domain name. The value descriptions are as follows:​
  • true: No concatenation.​
  • false: Concatenation.​
The default value is false.​
tip​
When accessing TOS via a custom domain, you must set isCustomDomain to true.​
​
​
Last updated: 2026.08.03 10:33:39