Skip to content

Commit 0225cce

Browse files
lewzylulewzylu
andauthored
Feature signature params (#161)
* update signature * del old file Co-authored-by: lewzylu <[email protected]>
1 parent 833b5c7 commit 0225cce

File tree

5 files changed

+216
-306
lines changed

5 files changed

+216
-306
lines changed

src/Qcloud/Cos/Client.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
use GuzzleHttp\Command\Guzzle\Deserializer;
1515
use GuzzleHttp\Command\CommandInterface;
1616
use GuzzleHttp\Command\Exception\CommandException;
17-
use GuzzleHttp\Exception\RequestException;
1817
use GuzzleHttp\Middleware;
1918
use GuzzleHttp\Psr7;
20-
use GuzzleHttp\Pool;
21-
2219

2320
class Client extends GuzzleClient {
2421
const VERSION = '2.0.9';
@@ -46,7 +43,7 @@ public function __construct($cosConfig) {
4643
$this->cosConfig['connect_timeout'] = isset($cosConfig['connect_timeout']) ? $cosConfig['connect_timeout'] : 3600;
4744
$this->cosConfig['ip'] = isset($cosConfig['ip']) ? $cosConfig['ip'] : null;
4845
$this->cosConfig['port'] = isset($cosConfig['port']) ? $cosConfig['port'] : null;
49-
$this->cosConfig['endpoint'] = isset($cosConfig['endpoint']) ? $cosConfig['endpoint'] : 'myqcloud.com';
46+
$this->cosConfig['endpoint'] = isset($cosConfig['endpoint']) ? $cosConfig['endpoint'] : null;
5047
$this->cosConfig['domain'] = isset($cosConfig['domain']) ? $cosConfig['domain'] : null;
5148
$this->cosConfig['proxy'] = isset($cosConfig['proxy']) ? $cosConfig['proxy'] : null;
5249
$this->cosConfig['retry'] = isset($cosConfig['retry']) ? $cosConfig['retry'] : 1;

src/Qcloud/Cos/CommandToRequestTransformer.php

Lines changed: 102 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,162 +2,175 @@
22

33
namespace Qcloud\Cos;
44

5-
use Guzzle\Service\Description\Parameter;
6-
use Guzzle\Service\Description\ServiceDescription;
7-
use GuzzleHttp\HandlerStack;
85
use Psr\Http\Message\RequestInterface;
9-
use Psr\Http\Message\ResponseInterface;
10-
use Qcloud\Cos\Signature;
11-
use GuzzleHttp\Command\Guzzle\Description;
12-
use GuzzleHttp\Command\Guzzle\GuzzleClient;
136
use GuzzleHttp\Command\CommandInterface;
14-
use GuzzleHttp\Exception\RequestException;
15-
use GuzzleHttp\Middleware;
16-
use GuzzleHttp\Psr7;
177
use GuzzleHttp\Psr7\Uri;
188
use InvalidArgumentException;
199

20-
2110
class CommandToRequestTransformer {
2211
private $config;
2312
private $operation;
2413

25-
public function __construct($config ,$operation) {
14+
public function __construct( $config, $operation ) {
2615
$this->config = $config;
2716
$this->operation = $operation;
2817
}
2918

3019
// format bucket style
31-
public function bucketStyleTransformer(CommandInterface $command, RequestInterface $request) {
20+
21+
public function bucketStyleTransformer( CommandInterface $command, RequestInterface $request ) {
3222
$action = $command->getName();
3323
if ($action == 'ListBuckets') {
34-
return $request->withUri(new Uri($this->config['schema']."://service.cos.myqcloud.com/"));
24+
$uri = "service.cos.myqcloud.com";
25+
26+
if ($this->config['endpoint'] != null) {
27+
$uri = $this->config['endpoint'];
28+
}
29+
if ($this->config['domain'] != null) {
30+
$uri = $this->config['domain'];
31+
}
32+
if ($this->config['ip'] != null) {
33+
$uri = $this->config['ip'];
34+
if ($this->config['port'] != null) {
35+
$uri = $this->config['ip'] . ":" . $this->config['port'];
36+
}
37+
}
38+
return $request->withUri(new Uri($this->config['schema']."://". $uri. "/"));
3539
}
3640
$operation = $this->operation;
3741
$bucketname = $command['Bucket'];
3842

3943
$appId = $this->config['appId'];
40-
if ($appId != null && endWith($bucketname, '-'.$appId) == False)
41-
{
44+
if ( $appId != null && endWith( $bucketname, '-'.$appId ) == False ) {
4245
$bucketname = $bucketname.'-'.$appId;
4346
}
4447
$command['Bucket'] = $bucketname;
45-
$path = '';
48+
$path = '';
49+
4650
$http_method = $operation['httpMethod'];
4751
$uri = $operation['uri'];
48-
52+
4953
// Hoststyle is used by default
5054
// Pathstyle
51-
if ($this->config['pathStyle'] != true) {
52-
if (isset($operation['parameters']['Bucket']) && $command->hasParam('Bucket')) {
53-
$uri = str_replace("{Bucket}", '', $uri);
54-
}
55-
if (isset($operation['parameters']['Key']) && $command->hasParam('Key')) {
56-
$uri = str_replace("{/Key*}", encodeKey($command['Key']), $uri);
55+
if ( $this->config['pathStyle'] != true ) {
56+
if ( isset( $operation['parameters']['Bucket'] ) && $command->hasParam( 'Bucket' ) ) {
57+
$uri = str_replace( '{Bucket}', '', $uri );
58+
}
59+
60+
if ( isset( $operation['parameters']['Key'] ) && $command->hasParam( 'Key' ) ) {
61+
$uri = str_replace( '{/Key*}', encodeKey( $command['Key'] ), $uri );
5762
}
5863
}
64+
if ($this->config['endpoint'] == null) {
65+
$this->config['endpoint'] = "myqcloud.com";
66+
}
5967
$origin_host = $bucketname. '.cos.' . $this->config['region'] . '.' . $this->config['endpoint'];
6068
// domain
61-
if ($this->config['domain'] != null) {
69+
if ( $this->config['domain'] != null ) {
6270
$origin_host = $this->config['domain'];
6371
}
6472
$host = $origin_host;
65-
if ($this->config['ip'] != null) {
73+
if ( $this->config['ip'] != null ) {
6674
$host = $this->config['ip'];
67-
if ($this->config['port'] != null) {
68-
$host = $this->config['ip'] . ":" . $this->config['port'];
75+
if ( $this->config['port'] != null ) {
76+
$host = $this->config['ip'] . ':' . $this->config['port'];
6977
}
7078
}
7179

72-
7380
$path = $this->config['schema'].'://'. $host . $uri;
74-
$uri = new Uri($path);
81+
$uri = new Uri( $path );
7582
$query = $request->getUri()->getQuery();
76-
if ($uri->getQuery() != $query && $uri->getQuery() != "") {
77-
$query = $uri->getQuery() . "&" . $request->getUri()->getQuery();
83+
if ( $uri->getQuery() != $query && $uri->getQuery() != '' ) {
84+
$query = $uri->getQuery() . '&' . $request->getUri()->getQuery();
7885
}
79-
$uri = $uri->withQuery($query);
80-
$request = $request->withUri($uri);
81-
$request = $request->withHeader('Host', $origin_host);
86+
$uri = $uri->withQuery( $query );
87+
$request = $request->withUri( $uri );
88+
$request = $request->withHeader( 'Host', $origin_host );
8289
return $request;
8390
}
8491

8592
// format upload body
86-
public function uploadBodyTransformer(CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile') {
87-
93+
94+
public function uploadBodyTransformer( CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile' ) {
95+
8896
$operation = $this->operation;
89-
if (!isset($operation['parameters']['Body'])) {
97+
if ( !isset( $operation['parameters']['Body'] ) ) {
9098
return $request;
9199
}
92-
$source = isset($command[$sourceParameter]) ? $command[$sourceParameter] : null;
93-
$body = isset($command[$bodyParameter]) ? $command[$bodyParameter] : null;
100+
$source = isset( $command[$sourceParameter] ) ? $command[$sourceParameter] : null;
101+
$body = isset( $command[$bodyParameter] ) ? $command[$bodyParameter] : null;
94102
// If a file path is passed in then get the file handle
95-
if (is_string($source) && file_exists($source)) {
96-
$body = fopen($source, 'rb');
103+
if ( is_string( $source ) && file_exists( $source ) ) {
104+
$body = fopen( $source, 'rb' );
97105
}
98106
// Prepare the body parameter and remove the source file parameter
99-
if (null !== $body) {
107+
if ( null !== $body ) {
100108
return $request;
101109
} else {
102110
throw new InvalidArgumentException(
103-
"You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters.");
111+
"You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters." );
112+
}
104113
}
105-
}
106114

107-
// update md5
108-
public function md5Transformer(CommandInterface $command, $request) {
109-
$operation = $this->operation;
110-
if (isset($operation['data']['contentMd5'])) {
111-
$request = $this->addMd5($request);
115+
// update md5
116+
117+
public function md5Transformer( CommandInterface $command, $request ) {
118+
$operation = $this->operation;
119+
if ( isset( $operation['data']['contentMd5'] ) ) {
120+
$request = $this->addMd5( $request );
121+
}
122+
if ( isset( $operation['parameters']['ContentMD5'] ) &&
123+
isset( $command['ContentMD5'] ) ) {
124+
$value = $command['ContentMD5'];
125+
if ( $value === true ) {
126+
$request = $this->addMd5( $request );
127+
}
128+
}
129+
130+
return $request;
112131
}
113-
if (isset($operation['parameters']['ContentMD5']) &&
114-
isset($command['ContentMD5'])) {
115-
$value = $command['ContentMD5'];
116-
if ($value === true) {
117-
$request = $this->addMd5($request);
132+
133+
// add meta
134+
135+
public function metadataTransformer( CommandInterface $command, $request ) {
136+
$operation = $this->operation;
137+
if ( isset( $command['Metadata'] ) ) {
138+
$meta = $command['Metadata'];
139+
foreach ( $meta as $key => $value ) {
140+
$request = $request->withHeader( 'x-cos-meta-' . $key, $value );
141+
}
118142
}
143+
$request = headersMap( $command, $request );
144+
145+
return $request;
119146
}
120147

121-
return $request;
122-
}
148+
// count md5
123149

124-
// add meta
125-
public function metadataTransformer(CommandInterface $command, $request) {
126-
$operation = $this->operation;
127-
if (isset($command['Metadata'])) {
128-
$meta = $command['Metadata'];
129-
foreach ($meta as $key => $value) {
130-
$request = $request->withHeader('x-cos-meta-' . $key, $value);
150+
private function addMd5( $request ) {
151+
$body = $request->getBody();
152+
if ( $body && $body->getSize() > 0 ) {
153+
$md5 = base64_encode( md5( $body, true ) );
154+
return $request->withHeader( 'Content-MD5', $md5 );
131155
}
156+
return $request;
132157
}
133-
$request = headersMap($command, $request);
134-
return $request;
135-
}
136158

137-
// count md5
138-
private function addMd5($request) {
139-
$body = $request->getBody();
140-
if ($body && $body->getSize() > 0) {
141-
$md5 = base64_encode(md5($body, true));
142-
return $request->withHeader('Content-MD5', $md5);
159+
// inventoryId
160+
161+
public function specialParamTransformer( CommandInterface $command, $request ) {
162+
$action = $command->getName();
163+
if ( $action == 'PutBucketInventory' ) {
164+
$id = $command['Id'];
165+
$uri = $request->getUri();
166+
$query = $uri->getQuery();
167+
$uri = $uri->withQuery( $query . '&Id='.$id );
168+
return $request->withUri( $uri );
169+
}
170+
return $request;
143171
}
144-
return $request;
145-
}
146172

147-
// inventoryId
148-
public function specialParamTransformer(CommandInterface $command, $request) {
149-
$action = $command->getName();
150-
if ($action == 'PutBucketInventory') {
151-
$id = $command['Id'];
152-
$uri = $request->getUri();
153-
$query = $uri->getQuery();
154-
$uri = $uri->withQuery($query . "&Id=".$id);
155-
return $request->withUri($uri);
173+
public function __destruct() {
156174
}
157-
return $request;
158-
}
159175

160-
public function __destruct() {
161176
}
162-
163-
}

src/Qcloud/Cos/Common.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,53 @@
22

33
namespace Qcloud\Cos;
44

5-
function region_map($region) {
6-
$regionmap = array('cn-east'=>'ap-shanghai',
7-
'cn-south'=>'ap-guangzhou',
8-
'cn-north'=>'ap-beijing-1',
9-
'cn-south-2'=>'ap-guangzhou-2',
10-
'cn-southwest'=>'ap-chengdu',
11-
'sg'=>'ap-singapore',
12-
'tj'=>'ap-beijing-1',
13-
'bj'=>'ap-beijing',
14-
'sh'=>'ap-shanghai',
15-
'gz'=>'ap-guangzhou',
16-
'cd'=>'ap-chengdu',
17-
'sgp'=>'ap-singapore');
18-
if (array_key_exists($region, $regionmap)) {
5+
function region_map( $region ) {
6+
$regionmap = array( 'cn-east'=>'ap-shanghai',
7+
'cn-south'=>'ap-guangzhou',
8+
'cn-north'=>'ap-beijing-1',
9+
'cn-south-2'=>'ap-guangzhou-2',
10+
'cn-southwest'=>'ap-chengdu',
11+
'sg'=>'ap-singapore',
12+
'tj'=>'ap-beijing-1',
13+
'bj'=>'ap-beijing',
14+
'sh'=>'ap-shanghai',
15+
'gz'=>'ap-guangzhou',
16+
'cd'=>'ap-chengdu',
17+
'sgp'=>'ap-singapore' );
18+
if ( array_key_exists( $region, $regionmap ) ) {
1919
return $regionmap[$region];
2020
}
2121
return $region;
2222
}
2323

24-
function encodeKey($key) {
25-
return str_replace('%2F', '/', rawurlencode($key));
24+
function encodeKey( $key ) {
25+
return str_replace( '%2F', '/', rawurlencode( $key ) );
2626
}
2727

28-
function endWith($haystack, $needle) {
29-
$length = strlen($needle);
30-
if($length == 0)
31-
{
28+
function endWith( $haystack, $needle ) {
29+
$length = strlen( $needle );
30+
if ( $length == 0 ) {
3231
return true;
3332
}
34-
return (substr($haystack, -$length) === $needle);
33+
return ( substr( $haystack, -$length ) === $needle );
3534
}
3635

37-
function headersMap($command, $request) {
36+
function startWith( $haystack, $needle ) {
37+
$length = strlen( $needle );
38+
if ( $length == 0 ) {
39+
return true;
40+
}
41+
return ( substr( $haystack, $length ) === $needle );
42+
}
43+
44+
function headersMap( $command, $request ) {
3845
$headermap = array(
39-
'TransferEncoding'=>'Transfer-Encoding',
40-
'ChannelId'=>'x-cos-channel-id'
46+
'TransferEncoding'=>'Transfer-Encoding',
47+
'ChannelId'=>'x-cos-channel-id'
4148
);
42-
foreach ($headermap as $key => $value) {
43-
if (isset($command[$key])) {
44-
$request = $request->withHeader($value, $command[$key]);
49+
foreach ( $headermap as $key => $value ) {
50+
if ( isset( $command[$key] ) ) {
51+
$request = $request->withHeader( $value, $command[$key] );
4552
}
4653
}
4754
return $request;

0 commit comments

Comments
 (0)