All Products
Search
Document Center

Object Storage Service:Query information about a bucket using OSS SDK for PHP 2.0

Last Updated:Mar 20, 2026

Use GetBucketInfo to retrieve metadata about a bucket, including its region, storage class, redundancy type, access control list (ACL), versioning state, cross-region replication (CRR) state, server-side encryption method, access tracking state, creation date, and the name and ID of the owner. The complete runnable sample is available on GitHub.

Prerequisites

Before you begin, make sure you have:

Usage notes

  • The sample code uses the China (Hangzhou) region (cn-hangzhou) and a public endpoint by default. To access OSS from another Alibaba Cloud service in the same region, pass the corresponding internal endpoint via --endpoint. For region-to-endpoint mappings, see Regions and endpoints.

Get bucket information

The sample accepts three command-line parameters — --region, --bucket, and optionally --endpoint — builds an OSSClient, and calls getBucketInfo.

Method signature

getBucketInfo(GetBucketInfoRequest $request): GetBucketInfoResult

Sample code

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define accepted command-line parameters.
$optsdesc = [
    "region"   => ['help' => 'The region where the bucket is located (e.g., oss-cn-hangzhou).', 'required' => true],
    "endpoint" => ['help' => 'A custom endpoint for accessing OSS.', 'required' => false],
    "bucket"   => ['help' => 'The name of the bucket.', 'required' => true],
];

$longopts = array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

$options = getopt("", $longopts);

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

$region = $options['region'];
$bucket = $options['bucket'];

// Load credentials from environment variables (AccessKey ID and AccessKey secret).
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

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

$client = new Oss\Client($cfg);

// Send the GetBucketInfo request.
$request = new Oss\Models\GetBucketInfoRequest($bucket);
$result  = $client->getBucketInfo($request);

// Print the result.
printf(
    "Status code: %s\n" .
    "Request ID:  %s\n" .
    "Bucket info: %s\n",
    $result->statusCode,
    $result->requestId,
    var_export($result->bucketInfo, true)
);

Run the sample:

php GetBucketInfo.php --region oss-cn-hangzhou --bucket <your-bucket-name>

Response fields

FieldTypeDescription
statusCodeintHTTP status code of the response.
requestIdstringUnique identifier for the request, useful for troubleshooting with Alibaba Cloud support.
bucketInfoobjectDetailed bucket metadata, including region, storage class, redundancy type, ACL, owner information, versioning state, CRR state, server-side encryption configuration, access tracking state, and public and internal endpoints.

What's next