You need to enable JavaScript to run this app.
导航
列举桶(PHP SDK)
最近更新时间:2024.02.04 18:30:58首次发布时间:2022.11.16 14:58:44

存储桶是存储对象的容器。本文介绍列举桶的示例代码。

示例代码

您可以通过 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;
}