This topic describes how to use PHP SDK V2 to query endpoint information for all supported regions or a specific region. The information includes public endpoints (IPv4), internal endpoints for classic networks or VPCs, and acceleration endpoints for cross-region upload and download acceleration.
Precautions
Querying endpoint information for all supported regions or a specific region depends on the regions supported by OSS, not on whether you have created a bucket in that region.
The sample code in this topic uses the ID of the China (Hangzhou) region,
cn-hangzhou, as an example. The sample code uses a public endpoint by default. If you want to access OSS from other Alibaba Cloud services in the same region, you must use an internal endpoint. For more information about the mappings between OSS-supported regions and endpoints, see OSS regions and endpoints.
Query the Endpoint information of all supported regions
<?php
// Import the Composer autoloader to load dependency packages.
require_once __DIR__ . '/../vendor/autoload.php';
// Import the namespace of Alibaba Cloud OSS SDK V2.
use AlibabaCloud\Oss\V2 as Oss;
// Define the description of command-line arguments.
$optsdesc = [
// region parameter: specifies the region where the bucket is located. This parameter is required.
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
// endpoint parameter: specifies the domain name used to access OSS. This parameter is optional.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
];
// Convert parameter names to the format required by the getopt function (add a colon to indicate that a value is required).
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Parse command-line arguments.
$options = getopt("", $longopts);
// Check whether required arguments are provided.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
// Obtain the value of the region parameter.
$region = $options["region"];
// Create an environment variable credential provider to obtain access credentials from environment variables.
// You must set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Load the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// If the endpoint parameter is provided, set the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a request object to describe regions.
$request = new Oss\Models\DescribeRegionsRequest();
// Send the request to obtain region information.
$result = $client->describeRegions($request);
// Print the response, including the following information:
// - HTTP status code
// - Request ID
// - Complete response
printf(
'status code:' . $result->statusCode . PHP_EOL .
'request id:' . $result->requestId . PHP_EOL .
'result:' . var_export($result, true)
);Query the Endpoint information of a specified region
<?php
// Import the Composer autoloader to load dependency packages.
require_once __DIR__ . '/../vendor/autoload.php';
// Import the namespace of Alibaba Cloud OSS SDK V2.
use AlibabaCloud\Oss\V2 as Oss;
// Define the description of command-line arguments.
$optsdesc = [
// region parameter: specifies the region where the bucket is located. This parameter is required.
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
// endpoint parameter: specifies the domain name used to access OSS. This parameter is optional.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
];
// Convert parameter names to the format required by the getopt function (add a colon to indicate that a value is required).
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Parse command-line arguments.
$options = getopt("", $longopts);
// Check whether required arguments are provided.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
// Obtain the value of the region parameter.
$region = $options["region"];
// Create an environment variable credential provider to obtain access credentials from environment variables.
// You must set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Load the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// If the endpoint parameter is provided, set the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a request object to describe regions.
$request = new Oss\Models\DescribeRegionsRequest(regions: "oss-cn-hangzhou");// Take the China (Hangzhou) region as an example. Set the value to oss-cn-hangzhou. For other regions, specify the actual region ID.
// Send the request to obtain region information.
$result = $client->describeRegions($request);
// Print the response, including the following information:
// - HTTP status code
// - Request ID
// - Complete response
printf(
'status code:' . $result->statusCode . PHP_EOL .
'request id:' . $result->requestId . PHP_EOL .
'result:' . var_export($result, true)
);References
For more information about the API operation used to query endpoint information for a region, see DescribeRegions.
For the complete sample code for querying endpoint information for a region, see the GitHub example.