Region.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Qiniu;
  3. use Qiniu\Http\Client;
  4. use Qiniu\Http\Error;
  5. class Region
  6. {
  7. //源站上传域名
  8. public $srcUpHosts;
  9. //CDN加速上传域名
  10. public $cdnUpHosts;
  11. //资源管理域名
  12. public $rsHost;
  13. //资源列举域名
  14. public $rsfHost;
  15. //资源处理域名
  16. public $apiHost;
  17. //IOVIP域名
  18. public $iovipHost;
  19. //构造一个Region对象
  20. public function __construct(
  21. $srcUpHosts = array(),
  22. $cdnUpHosts = array(),
  23. $rsHost = "rs.qiniu.com",
  24. $rsfHost = "rsf.qiniu.com",
  25. $apiHost = "api.qiniu.com",
  26. $iovipHost = null
  27. ) {
  28. $this->srcUpHosts = $srcUpHosts;
  29. $this->cdnUpHosts = $cdnUpHosts;
  30. $this->rsHost = $rsHost;
  31. $this->rsfHost = $rsfHost;
  32. $this->apiHost = $apiHost;
  33. $this->iovipHost = $iovipHost;
  34. }
  35. //华东机房
  36. public static function regionHuadong()
  37. {
  38. $regionHuadong = new Region(
  39. array("up.qiniup.com", 'up-jjh.qiniup.com', 'up-xs.qiniup.com'),
  40. array('upload.qiniup.com', 'upload-jjh.qiniup.com', 'upload-xs.qiniup.com'),
  41. 'rs.qbox.me',
  42. 'rsf.qbox.me',
  43. 'api.qiniu.com',
  44. 'iovip.qbox.me'
  45. );
  46. return $regionHuadong;
  47. }
  48. //华东机房内网上传
  49. public static function qvmRegionHuadong()
  50. {
  51. $qvmRegionHuadong = new Region(
  52. array("free-qvm-z0-xs.qiniup.com"),
  53. 'rs.qbox.me',
  54. 'rsf.qbox.me',
  55. 'api.qiniu.com',
  56. 'iovip.qbox.me'
  57. );
  58. return $qvmRegionHuadong;
  59. }
  60. //华北机房内网上传
  61. public static function qvmRegionHuabei()
  62. {
  63. $qvmRegionHuabei = new Region(
  64. array("free-qvm-z1-zz.qiniup.com"),
  65. "rs-z1.qbox.me",
  66. "rsf-z1.qbox.me",
  67. "api-z1.qiniu.com",
  68. "iovip-z1.qbox.me"
  69. );
  70. return $qvmRegionHuabei;
  71. }
  72. //华北机房
  73. public static function regionHuabei()
  74. {
  75. $regionHuabei = new Region(
  76. array('up-z1.qiniup.com'),
  77. array('upload-z1.qiniup.com'),
  78. "rs-z1.qbox.me",
  79. "rsf-z1.qbox.me",
  80. "api-z1.qiniu.com",
  81. "iovip-z1.qbox.me"
  82. );
  83. return $regionHuabei;
  84. }
  85. //华南机房
  86. public static function regionHuanan()
  87. {
  88. $regionHuanan = new Region(
  89. array('up-z2.qiniup.com', 'up-dg.qiniup.com', 'up-fs.qiniup.com'),
  90. array('upload-z2.qiniup.com', 'upload-dg.qiniup.com', 'upload-fs.qiniup.com'),
  91. "rs-z2.qbox.me",
  92. "rsf-z2.qbox.me",
  93. "api-z2.qiniu.com",
  94. "iovip-z2.qbox.me"
  95. );
  96. return $regionHuanan;
  97. }
  98. //北美机房
  99. public static function regionNorthAmerica()
  100. {
  101. //北美机房
  102. $regionNorthAmerica = new Region(
  103. array('up-na0.qiniup.com'),
  104. array('upload-na0.qiniup.com'),
  105. "rs-na0.qbox.me",
  106. "rsf-na0.qbox.me",
  107. "api-na0.qiniu.com",
  108. "iovip-na0.qbox.me"
  109. );
  110. return $regionNorthAmerica;
  111. }
  112. //新加坡机房
  113. public static function regionSingapore()
  114. {
  115. //新加坡机房
  116. $regionSingapore = new Region(
  117. array('up-as0.qiniup.com'),
  118. array('upload-as0.qiniup.com'),
  119. "rs-as0.qbox.me",
  120. "rsf-as0.qbox.me",
  121. "api-as0.qiniu.com",
  122. "iovip-as0.qbox.me"
  123. );
  124. return $regionSingapore;
  125. }
  126. /*
  127. * GET /v2/query?ak=<ak>&&bucket=<bucket>
  128. **/
  129. public static function queryRegion($ak, $bucket)
  130. {
  131. $Region = new Region();
  132. $url = Config::API_HOST . '/v2/query' . "?ak=$ak&bucket=$bucket";
  133. $ret = Client::Get($url);
  134. if (!$ret->ok()) {
  135. return array(null, new Error($url, $ret));
  136. }
  137. $r = ($ret->body === null) ? array() : $ret->json();
  138. //parse Region;
  139. $iovipHost = $r['io']['src']['main'][0];
  140. $Region->iovipHost = $iovipHost;
  141. $accMain = $r['up']['acc']['main'][0];
  142. array_push($Region->cdnUpHosts, $accMain);
  143. if (isset($r['up']['acc']['backup'])) {
  144. foreach ($r['up']['acc']['backup'] as $key => $value) {
  145. array_push($Region->cdnUpHosts, $value);
  146. }
  147. }
  148. $srcMain = $r['up']['src']['main'][0];
  149. array_push($Region->srcUpHosts, $srcMain);
  150. if (isset($r['up']['src']['backup'])) {
  151. foreach ($r['up']['src']['backup'] as $key => $value) {
  152. array_push($Region->srcUpHosts, $value);
  153. }
  154. }
  155. //set specific hosts
  156. if (strstr($Region->iovipHost, "z1") !== false) {
  157. $Region->rsHost = "rs-z1.qbox.me";
  158. $Region->rsfHost = "rsf-z1.qbox.me";
  159. $Region->apiHost = "api-z1.qiniu.com";
  160. } elseif (strstr($Region->iovipHost, "z2") !== false) {
  161. $Region->rsHost = "rs-z2.qbox.me";
  162. $Region->rsfHost = "rsf-z2.qbox.me";
  163. $Region->apiHost = "api-z2.qiniu.com";
  164. } elseif (strstr($Region->iovipHost, "na0") !== false) {
  165. $Region->rsHost = "rs-na0.qbox.me";
  166. $Region->rsfHost = "rsf-na0.qbox.me";
  167. $Region->apiHost = "api-na0.qiniu.com";
  168. } elseif (strstr($Region->iovipHost, "as0") !== false) {
  169. $Region->rsHost = "rs-as0.qbox.me";
  170. $Region->rsfHost = "rsf-as0.qbox.me";
  171. $Region->apiHost = "api-as0.qiniu.com";
  172. } else {
  173. $Region->rsHost = "rs.qbox.me";
  174. $Region->rsfHost = "rsf.qbox.me";
  175. $Region->apiHost = "api.qiniu.com";
  176. }
  177. return $Region;
  178. }
  179. }