Skip to content

Commit 9f2de65

Browse files
committed
feat 云处理、Rest API 使用新的签名方式
1 parent 24baaf3 commit 9f2de65

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

src/Upyun/Api/Pretreat.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ public function process($source, $tasks) {
3131
'accept' => 'json'
3232
);
3333

34-
$url = $this->url . '/pretreatment';
35-
$signature = Signature::getSignature($this->config, $params, Signature::SIGN_VIDEO);
36-
$response = $client->request('POST', $url, [
37-
'headers' => array('Authorization' => "UPYUN {$this->config->operatorName}:$signature"),
34+
$path = '/pretreatment/';
35+
$method = 'POST';
36+
$signedHeaders = Signature::getHeaderSign($this->config, $method, $path);
37+
38+
$response = $client->request($method, $this->url . $path, [
39+
'headers' => $signedHeaders,
3840
'form_params' => $params
3941
]);
4042

@@ -49,15 +51,16 @@ public function query($taskIds, $path) {
4951
]);
5052

5153
$params = array(
52-
'bucket_name' => $this->config->bucketName,
54+
'service' => $this->config->bucketName,
5355
'task_ids' => implode(',', $taskIds)
5456
);
57+
$path = $path . '?' . http_build_query($params);
5558

59+
$method = 'GET';
5660
$url = $this->url . $path;
57-
$signature = Signature::getSignature($this->config, $params, Signature::SIGN_VIDEO);
58-
$response = $client->request('GET', $url, [
59-
'headers' => array('Authorization' => "UPYUN {$this->config->operatorName}:$signature"),
60-
'query' => $params
61+
$signedHeaders = Signature::getHeaderSign($this->config, $method, $path);
62+
$response = $client->request($method, $url, [
63+
'headers' => $signedHeaders
6164
]);
6265

6366
if ($response->getStatusCode() === 200) {
@@ -69,4 +72,4 @@ public function query($taskIds, $path) {
6972
}
7073
return false;
7174
}
72-
}
75+
}

src/Upyun/Api/Rest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(Config $config) {
2828
$this->config = $config;
2929
$this->endpoint = Config::$restApiEndPoint . '/' . $config->bucketName;
3030
}
31-
31+
3232
public function request($method, $storagePath) {
3333
$this->method = strtoupper($method);
3434
$this->storagePath = '/' . ltrim($storagePath, '/');
@@ -57,14 +57,14 @@ public function send() {
5757
]);
5858

5959
$url = ($this->config->useSsl ? 'https://' : 'http://') . $this->endpoint . $this->storagePath;
60-
$bodySize = 0;
6160
$body = null;
6261
if($this->file && $this->method === 'PUT') {
63-
$bodySize = $this->file->getSize();
6462
$body = $this->file;
6563
}
66-
67-
$authHeader = Signature::getRestApiSignHeader($this->config, $this->method, $this->storagePath, $bodySize);
64+
// TODO urlencode path
65+
$path = '/' . $this->config->bucketName . '/' . ltrim($this->storagePath, '/');
66+
67+
$authHeader = Signature::getHeaderSign($this->config, $this->method, $path);
6868
$response = $client->request($this->method, $url, [
6969
'headers' => array_merge($authHeader, $this->headers),
7070
'body' => $body

src/Upyun/Signature.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,34 @@ class Signature {
2121
const SIGN_VIDEO_NO_OPERATOR = 3;
2222

2323
/**
24-
* 获取 RESET API 请求需要的签名头
24+
* 获取 Header 签名
2525
*
2626
* @param Config $bucketConfig
2727
* @param $method
28-
* @param $path
29-
* @param $contentLength
28+
* @param $path 请求路径
29+
* @param $contentMd5 文件内容 md5
3030
*
3131
* @return array
3232
*/
33-
public static function getRestApiSignHeader($bucketConfig, $method, $remotePath, $contentLength) {
33+
public static function getHeaderSign($bucketConfig, $method, $path, $contentMd5 = null) {
3434
$gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
35-
$path = '/' . $bucketConfig->bucketName . '/' . ltrim($remotePath, '/');
3635

37-
$sign = md5("$method&$path&$gmtDate&$contentLength&{$bucketConfig->operatorPassword}");
36+
$signParams = array(
37+
$method,
38+
$path,
39+
$gmtDate
40+
);
41+
42+
if ($contentMd5) {
43+
$signParams[] = $contentMd5;
44+
}
45+
46+
$sign = self::calcSignature($bucketConfig, $signParams);
3847

3948
$headers = array(
40-
'Authorization' => "UpYun {$bucketConfig->operatorName}:$sign",
49+
'Authorization' => "UPYUN {$bucketConfig->operatorName}:$sign",
4150
'Date' => $gmtDate,
42-
'User-agent' => 'Php-Sdk/' . $bucketConfig->getVersion() . ' (rest api)'
51+
'User-agent' => 'Php-Sdk/' . $bucketConfig->getVersion()
4352
);
4453
return $headers;
4554
}
@@ -62,6 +71,13 @@ public static function getPurgeSignHeader(Config $bucketConfig, $urlString) {
6271
);
6372
}
6473

74+
/**
75+
* 获取表单 API 需要的签名,依据 body 签名规则计算
76+
* @param Config $bucketConfig
77+
* @param $data
78+
*
79+
* @return array
80+
*/
6581
public static function getFormSignature(Config $bucketConfig, $data) {
6682
$data['bucket'] = $bucketConfig->bucketName;
6783
$policy = Util::base64Json($data);
@@ -78,14 +94,18 @@ public static function getFormSignature(Config $bucketConfig, $data) {
7894
$signParams['md5'] = $data['content-md5'];
7995
};
8096

81-
$signature = base64_encode(hash_hmac('sha1', implode('&', $signParams), $bucketConfig->operatorPassword, true));
97+
$signature = self::calcSignature($bucketConfig, $signParams);
8298
return array(
8399
'policy' => $policy,
84100
'signature' => $signature
85101
);
86102
}
87103

88-
public static function getSignature( Config $bucketConfig, $data, $type, $tokenSecret = '') {
104+
private static function calcSignature(Config $bucketConfig, $signParams) {
105+
return base64_encode(hash_hmac('sha1', implode('&', $signParams), $bucketConfig->operatorPassword, true));
106+
}
107+
108+
public static function getSignature(Config $bucketConfig, $data, $type, $tokenSecret = '') {
89109
if (is_array($data)) {
90110
ksort($data);
91111
$string = '';

src/Upyun/Upyun.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public function process($source, $tasks) {
320320
*/
321321
public function queryProcessStatus($taskIds) {
322322
$video = new Api\Pretreat($this->config);
323-
return $video->query($taskIds, '/status');
323+
return $video->query($taskIds, '/status/');
324324
}
325325

326326
/**
@@ -346,6 +346,6 @@ public function queryProcessStatus($taskIds) {
346346
*/
347347
public function queryProcessResult($taskIds) {
348348
$video = new Api\Pretreat($this->config);
349-
return $video->query($taskIds, '/result');
349+
return $video->query($taskIds, '/result/');
350350
}
351351
}

0 commit comments

Comments
 (0)