存储桶是存储对象的容器。本文介绍获取桶元数据的示例代码。
您可以通过 TosClient->headBucket 接口获取桶元数据,通过该接口您还可以判断桶是否存在,示例代码如下:
<?php // 假设使用 composer 安装 require_once __DIR__ . '/vendor/autoload.php'; use Tos\TosClient; use Tos\Exception\TosClientException; use Tos\Exception\TosServerException; use Tos\Model\HeadBucketInput; try { $client = new TosClient([ 'region' => 'your region', 'endpoint' => 'your endpoint', // 从环境变量中获取访问密钥 'ak' => getenv('TOS_ACCESS_KEY'), 'sk' => getenv('TOS_SECRET_KEY'), ]); $input = new HeadBucketInput('bucket-test'); $output = $client->headBucket($input); echo $output->getRequestId() . PHP_EOL; // 获取桶的元数据 echo $output->getRegion() . PHP_EOL; echo $output->getStorageClass() . PHP_EOL; echo $output->getAzRedundancyType() . PHP_EOL; } catch (TosClientException $ex) { echo $ex->getMessage() . PHP_EOL; } catch (TosServerException $ex) { if($ex->getStatusCode() === 404){ echo 'bucket does not exist' . PHP_EOL; } echo $ex->getRequestId() . PHP_EOL; echo $ex->getStatusCode() . PHP_EOL; }