From 2ddad537c357a42c406665fef775a9059467cc1e Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Tue, 18 Dec 2018 15:44:54 +0800 Subject: [PATCH 1/2] feature-ip-port --- src/Qcloud/Cos/BucketStyleListener.php | 35 ++++++++++++++++++++------ src/Qcloud/Cos/Client.php | 8 ++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/Qcloud/Cos/BucketStyleListener.php b/src/Qcloud/Cos/BucketStyleListener.php index a035d053..99687e25 100644 --- a/src/Qcloud/Cos/BucketStyleListener.php +++ b/src/Qcloud/Cos/BucketStyleListener.php @@ -19,9 +19,21 @@ function endWith($haystack, $needle) { class BucketStyleListener implements EventSubscriberInterface { private $appId; // string: application id. + private $ip; + private $port; + private $ipport; - public function __construct($appId) { + public function __construct($appId, $ip=null, $port=null) { $this->appId = $appId; + $this->ip = $ip; + $this->port = $port; + $this->ipport = null; + if ($ip != null) { + $this->ipport = $ip; + if ($port != null) { + $this->ipport = $ip.":".$port; + } + } } public static function getSubscribedEvents() { @@ -33,13 +45,19 @@ public static function getSubscribedEvents() { * @param Event $event Event emitted. */ public function onCommandAfterPrepare(Event $event) { + $command = $event['command']; $bucket = $command['Bucket']; $request = $command->getRequest(); - if ($command->getName() == 'ListBuckets') { - $request->setHost('service.cos.myqcloud.com'); + if ($this->ipport != null) { + $request->setHost($this->ipport); + $request->setHeader('Host', 'service.cos.myqcloud.com'); + } else { + + $request->setHost('service.cos.myqcloud.com'); + } return ; } if ($key = $command['Key']) { @@ -55,12 +73,15 @@ public function onCommandAfterPrepare(Event $event) { { $bucket = $bucket.'-'.$this->appId; } - // Set the key and bucket on the request $request->getParams()->set('bucket', $bucket)->set('key', $key); - //$request->setPath(urldecode($request->getPath())); - // Switch to virtual hosted bucket - $request->setHost($bucket. '.' . $request->getHost()); + $realHost = $bucket. '.' . $request->getHost(); + if($this->ipport != null) { + $request->setHost($this->ipport); + $request->setHeader('Host', $realHost); + } else { + $request->setHost($realHost); + } if (!$bucket) { $request->getParams()->set('cos.resource', '/'); } else { diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index ceffd70c..773f57a7 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -23,6 +23,8 @@ class Client extends GSClient { private $connect_timeout; // int: connect_timeout private $signature; private $schema; + private $ip; + private $port; public function __construct($config) { @@ -40,6 +42,8 @@ public function __construct($config) { 'cd'=>'ap-chengdu', 'sgp'=>'ap-singapore',); $this->schema = isset($config['schema']) ? $config['schema'] : 'http'; + $this->ip = isset($config['ip']) ? $config['ip'] : null; + $this->port = isset($config['port']) ? $config['port'] : null; $this->region = isset($regionmap[$this->region]) ? $regionmap[$this->region] : $this->region; $this->credentials = $config['credentials']; $this->appId = isset($config['credentials']['appId']) ? $config['credentials']['appId'] : null; @@ -60,7 +64,7 @@ public function __construct($config) { $this->addSubscriber(new Md5Listener($this->signature)); $this->addSubscriber(new TokenListener($this->token)); $this->addSubscriber(new SignatureListener($this->secretId, $this->secretKey)); - $this->addSubscriber(new BucketStyleListener($this->appId)); + $this->addSubscriber(new BucketStyleListener($this->appId, $this->ip, $this->port)); // Allow for specifying bodies with file paths and file handles $this->addSubscriber(new UploadBodyListener(array('PutObject', 'UploadPart'))); } @@ -167,7 +171,7 @@ public function Copy($bucket, $key, $copysource, $options = array()) { if (!key_exists('VersionId',$options['params'])) { $sourceversion = ""; } - else{ + else { $sourceversion = $options['params']['VersionId']; } $rt = $cosClient->headObject(array('Bucket'=>$sourcebucket, From b90877ac25b042a3d8f2863bf6f952e611170226 Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Mon, 28 Jan 2019 16:21:13 +0800 Subject: [PATCH 2/2] feature-ip-port --- src/Qcloud/Cos/BucketStyleListener.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Qcloud/Cos/BucketStyleListener.php b/src/Qcloud/Cos/BucketStyleListener.php index 99687e25..63f8fefb 100644 --- a/src/Qcloud/Cos/BucketStyleListener.php +++ b/src/Qcloud/Cos/BucketStyleListener.php @@ -73,6 +73,7 @@ public function onCommandAfterPrepare(Event $event) { { $bucket = $bucket.'-'.$this->appId; } + $request->setPath(urldecode($request->getPath())); $request->getParams()->set('bucket', $bucket)->set('key', $key); $realHost = $bucket. '.' . $request->getHost();