From 9dbf12062b40be8b9010a37c3617cc9301d632ba Mon Sep 17 00:00:00 2001 From: yangpeng Date: Wed, 19 Jul 2023 13:18:36 +0800 Subject: [PATCH] add describe regions api --- samples/DescribeRegions.php | 100 ++++++++++++++++++ src/OSS/Model/RegionInfo.php | 72 +++++++++++++ src/OSS/Model/RegionInfoList.php | 37 +++++++ src/OSS/OssClient.php | 62 ++++++++++- src/OSS/Result/GetDescribeRegionsResult.php | 30 ++++++ .../Tests/GetDescribeRegionsResultTest.php | 100 ++++++++++++++++++ .../Tests/OssClientDescribeRegionsTest.php | 39 +++++++ 7 files changed, 439 insertions(+), 1 deletion(-) create mode 100644 samples/DescribeRegions.php create mode 100644 src/OSS/Model/RegionInfo.php create mode 100644 src/OSS/Model/RegionInfoList.php create mode 100644 src/OSS/Result/GetDescribeRegionsResult.php create mode 100644 tests/OSS/Tests/GetDescribeRegionsResultTest.php create mode 100644 tests/OSS/Tests/OssClientDescribeRegionsTest.php diff --git a/samples/DescribeRegions.php b/samples/DescribeRegions.php new file mode 100644 index 0000000..41ed684 --- /dev/null +++ b/samples/DescribeRegions.php @@ -0,0 +1,100 @@ +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"); +} \ No newline at end of file diff --git a/src/OSS/Model/RegionInfo.php b/src/OSS/Model/RegionInfo.php new file mode 100644 index 0000000..98c20cd --- /dev/null +++ b/src/OSS/Model/RegionInfo.php @@ -0,0 +1,72 @@ +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; + } +} + diff --git a/src/OSS/Model/RegionInfoList.php b/src/OSS/Model/RegionInfoList.php new file mode 100644 index 0000000..07469a8 --- /dev/null +++ b/src/OSS/Model/RegionInfoList.php @@ -0,0 +1,37 @@ +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; + } +} diff --git a/src/OSS/OssClient.php b/src/OSS/OssClient.php index c953344..3dc8270 100644 --- a/src/OSS/OssClient.php +++ b/src/OSS/OssClient.php @@ -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; @@ -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. * @@ -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 * @@ -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; @@ -3749,4 +3807,6 @@ public function setConnectTimeout($connectTimeout) private $enableStsInUrl = false; private $timeout = 0; private $connectTimeout = 0; + + } diff --git a/src/OSS/Result/GetDescribeRegionsResult.php b/src/OSS/Result/GetDescribeRegionsResult.php new file mode 100644 index 0000000..03e29fb --- /dev/null +++ b/src/OSS/Result/GetDescribeRegionsResult.php @@ -0,0 +1,30 @@ +rawResponse->body; + if (!isset($content) || $content === "") { + throw new OssException("body is null"); + } + $list= new RegionInfoList(); + $list->parseFromXml($content); + return $list; + } +} \ No newline at end of file diff --git a/tests/OSS/Tests/GetDescribeRegionsResultTest.php b/tests/OSS/Tests/GetDescribeRegionsResultTest.php new file mode 100644 index 0000000..ded711c --- /dev/null +++ b/tests/OSS/Tests/GetDescribeRegionsResultTest.php @@ -0,0 +1,100 @@ + + + +oss-cn-hangzhou +oss-cn-hangzhou.aliyuncs.com +oss-cn-hangzhou-internal.aliyuncs.com +oss-accelerate.aliyuncs.com + + +oss-cn-shanghai +oss-cn-shanghai.aliyuncs.com +oss-cn-shanghai-internal.aliyuncs.com +oss-accelerate.aliyuncs.com + + +BBBB; + private $validXml1 = << + + +oss-cn-hangzhou +oss-cn-hangzhou.aliyuncs.com +oss-cn-hangzhou-internal.aliyuncs.com +oss-accelerate.aliyuncs.com + + +BBBB; + + private $invalidXml2 = << + + +BBBB; + + public function testParseValidXml() + { + $response = new ResponseCore(array(), $this->validXml, 200); + $result = new GetDescribeRegionsResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $result = $result->getData(); + $this->assertEquals(count($result->getRegionInfoList()), 2); + + $list = $result->getRegionInfoList(); + $this->assertEquals($list[0]->getRegion(), "oss-cn-hangzhou"); + $this->assertEquals($list[0]->getRegion(), "oss-cn-hangzhou"); + $this->assertEquals($list[0]->getInternetEndpoint(), "oss-cn-hangzhou.aliyuncs.com"); + $this->assertEquals($list[0]->getInternalEndpoint(), "oss-cn-hangzhou-internal.aliyuncs.com"); + $this->assertEquals($list[0]->getAccelerateEndpoint(), "oss-accelerate.aliyuncs.com"); + + $this->assertEquals($list[1]->getRegion(), "oss-cn-shanghai"); + $this->assertEquals($list[1]->getInternetEndpoint(), "oss-cn-shanghai.aliyuncs.com"); + $this->assertEquals($list[1]->getInternalEndpoint(), "oss-cn-shanghai-internal.aliyuncs.com"); + $this->assertEquals($list[1]->getAccelerateEndpoint(), "oss-accelerate.aliyuncs.com"); + } + + public function testParseValidXml1() + { + $response = new ResponseCore(array(), $this->validXml1, 200); + $result = new GetDescribeRegionsResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $result = $result->getData(); + $this->assertEquals(count($result->getRegionInfoList()), 1); + + $list = $result->getRegionInfoList(); + $this->assertEquals($list[0]->getRegion(), "oss-cn-hangzhou"); + $this->assertEquals($list[0]->getInternetEndpoint(), "oss-cn-hangzhou.aliyuncs.com"); + $this->assertEquals($list[0]->getInternalEndpoint(), "oss-cn-hangzhou-internal.aliyuncs.com"); + $this->assertEquals($list[0]->getAccelerateEndpoint(), "oss-accelerate.aliyuncs.com"); + } + + public function testParseInvalidXml2() + { + $response = new ResponseCore(array(), $this->invalidXml2, 200); + $result = new GetDescribeRegionsResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $this->assertNotNull($result->getRawResponse()->body); + + + $result = $result->getData(); + $this->assertNull($result->getRegionInfoList()); + } +} diff --git a/tests/OSS/Tests/OssClientDescribeRegionsTest.php b/tests/OSS/Tests/OssClientDescribeRegionsTest.php new file mode 100644 index 0000000..7e821f5 --- /dev/null +++ b/tests/OSS/Tests/OssClientDescribeRegionsTest.php @@ -0,0 +1,39 @@ +ossClient->getDescribeRegions(); + $this->assertNotNull($list->getRegionInfoList()); + $this->assertTrue(count($list->getRegionInfoList()) > 0); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + + try { + $options['regions'] = 'oss-cn-hangzhou'; + $result = $this->ossClient->getDescribeRegions($options); + $this->assertNotNull($result->getRegionInfoList()); + $this->assertTrue(count($result->getRegionInfoList()) > 0); + $list = $result->getRegionInfoList(); + $this->assertEquals($list[0]->getRegion(), "oss-cn-hangzhou"); + $this->assertEquals($list[0]->getRegion(), "oss-cn-hangzhou"); + $this->assertEquals($list[0]->getInternetEndpoint(), "oss-cn-hangzhou.aliyuncs.com"); + $this->assertEquals($list[0]->getInternalEndpoint(), "oss-cn-hangzhou-internal.aliyuncs.com"); + $this->assertEquals($list[0]->getAccelerateEndpoint(), "oss-accelerate.aliyuncs.com"); + } catch (OssException $e) { + $this->assertTrue(false); + } + } +}