Skip to content

add describe regions api #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions samples/DescribeRegions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
require_once __DIR__ . '/Common.php';

use OSS\Http\RequestCore_Exception;
use OSS\OssClient;
use OSS\Core\OssException;

$bucket = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);

//*******************************Simple Usage ***************************************************************

// list all regions
$result = $ossClient->getDescribeRegions();
if ($result->getRegionInfoList() !== null){
foreach ($result->getRegionInfoList() as $region){
printf("Region:".$region->getRegion().PHP_EOL);
printf("Region Internet Endpoint:".$region->getInternetEndpoint().PHP_EOL);
printf("Region Internal Endpoint:".$region->getInternalEndpoint().PHP_EOL);
printf("Region Accelerate Endpoint:".$region->getAccelerateEndpoint().PHP_EOL);
}
}

// get region by endpoint
$options['regions'] = 'oss-cn-hangzhou';
$result = $ossClient->getDescribeRegions($options);
if ($result->getRegionInfoList() !== null){
foreach ($result->getRegionInfoList() as $region){
printf("Region:".$region->getRegion().PHP_EOL);
printf("Region Internet Endpoint:".$region->getInternetEndpoint().PHP_EOL);
printf("Region Internal Endpoint:".$region->getInternalEndpoint().PHP_EOL);
printf("Region Accelerate Endpoint:".$region->getAccelerateEndpoint().PHP_EOL);
}
}

//******************************* For complete usage, see the following functions ****************************************************

getDescribeRegions($ossClient);
listDescribeRegions($ossClient);

/**
* Set bucket logging configuration
*
* @param OssClient $ossClient OssClient instance
* @param string $bucket bucket name
* @return null
*/

/**
* Get Describe Regions
* @param OssClient $ossClient OssClient instance
* @throws RequestCore_Exception
*/
function getDescribeRegions($ossClient)
{
try {
$options['regions'] = 'oss-cn-hangzhou';
$result = $ossClient->getDescribeRegions($options);
if ($result->getRegionInfoList() !== null){
foreach ($result->getRegionInfoList() as $region){
printf("Region:".$region->getRegion().PHP_EOL);
printf("Region Internet Endpoint:".$region->getInternetEndpoint().PHP_EOL);
printf("Region Internal Endpoint:".$region->getInternalEndpoint().PHP_EOL);
printf("Region Accelerate Endpoint:".$region->getAccelerateEndpoint().PHP_EOL);
}
}
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}

/**
* List Describe Regions
*
* @param OssClient $ossClient OssClient instance
* @throws RequestCore_Exception
*/
function listDescribeRegions($ossClient)
{
try {
$result = $ossClient->getDescribeRegions();
if ($result->getRegionInfoList() !== null){
foreach ($result->getRegionInfoList() as $region){
printf("Region:".$region->getRegion().PHP_EOL);
printf("Region Internet Endpoint:".$region->getInternetEndpoint().PHP_EOL);
printf("Region Internal Endpoint:".$region->getInternalEndpoint().PHP_EOL);
printf("Region Accelerate Endpoint:".$region->getAccelerateEndpoint().PHP_EOL);
}
}
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}
72 changes: 72 additions & 0 deletions src/OSS/Model/RegionInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace OSS\Model;

/**
* Class RegionInfo
* @package OSS\Model
* @link https://help.aliyun.com/document_detail/345596.html
*/
class RegionInfo {

/**
* @var string
*/
private $region;
/**
* @var string
*/
private $internetEndpoint;
/**
* @var string
*/
private $internalEndpoint;
/**
* @var string
*/
private $accelerateEndpoint;


/**
* RegionInfo constructor.
* @param string $region
* @param string $internetEndpoint
* @param string $internalEndpoint
* @param string $accelerateEndpoint
*/
public function __construct($region, $internetEndpoint, $internalEndpoint, $accelerateEndpoint) {
$this->region = $region;
$this->internetEndpoint = $internetEndpoint;
$this->internalEndpoint = $internalEndpoint;
$this->accelerateEndpoint = $accelerateEndpoint;
}


/**
* @return string
*/
public function getRegion() {
return $this->region;
}

/**
* @return string
*/
public function getInternetEndpoint() {
return $this->internetEndpoint;
}

/**
* @return string
*/
public function getInternalEndpoint() {
return $this->internalEndpoint;
}

/**
* @return string
*/
public function getAccelerateEndpoint() {
return $this->accelerateEndpoint;
}
}

37 changes: 37 additions & 0 deletions src/OSS/Model/RegionInfoList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace OSS\Model;

use OSS\Model\RegionInfo;
/**
* Class RegionInfoList
* @package OSS\Model
* @link https://help.aliyun.com/document_detail/345596.html
*/
class RegionInfoList {


/**
* @var RegionInfo[]
*/
private $regionInfoList;

public function parseFromXml($strXml)
{
$xml = simplexml_load_string($strXml);
if (!isset($xml->RegionInfo)) return;
foreach ($xml->RegionInfo as $regionInfo) {
$region = strval($regionInfo->Region);
$internetEndpoint = strval($regionInfo->InternetEndpoint);
$internalEndpoint = strval($regionInfo->InternalEndpoint);
$accelerateEndpoint = strval($regionInfo->AccelerateEndpoint);
$this->regionInfoList[] = new RegionInfo($region, $internetEndpoint, $internalEndpoint, $accelerateEndpoint);
}
}

/**
* @return RegionInfo[]
*/
public function getRegionInfoList() {
return $this->regionInfoList;
}
}
62 changes: 61 additions & 1 deletion src/OSS/OssClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
use OSS\Model\LiveChannelInfo;
use OSS\Model\LiveChannelListInfo;
use OSS\Model\ObjectListInfoV2;
use OSS\Model\RegionInfoList;
use OSS\Model\StorageCapacityConfig;
use OSS\Result\AclResult;
use OSS\Result\BodyResult;
use OSS\Result\GetCorsResult;
use OSS\Result\GetDescribeRegionsResult;
use OSS\Result\GetLifecycleResult;
use OSS\Result\GetLocationResult;
use OSS\Result\GetLoggingResult;
Expand Down Expand Up @@ -2830,6 +2832,32 @@ public function generatePresignedUrl($bucket, $object, $expiration, $method = se
return $this->auth($options);
}


/**
* Get Describe Regions
*
* @param null|array $options
* @return RegionInfoList|ResponseCore|string|null
* @throws OssException
* @throws RequestCore_Exception
*/
public function getDescribeRegions($options = NULL)
{
$this->precheckOptions($options);
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = '/';
if (isset($options['regions'])){
$regions = $options['regions'];
}else{
$regions = "";
}
$options[self::OSS_SUB_RESOURCE] = 'regions='.$regions;
$response = $this->auth($options);
$result = new GetDescribeRegionsResult($response);
return $result->getData();
}


/**
* validates options. Create a empty array if it's NULL.
*
Expand Down Expand Up @@ -3412,11 +3440,39 @@ private function generateSignableResource($options)
$signableResource .= '/' . str_replace(array('%2F', '%25'), array('/', '%'), rawurlencode($options[self::OSS_OBJECT]));
}
if (isset($options[self::OSS_SUB_RESOURCE])) {
$signableResource .= '?' . $options[self::OSS_SUB_RESOURCE];
$subResource = $this->filterSubResource($options[self::OSS_SUB_RESOURCE]);
if (strlen($subResource) > 0){
$signableResource .= '?' . $subResource;
}
}
return $signableResource;
}


/**
* Filter sub resource
* @param $subResource
* @return string
*/
private function filterSubResource($subResource)
{
$queryString = '';
parse_str($subResource, $queryArrayParams);
foreach($queryArrayParams as $key=> $param)
{
if (!in_array($key,self::$FILTER_SIGN_KEY)){
if (!empty($param)){
$queryString .= $key . '=' . $param . '&';
}else{
$queryString .= $key . '&';
}
}
}
$queryString = substr($queryString, 0, -1);

return $queryString;
}

/**
* generates query string
*
Expand Down Expand Up @@ -3733,6 +3789,8 @@ public function setConnectTimeout($connectTimeout)
const OSS_OPTIONS_REQUEST_METHOD = 'Access-Control-Request-Method';
const OSS_OPTIONS_REQUEST_HEADERS = 'Access-Control-Request-Headers';

static $FILTER_SIGN_KEY = array("regions");

//use ssl flag
private $useSSL = false;
private $maxRetries = 3;
Expand All @@ -3749,4 +3807,6 @@ public function setConnectTimeout($connectTimeout)
private $enableStsInUrl = false;
private $timeout = 0;
private $connectTimeout = 0;


}
30 changes: 30 additions & 0 deletions src/OSS/Result/GetDescribeRegionsResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace OSS\Result;

use OSS\Core\OssException;
use OSS\Model\RegionInfoList;


/**
* Class GetDescribeRegionsResult
* @package OSS\Result
* @link https://help.aliyun.com/document_detail/345596.html
*/
class GetDescribeRegionsResult extends Result
{
/**
* @return RegionInfoList
* @throws OssException
*/
protected function parseDataFromResponse()
{
$content = $this->rawResponse->body;
if (!isset($content) || $content === "") {
throw new OssException("body is null");
}
$list= new RegionInfoList();
$list->parseFromXml($content);
return $list;
}
}
Loading