Skip to content

Commit 919fbb9

Browse files
authored
Feature ip port (#93)
* feature-ip-port * feature-ip-port
1 parent 89950dd commit 919fbb9

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

src/Qcloud/Cos/BucketStyleListener.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@ function endWith($haystack, $needle) {
1919
class BucketStyleListener implements EventSubscriberInterface {
2020

2121
private $appId; // string: application id.
22+
private $ip;
23+
private $port;
24+
private $ipport;
2225

23-
public function __construct($appId) {
26+
public function __construct($appId, $ip=null, $port=null) {
2427
$this->appId = $appId;
28+
$this->ip = $ip;
29+
$this->port = $port;
30+
$this->ipport = null;
31+
if ($ip != null) {
32+
$this->ipport = $ip;
33+
if ($port != null) {
34+
$this->ipport = $ip.":".$port;
35+
}
36+
}
2537
}
2638

2739
public static function getSubscribedEvents() {
@@ -33,13 +45,19 @@ public static function getSubscribedEvents() {
3345
* @param Event $event Event emitted.
3446
*/
3547
public function onCommandAfterPrepare(Event $event) {
48+
3649
$command = $event['command'];
3750
$bucket = $command['Bucket'];
3851
$request = $command->getRequest();
39-
4052
if ($command->getName() == 'ListBuckets')
4153
{
42-
$request->setHost('service.cos.myqcloud.com');
54+
if ($this->ipport != null) {
55+
$request->setHost($this->ipport);
56+
$request->setHeader('Host', 'service.cos.myqcloud.com');
57+
} else {
58+
59+
$request->setHost('service.cos.myqcloud.com');
60+
}
4361
return ;
4462
}
4563
if ($key = $command['Key']) {
@@ -55,12 +73,16 @@ public function onCommandAfterPrepare(Event $event) {
5573
{
5674
$bucket = $bucket.'-'.$this->appId;
5775
}
58-
// Set the key and bucket on the request
76+
$request->setPath(urldecode($request->getPath()));
5977
$request->getParams()->set('bucket', $bucket)->set('key', $key);
6078

61-
//$request->setPath(urldecode($request->getPath()));
62-
// Switch to virtual hosted bucket
63-
$request->setHost($bucket. '.' . $request->getHost());
79+
$realHost = $bucket. '.' . $request->getHost();
80+
if($this->ipport != null) {
81+
$request->setHost($this->ipport);
82+
$request->setHeader('Host', $realHost);
83+
} else {
84+
$request->setHost($realHost);
85+
}
6486
if (!$bucket) {
6587
$request->getParams()->set('cos.resource', '/');
6688
} else {

src/Qcloud/Cos/Client.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Client extends GSClient {
2323
private $connect_timeout; // int: connect_timeout
2424
private $signature;
2525
private $schema;
26+
private $ip;
27+
private $port;
2628

2729

2830
public function __construct($config) {
@@ -40,6 +42,8 @@ public function __construct($config) {
4042
'cd'=>'ap-chengdu',
4143
'sgp'=>'ap-singapore',);
4244
$this->schema = isset($config['schema']) ? $config['schema'] : 'http';
45+
$this->ip = isset($config['ip']) ? $config['ip'] : null;
46+
$this->port = isset($config['port']) ? $config['port'] : null;
4347
$this->region = isset($regionmap[$this->region]) ? $regionmap[$this->region] : $this->region;
4448
$this->credentials = $config['credentials'];
4549
$this->appId = isset($config['credentials']['appId']) ? $config['credentials']['appId'] : null;
@@ -60,7 +64,7 @@ public function __construct($config) {
6064
$this->addSubscriber(new Md5Listener($this->signature));
6165
$this->addSubscriber(new TokenListener($this->token));
6266
$this->addSubscriber(new SignatureListener($this->secretId, $this->secretKey));
63-
$this->addSubscriber(new BucketStyleListener($this->appId));
67+
$this->addSubscriber(new BucketStyleListener($this->appId, $this->ip, $this->port));
6468
// Allow for specifying bodies with file paths and file handles
6569
$this->addSubscriber(new UploadBodyListener(array('PutObject', 'UploadPart')));
6670
}
@@ -167,7 +171,7 @@ public function Copy($bucket, $key, $copysource, $options = array()) {
167171
if (!key_exists('VersionId',$options['params'])) {
168172
$sourceversion = "";
169173
}
170-
else{
174+
else {
171175
$sourceversion = $options['params']['VersionId'];
172176
}
173177
$rt = $cosClient->headObject(array('Bucket'=>$sourcebucket,

0 commit comments

Comments
 (0)