All Products
Search
Document Center

Object Storage Service:Query the region of a bucket (PHP SDK V2)

Last Updated:Aug 05, 2025

This topic describes how to query the region of a bucket.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou for the China (Hangzhou) region as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud products in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.

  • To query the region of a bucket, you must have the oss:GetBucketLocation permission. For more information, see Attach a custom policy to a RAM user.

Sample code

You can use the following code to query the region of a bucket.

<?php

require_once __DIR__ . '/../vendor/autoload.php'; // Import the autoloader file to load dependency libraries.

use AlibabaCloud\Oss\V2 as Oss;

// Define the description of command-line parameters.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region where the bucket is located is a required parameter. For example, oss-cn-hangzhou.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is an optional parameter. It specifies the domain name that other services can use to access OSS.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is a required parameter.
];
$longopts = \array_map(function ($key) {
    return "$key:"; // Add a colon (:) after each parameter to indicate that a value is required.
}, array_keys($optsdesc));

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

// Check whether required parameters are missing.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required parameter is missing.
        exit(1); 
    }
}

// Obtain the values of the command-line parameters.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The bucket name.

// Use environment variables to load credential information (AccessKeyId and AccessKeySecret).
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider(); 

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault(); // Load the default configurations of the SDK.
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider.
$cfg->setRegion($region); // Set the region.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]); // If an endpoint is provided, set the endpoint.
}

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

// Create a request object to obtain the bucket location.
$request = new Oss\Models\GetBucketLocationRequest($bucket); 

// Call the getBucketLocation method to obtain the bucket location.
$result = $client->getBucketLocation($request); 

// Print the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
    'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
    'bucket location:' . var_export($result->location, true) // The location of the region where the bucket is located.
);

References

  • For the complete sample code used to query the region of a bucket, see GitHub example.

  • For more information about the API operation for querying the region of a bucket, see GetBucketLocation.