All Products
Search
Document Center

Object Storage Service:Get the region of a bucket (PHP SDK V1)

Last Updated:Mar 20, 2026

Use the OSS PHP SDK V1 to retrieve the region of a bucket by calling getBucketLocation().

Prerequisites

Before you begin, make sure that you have:

Get the region of a bucket

The following example calls getBucketLocation($bucket) and prints the returned region string.

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
$provider = new EnvironmentVariableCredentialsProvider();

// Specify the endpoint of any OSS region. Example: https://oss-cn-hangzhou.aliyuncs.com
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

// Specify the bucket name.
$bucket = "examplebucket";

try {
    $config = array(
        "provider"         => $provider,
        "endpoint"         => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"           => "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

    // Returns a string such as "oss-cn-hangzhou".
    $region = $ossClient->getBucketLocation($bucket);
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK\n");
var_dump($region);

Return value: getBucketLocation() returns a string in the format oss-<region-id>, for example, oss-cn-hangzhou. Use this value to identify the region where the bucket resides.

Note: The example above uses the public endpoint of the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For a list of supported regions and endpoints, see Regions and endpoints.

What's next