存储桶是存储对象的容器。本文介绍列举桶的示例代码。
您可以通过 TosClient->listBuckets 列举桶,该接口会列举所有地域的桶,示例代码如下:
<?php // 假设使用 composer 安装 require_once __DIR__ . '/vendor/autoload.php'; use Tos\TosClient; use Tos\Exception\TosClientException; use Tos\Exception\TosServerException; try { $client = new TosClient([ 'region' => 'your region', 'endpoint' => 'your endpoint', // 从环境变量中获取访问密钥 'ak' => getenv('TOS_ACCESS_KEY'), 'sk' => getenv('TOS_SECRET_KEY'), ]); $output = $client->listBuckets(); echo $output->getRequestId() . PHP_EOL; // 获取桶列表 foreach ($output->getBuckets() as $bucket) { echo $bucket->getName() . PHP_EOL; echo $bucket->getLocation() . PHP_EOL; echo $bucket->getCreationDate() . 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; }