Skip to content

Commit ad1b827

Browse files
committed
get iohost in bucket manager
1 parent 8e17714 commit ad1b827

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

src/Qiniu/Auth.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public function __construct($accessKey, $secretKey)
1515
$this->secretKey = $secretKey;
1616
}
1717

18+
public function getAccessKey()
19+
{
20+
return $this->accessKey;
21+
}
22+
1823
public function sign($data)
1924
{
2025
$hmac = hash_hmac('sha1', $data, $this->secretKey, true);
@@ -72,7 +77,8 @@ public function uploadToken(
7277
$key = null,
7378
$expires = 3600,
7479
$policy = null,
75-
$strictPolicy = true
80+
$strictPolicy = true,
81+
Zone $zone = null
7682
) {
7783
$deadline = time() + $expires;
7884
$scope = $bucket;
@@ -84,7 +90,10 @@ public function uploadToken(
8490
$args['scope'] = $scope;
8591
$args['deadline'] = $deadline;
8692

87-
$zone = new Zone();
93+
if ($zone === null)
94+
{
95+
$zone = new Zone();
96+
}
8897
$args['upHosts'] = $zone->getUpHosts($this->accessKey, $bucket);
8998
$b = json_encode($args);
9099
return $this->signWithData($b);

src/Qiniu/Storage/BucketManager.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Qiniu\Auth;
55
use Qiniu\Config;
6+
use Qiniu\Zone;
67
use Qiniu\Http\Client;
78
use Qiniu\Http\Error;
89

@@ -14,10 +15,15 @@
1415
final class BucketManager
1516
{
1617
private $auth;
18+
private $zone;
1719

18-
public function __construct(Auth $auth)
20+
public function __construct(Auth $auth, Zone $zone = null)
1921
{
2022
$this->auth = $auth;
23+
if ($zone === null)
24+
{
25+
$this->zone = new Zone();
26+
}
2127
}
2228

2329
/**
@@ -206,7 +212,12 @@ public function fetch($url, $bucket, $key = null)
206212
$resource = \Qiniu\base64_urlSafeEncode($url);
207213
$to = \Qiniu\entry($bucket, $key);
208214
$path = '/fetch/' . $resource . '/to/' . $to;
209-
return $this->ioPost($path);
215+
216+
$ak = $this->auth->getAccessKey();
217+
$ioHost = $this->zone->getIoHost($ak, $bucket);
218+
219+
$url = $ioPost . $path;
220+
return $this->post($url, null);
210221
}
211222

212223
/**
@@ -222,7 +233,12 @@ public function prefetch($bucket, $key)
222233
{
223234
$resource = \Qiniu\entry($bucket, $key);
224235
$path = '/prefetch/' . $resource;
225-
list(, $error) = $this->ioPost($path);
236+
237+
$ak = $this->auth->getAccessKey();
238+
$ioHost = $this->zone->getIoHost($ak, $bucket);
239+
240+
$url = $ioPost . $path;
241+
list(, $error) = $this->post($url, null);
226242
return $error;
227243
}
228244

src/Qiniu/Storage/FormUploader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static function putFile(
9999
$fields['key'] = $key;
100100
$headers =array('Content-Type' => 'multipart/form-data');
101101

102-
$upHost = $config->zone->getBucketHostsByUpToken($upToken);
102+
$upHost = $config->zone->getUpHostByToken($upToken);
103103
$response = client::post($upHost, $fields, $headers);
104104
if (!$response->ok()) {
105105
return array(null, new Error($upHost, $response));

src/Qiniu/Storage/UploadManager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace Qiniu\Storage;
33

44
use Qiniu\Config;
5-
use Qiniu\Zone;
65
use Qiniu\Http\HttpClient;
76
use Qiniu\Storage\ResumeUploader;
87
use Qiniu\Storage\FormUploader;

src/Qiniu/Zone.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,34 @@ public function __construct($scheme = null)
2525
public function getUpHostByToken($uptoken)
2626
{
2727
list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
28-
$upHosts = $this->getUpHosts($ak, $bucket);
28+
list($upHosts,) = $this->getUpHosts($ak, $bucket);
2929
return $upHosts[0];
3030
}
3131

3232
public function getBackupUpHostByToken($uptoken)
3333
{
3434
list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
35-
$upHosts = $this->getUpHosts($ak, $bucket);
35+
list($upHosts,) = $this->getUpHosts($ak, $bucket);
3636
return $upHosts[1];
3737
}
3838

39-
public function getUpHosts($ak, $bucket)
39+
public function getIoHost($ak, $bucket)
4040
{
41-
$bucketHosts = $this->getBucketHosts($ak, $bucket);
42-
$upHosts = $bucketHosts['upHosts'];
43-
return $upHosts;
41+
list($bucketHosts, ) = $this->getBucketHosts($ak, $bucket);
42+
$ioHost = $bucketHosts['ioHost'];
43+
return $upHost;
4444
}
4545

46-
public function getBucketHostsByUpToken($uptoken)
46+
public function getUpHosts($ak, $bucket)
4747
{
48-
list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
49-
return $this->getBucketHosts($ak, $bucket);
48+
list($bucketHosts, $err) = $this->getBucketHosts($ak, $bucket);
49+
if ($err !== null)
50+
{
51+
return array(null, $err);
52+
}
53+
54+
$upHosts = $bucketHosts['upHosts'];
55+
return array($upHosts, null);
5056
}
5157

5258
private function unmarshalUpToken($uptoken)
@@ -61,7 +67,11 @@ private function unmarshalUpToken($uptoken)
6167
$policy = base64_urlSafeDecode($token[2]);
6268
$policy = json_decode($policy, true);
6369

64-
list($bucket, $_) = split(':', $policy['scope']);
70+
$bucket = $policy['scope'];
71+
if (strpos($bucket, ':'))
72+
{
73+
$bucket = split(':', $bucket)[0];
74+
}
6575

6676
return array($ak, $bucket);
6777
}
@@ -81,14 +91,17 @@ public function getBucketHosts($ak, $bucket)
8191
return $this->hostCache[$key];
8292
}
8393

84-
list($hosts, $_) = $this->bucketHosts($ak, $bucket);
94+
list($hosts, $err) = $this->bucketHosts($ak, $bucket);
95+
if ($err !== null)
96+
{
97+
return array(null , $err);
98+
}
8599

86-
var_dump($hosts);
87100
$schemeHosts = $hosts[$this->scheme];
88101
$bucketHosts = array('upHosts' => $schemeHosts['up'], 'ioHost' => $schemeHosts['io'], 'deadline' => time() + $hosts['ttl']);
89102

90103
$this->hostCache[$key] = $bucketHosts;
91-
return $bucketHosts;
104+
return array($bucketHosts, null);
92105
}
93106

94107

0 commit comments

Comments
 (0)