Region.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace Qiniu;
  3. use Qiniu\Http\Client;
  4. use Qiniu\Http\Error;
  5. use Qiniu\Http\Middleware\RetryDomainsMiddleware;
  6. use Qiniu\Http\RequestOptions;
  7. class Region
  8. {
  9. //源站上传域名
  10. public $srcUpHosts;
  11. //CDN加速上传域名
  12. public $cdnUpHosts;
  13. //资源管理域名
  14. public $rsHost;
  15. //资源列举域名
  16. public $rsfHost;
  17. //资源处理域名
  18. public $apiHost;
  19. //IOVIP域名
  20. public $iovipHost;
  21. // TTL
  22. public $ttl;
  23. //构造一个Region对象
  24. public function __construct(
  25. $srcUpHosts = array(),
  26. $cdnUpHosts = array(),
  27. $rsHost = "rs-z0.qiniuapi.com",
  28. $rsfHost = "rsf-z0.qiniuapi.com",
  29. $apiHost = "api.qiniuapi.com",
  30. $iovipHost = null,
  31. $ttl = null
  32. ) {
  33. $this->srcUpHosts = $srcUpHosts;
  34. $this->cdnUpHosts = $cdnUpHosts;
  35. $this->rsHost = $rsHost;
  36. $this->rsfHost = $rsfHost;
  37. $this->apiHost = $apiHost;
  38. $this->iovipHost = $iovipHost;
  39. $this->ttl = $ttl;
  40. }
  41. //华东机房
  42. public static function regionHuadong()
  43. {
  44. $regionHuadong = new Region(
  45. array("up.qiniup.com"),
  46. array('upload.qiniup.com'),
  47. 'rs-z0.qiniuapi.com',
  48. 'rsf-z0.qiniuapi.com',
  49. 'api.qiniuapi.com',
  50. 'iovip.qbox.me'
  51. );
  52. return $regionHuadong;
  53. }
  54. //华东机房内网上传
  55. public static function qvmRegionHuadong()
  56. {
  57. $qvmRegionHuadong = new Region(
  58. array("free-qvm-z0-xs.qiniup.com"),
  59. 'rs-z0.qiniuapi.com',
  60. 'rsf-z0.qiniuapi.com',
  61. 'api.qiniuapi.com',
  62. 'iovip.qbox.me'
  63. );
  64. return $qvmRegionHuadong;
  65. }
  66. //华北机房内网上传
  67. public static function qvmRegionHuabei()
  68. {
  69. $qvmRegionHuabei = new Region(
  70. array("free-qvm-z1-zz.qiniup.com"),
  71. "rs-z1.qiniuapi.com",
  72. "rsf-z1.qiniuapi.com",
  73. "api-z1.qiniuapi.com",
  74. "iovip-z1.qbox.me"
  75. );
  76. return $qvmRegionHuabei;
  77. }
  78. //华北机房
  79. public static function regionHuabei()
  80. {
  81. $regionHuabei = new Region(
  82. array('up-z1.qiniup.com'),
  83. array('upload-z1.qiniup.com'),
  84. "rs-z1.qiniuapi.com",
  85. "rsf-z1.qiniuapi.com",
  86. "api-z1.qiniuapi.com",
  87. "iovip-z1.qbox.me"
  88. );
  89. return $regionHuabei;
  90. }
  91. //华南机房
  92. public static function regionHuanan()
  93. {
  94. $regionHuanan = new Region(
  95. array('up-z2.qiniup.com'),
  96. array('upload-z2.qiniup.com'),
  97. "rs-z2.qiniuapi.com",
  98. "rsf-z2.qiniuapi.com",
  99. "api-z2.qiniuapi.com",
  100. "iovip-z2.qbox.me"
  101. );
  102. return $regionHuanan;
  103. }
  104. //华东2 机房
  105. public static function regionHuadong2()
  106. {
  107. return new Region(
  108. array('up-cn-east-2.qiniup.com'),
  109. array('upload-cn-east-2.qiniup.com'),
  110. "rs-cn-east-2.qiniuapi.com",
  111. "rsf-cn-east-2.qiniuapi.com",
  112. "api-cn-east-2.qiniuapi.com",
  113. "iovip-cn-east-2.qiniuio.com"
  114. );
  115. }
  116. //北美机房
  117. public static function regionNorthAmerica()
  118. {
  119. //北美机房
  120. $regionNorthAmerica = new Region(
  121. array('up-na0.qiniup.com'),
  122. array('upload-na0.qiniup.com'),
  123. "rs-na0.qiniuapi.com",
  124. "rsf-na0.qiniuapi.com",
  125. "api-na0.qiniuapi.com",
  126. "iovip-na0.qbox.me"
  127. );
  128. return $regionNorthAmerica;
  129. }
  130. //新加坡机房
  131. public static function regionSingapore()
  132. {
  133. //新加坡机房
  134. $regionSingapore = new Region(
  135. array('up-as0.qiniup.com'),
  136. array('upload-as0.qiniup.com'),
  137. "rs-as0.qiniuapi.com",
  138. "rsf-as0.qiniuapi.com",
  139. "api-as0.qiniuapi.com",
  140. "iovip-as0.qbox.me"
  141. );
  142. return $regionSingapore;
  143. }
  144. /*
  145. * GET /v4/query?ak=<ak>&bucket=<bucket>
  146. * @param string $ak
  147. * @param string $bucket
  148. * @param string $ucHost|null
  149. * @param array $backupUcHosts
  150. * @param int $retryTimes
  151. * @param RequestOptions|null $reqOpt
  152. * @return Response
  153. **/
  154. public static function queryRegion(
  155. $ak,
  156. $bucket,
  157. $ucHost = null,
  158. $backupUcHosts = array(),
  159. $retryTimes = 2,
  160. $reqOpt = null
  161. ) {
  162. $region = new Region();
  163. if (!$ucHost) {
  164. $ucHost = "https://" . Config::QUERY_REGION_HOST;
  165. }
  166. $url = $ucHost . '/v4/query' . "?ak=$ak&bucket=$bucket";
  167. if ($reqOpt == null) {
  168. $reqOpt = new RequestOptions();
  169. }
  170. $reqOpt->middlewares = array(
  171. new RetryDomainsMiddleware(
  172. $backupUcHosts,
  173. $retryTimes
  174. )
  175. );
  176. $ret = Client::get($url, array(), $reqOpt);
  177. if (!$ret->ok()) {
  178. return array(null, new Error($url, $ret));
  179. }
  180. $r = ($ret->body === null) ? array() : $ret->json();
  181. if (!is_array($r["hosts"]) || count($r["hosts"]) == 0) {
  182. return array(null, new Error($url, $ret));
  183. }
  184. // parse region;
  185. $regionHost = $r["hosts"][0];
  186. $region->cdnUpHosts = array_merge($region->cdnUpHosts, $regionHost['up']['domains']);
  187. $region->srcUpHosts = array_merge($region->srcUpHosts, $regionHost['up']['domains']);
  188. // set specific hosts
  189. $region->iovipHost = $regionHost['io']['domains'][0];
  190. if (isset($regionHost['rs']['domains']) && count($regionHost['rs']['domains']) > 0) {
  191. $region->rsHost = $regionHost['rs']['domains'][0];
  192. } else {
  193. $region->rsHost = Config::RS_HOST;
  194. }
  195. if (isset($regionHost['rsf']['domains']) && count($regionHost['rsf']['domains']) > 0) {
  196. $region->rsfHost = $regionHost['rsf']['domains'][0];
  197. } else {
  198. $region->rsfHost = Config::RSF_HOST;
  199. }
  200. if (isset($regionHost['api']['domains']) && count($regionHost['api']['domains']) > 0) {
  201. $region->apiHost = $regionHost['api']['domains'][0];
  202. } else {
  203. $region->apiHost = Config::API_HOST;
  204. }
  205. // set ttl
  206. $region->ttl = $regionHost['ttl'];
  207. return $region;
  208. }
  209. }