Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/Qcloud/Cos/BucketStyleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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']) {
Expand All @@ -55,12 +73,16 @@ public function onCommandAfterPrepare(Event $event) {
{
$bucket = $bucket.'-'.$this->appId;
}
// Set the key and bucket on the request
$request->setPath(urldecode($request->getPath()));
$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 {
Expand Down
8 changes: 6 additions & 2 deletions src/Qcloud/Cos/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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')));
}
Expand Down Expand Up @@ -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,
Expand Down