All Products
Search
Document Center

Object Storage Service:List buckets using OSS SDK for PHP 2.0

Last Updated:Mar 20, 2026

Use the OSS SDK for PHP 2.0 to list all buckets in your account across all regions, with optional filtering by resource group.

Prerequisites

Before you begin, ensure that you have:

Usage notes

  • The examples on this page use the China (Hangzhou) region (cn-hangzhou) and a public endpoint. To access OSS from another Alibaba Cloud product in the same region, use an internal endpoint instead. For supported regions and endpoints, see OSS regions and endpoints.

  • Resource group filtering behavior:

    • By default, the request does not include a resource group ID, and the XML response contains no resource group information. OSS returns all buckets the requester owns.

    • If you include a resource group ID, OSS returns only the buckets in that resource group.

How it works

ListBucketsPaginator handles pagination for the ListBuckets operation automatically. Because the API may return results across multiple pages, the paginator iterates through each page so you do not need to manage continuation tokens manually.

Each page contains a subset of buckets. For each bucket, the response includes the bucket name and its region.

List all buckets in an account

<?php

require_once __DIR__ . '/../vendor/autoload.php'; // Load dependency libraries via the autoloader.

use AlibabaCloud\Oss\V2 as Oss;

// Define command-line arguments.
$optsdesc = [
    "region"   => ['help' => 'The region where the bucket is located.', 'required' => true],  // Required. Example: oss-cn-hangzhou.
    "endpoint" => ['help' => 'The endpoint for accessing OSS.',          'required' => false], // Optional.
];
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse command-line arguments.
$options = getopt("", $longopts);

// Validate required arguments.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === true && empty($options[$key])) {
        echo "Error: --$key is required. {$value['help']}\n";
        exit(1);
    }
}

$region = $options["region"];

// Load credentials (AccessKeyId and AccessKeySecret) from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Initialize the SDK configuration.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create the OSS client.
$client = new Oss\Client($cfg);

// Create a paginator and iterate over all pages.
$paginator = new Oss\Paginator\ListBucketsPaginator($client);
$iter = $paginator->iterPage(new Oss\Models\ListBucketsRequest());

foreach ($iter as $page) {
    foreach ($page->buckets ?? [] as $bucket) {
        print("Bucket: $bucket->name, $bucket->location\n");
    }
}

Sample output:

Bucket: my-bucket-a, oss-cn-hangzhou
Bucket: my-bucket-b, oss-ap-southeast-1

List buckets in a resource group

To filter by resource group, pass the resource group ID as resourceGroupId in ListBucketsRequest. OSS returns only the buckets that belong to the specified resource group.

<?php

require_once __DIR__ . '/../vendor/autoload.php'; // Load dependency libraries via the autoloader.

use AlibabaCloud\Oss\V2 as Oss;

// Define command-line arguments.
$optsdesc = [
    "region"   => ['help' => 'The region where the bucket is located.', 'required' => true],
    "endpoint" => ['help' => 'The endpoint for accessing OSS.',          'required' => false],
];
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse command-line arguments.
$options = getopt("", $longopts);

// Validate required arguments.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === true && empty($options[$key])) {
        echo "Error: --$key is required. {$value['help']}\n";
        exit(1);
    }
}

$region = $options["region"];

// Load credentials (AccessKeyId and AccessKeySecret) from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Initialize the SDK configuration.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create the OSS client.
$client = new Oss\Client($cfg);

// Create a paginator filtered by resource group ID.
$paginator = new Oss\Paginator\ListBucketsPaginator($client);
$iter = $paginator->iterPage(new Oss\Models\ListBucketsRequest(
    resourceGroupId: "rg-aekzfalvmw2sxby", // Replace with your resource group ID.
));

foreach ($iter as $page) {
    foreach ($page->buckets ?? [] as $bucket) {
        print("Bucket: $bucket->name, $bucket->location\n");
    }
}

References