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

Torch Object Storage

Copy page
Download PDF
PHP
Quick Start Guide (PHP SDK)
Copy page
Download PDF
Quick Start Guide (PHP SDK)
This chapter describes how to perform common operations using the TOS PHP SDK, such as creating buckets, uploading, downloading, deleting objects, and more.​
Prerequisites​
Before sending HTTP or HTTPS requests to TOS, you must first create a TOS client (that is, instantiate a TosClient). For detailed instructions, see Client Initialization.​
General Client Example​
After completing an API operation using TosClient, the absence of any exceptions indicates that the operation was successful. If an exception is thrown, the operation has failed; in this case, detailed error information should be obtained from SDK custom exceptions. The following code demonstrates the general usage of TosClient.​
​
<?php​
​
require_once __DIR__ . '/vendor/autoload.php';​
​
use Tos\TosClient;​
use Tos\Exception\TosClientException;​
use Tos\Exception\TosServerException;​
use Tos\Model\PutObjectInput;​
​
$region = 'your region';​
$endpoint = 'your endpoint';​
$ak = getenv('TOS_ACCESS_KEY'); ​
$sk = getenv('TOS_SECRET_KEY'); ​
$bucket = 'bucket-test';​
$key = 'key-test';​
$data = 'hello world';​
​
try {​
$client = new TosClient([​
'region' => $region,​
'endpoint' => $endpoint,​
'ak' => $ak,​
'sk' => $sk,​
]);​
​
$input = new PutObjectInput($bucket, $key, $data);​
$output = $client->putObject($input);​
echo 'put object succeed, request id: ' . $output->getRequestId() . PHP_EOL;​
echo $output->getETag() . PHP_EOL;​
echo $output->getHashCrc64ecma() . PHP_EOL;​
} catch (TosClientException $ex) {​
echo 'put object failed, message: ' . $ex->getMessage() . PHP_EOL;​
} catch (TosServerException $ex) {​
echo 'put object failed, code: ' . $ex->getErrorCode() . PHP_EOL;​
echo $ex->getRequestId() . PHP_EOL;​
echo $ex->getStatusCode() . PHP_EOL;​
echo $ex->getMessage() . PHP_EOL;​
} catch (\Exception $ex) {​
echo 'put object unexpected exception, message: ' . $ex->getMessage() . PHP_EOL;​
}​
​
Create Bucket​
A bucket in TOS serves as the global namespace, functioning as a data container and the root directory of the file system, and is capable of storing multiple objects. The following code demonstrates how to create a new bucket.​
​
<?php​
​
require_once __DIR__ . '/vendor/autoload.php';​
​
use Tos\TosClient;​
use Tos\Exception\TosClientException;​
use Tos\Exception\TosServerException;​
use Tos\Model\CreateBucketInput;​
​
try {​
$client = new TosClient([​
'region' => 'your region',​
'endpoint' => 'your endpoint',​
'ak' => getenv('TOS_ACCESS_KEY'),​
'sk' => getenv('TOS_SECRET_KEY'),​
]);​
​
$input = new CreateBucketInput('bucket-test');​
$output = $client->createBucket($input);​
​
echo $output->getRequestId() . PHP_EOL;​
} catch (TosClientException $ex) {​
echo $ex->getMessage() . PHP_EOL;​
} catch (TosServerException $ex) {​
echo $ex->getRequestId() . PHP_EOL;​
echo $ex->getStatusCode() . PHP_EOL;​
echo $ex->getErrorCode() . PHP_EOL;​
}​
​
Uploads an object.​
The following code demonstrates how to upload an object to a TOS bucket.​
​
<?php​
​
require_once __DIR__ . '/vendor/autoload.php';​
​
use Tos\TosClient;​
use Tos\Exception\TosClientException;​
use Tos\Exception\TosServerException;​
use Tos\Model\PutObjectInput;​
​
try {​
$client = new TosClient([​
'region' => 'your region',​
'endpoint' => 'your endpoint',​
'ak' => getenv('TOS_ACCESS_KEY'),​
'sk' => getenv('TOS_SECRET_KEY'),​
]);​
​
$input = new PutObjectInput('bucket-test', 'key-test', 'hello world');​
$output = $client->putObject($input);​
​
echo $output->getRequestId() . PHP_EOL;​
} catch (TosClientException $ex) {​
echo $ex->getMessage() . PHP_EOL;​
} catch (TosServerException $ex) {​
echo $ex->getRequestId() . PHP_EOL;​
echo $ex->getStatusCode() . PHP_EOL;​
echo $ex->getErrorCode() . PHP_EOL;​
}​
​
Download Object​
The following code demonstrates how to download a specified object from a TOS bucket and read its data.​
​
<?php​
​
require_once __DIR__ . '/vendor/autoload.php';​
​
use Tos\TosClient;​
use Tos\Exception\TosClientException;​
use Tos\Exception\TosServerException;​
use Tos\Model\GetObjectInput;​
​
$output = null;​
try {​
$client = new TosClient([​
'region' => 'your region',​
'endpoint' => 'your endpoint',​
'ak' => getenv('TOS_ACCESS_KEY'),​
'sk' => getenv('TOS_SECRET_KEY'),​
]);​
​
$input = new GetObjectInput('bucket-test', 'key-test');​
$output = $client->getObject($input);​
​
echo $output->getRequestId() . PHP_EOL;​
echo $output->getContent().getContents(). PHP_EOL;​
} catch (TosClientException $ex) {​
echo $ex->getMessage() . PHP_EOL;​
} catch (TosServerException $ex) {​
echo $ex->getRequestId() . PHP_EOL;​
echo $ex->getStatusCode() . PHP_EOL;​
echo $ex->getErrorCode() . PHP_EOL;​
} finally {​
if ($output) {​
$output->getContent()->close();​
}​
}​
​
Enumeration of Objects​
After completing a series of object upload operations, it may be necessary to review which objects are contained in the bucket. The following code demonstrates how to list objects in a specified bucket.​
​
<?php​
​
require_once __DIR__ . '/vendor/autoload.php';​
​
use Tos\TosClient;​
use Tos\Exception\TosClientException;​
use Tos\Exception\TosServerException;​
use Tos\Model\ListObjectsInput;​
​
$output = null;​
try {​
$client = new TosClient([​
'region' => 'your region',​
'endpoint' => 'your endpoint',​
'ak' => getenv('TOS_ACCESS_KEY'),​
'sk' => getenv('TOS_SECRET_KEY'),​
]);​
​
$input = new ListObjectsInput('bucket-test');​
$output = $client->listObjects($input);​
​
echo $output->getRequestId() . PHP_EOL;​
foreach ($output->getContents() as $content) {​
echo $content->getKey() . PHP_EOL;​
echo $content->getETag() . PHP_EOL;​
}​
} catch (TosClientException $ex) {​
echo $ex->getMessage() . PHP_EOL;​
} catch (TosServerException $ex) {​
echo $ex->getRequestId() . PHP_EOL;​
echo $ex->getStatusCode() . PHP_EOL;​
echo $ex->getErrorCode() . PHP_EOL;​
} finally {​
if ($output) {​
$output->getContent()->close();​
}​
}​
​
When you delete an object​
The following code demonstrates how to delete a specified object.​
​
<?php​
​
require_once __DIR__ . '/vendor/autoload.php';​
​
use Tos\TosClient;​
use Tos\Exception\TosClientException;​
use Tos\Exception\TosServerException;​
use Tos\Model\DeleteObjectInput;​
​
$output = null;​
try {​
$client = new TosClient([​
'region' => 'your region',​
'endpoint' => 'your endpoint',​
'ak' => getenv('TOS_ACCESS_KEY'),​
'sk' => getenv('TOS_SECRET_KEY'),​
]);​
​
$input = new DeleteObjectInput('bucket-test', 'key-test');​
$output = $client->deleteObject($input);​
​
echo $output->getRequestId() . PHP_EOL;​
} catch (TosClientException $ex) {​
echo $ex->getMessage() . PHP_EOL;​
} catch (TosServerException $ex) {​
echo $ex->getRequestId() . PHP_EOL;​
echo $ex->getStatusCode() . PHP_EOL;​
echo $ex->getErrorCode() . PHP_EOL;​
}​
​
​
Last updated: 2025.09.23 14:41:21