Query the region of a bucket using OSS SDK for PHP 2.0

Updated at:

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 of the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • To query the region where a bucket is located, you must have the oss:GetBucketLocation permission. For more information, see Grant a custom policy.

Sample code

The following sample code provides an example on how to query the region of a bucket.

<?php

require_once __DIR__ . '/../vendor/autoload.php'; // Introduce autoload files 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 in which the bucket is located is required. For example: oss-cn-hangzhou.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint of the region in which the bucket is located. 
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
];
$longopts = \array_map(function ($key) {
    return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));

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

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

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

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

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

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

// Create a request to query the region of a bucket.
$request = new Oss\Models\GetBucketLocationRequest($bucket); 

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

// Display 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 region in which the bucket is located.
);

References

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

  • For more information about the API operation that you can call to query the region of a bucket, see GetBucketLocation.