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
54 changes: 54 additions & 0 deletions sample/selectObjectContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
$secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
$region = "ap-beijing"; //设置一个默认的存储桶地域
$cosClient = new Qcloud\Cos\Client(array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey
)
));
try {
$result = $cosClient->selectObjectContent(array(
'Bucket' => $bucket, //格式:BucketName-APPID
'Key' => $key,
'Expression' => 'Select * from COSObject s',
'ExpressionType' => 'SQL',
'InputSerialization' => array(
'CompressionType' => 'None',
'CSV' => array(
'FileHeaderInfo' => 'NONE',
'RecordDelimiter' => '\n',
'FieldDelimiter' => ',',
'QuoteEscapeCharacter' => '"',
'Comments' => '#',
'AllowQuotedRecordDelimiter' => 'FALSE'
)
),
'OutputSerialization' => array(
'CSV' => array(
'QuoteField' => 'ASNEEDED',
'RecordDelimiter' => '\n',
'FieldDelimiter' => ',',
'QuoteCharacter' => '"',
'QuoteEscapeCharacter' => '"'
)
),
'RequestProgress' => array(
'Enabled' => 'FALSE'
)
));
// 请求成功
foreach ($result['Data'] as $data) {
// 迭代遍历select结果
print_r($data);
}
} catch (\Exception $e) {
// 请求失败
echo($e);
}
47 changes: 15 additions & 32 deletions src/Qcloud/Cos/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@


class Client extends GuzzleClient {
const VERSION = '2.0.6';
const VERSION = '2.0.7';

public $httpClient;

private $api;
private $desc;
private $action;
private $operation;
private $cosConfig;
private $signature;
private $rawCosConfig;
Expand Down Expand Up @@ -81,9 +83,9 @@ public function __construct($cosConfig) {

public function commandToRequestTransformer(CommandInterface $command)
{
$action = $command->GetName();
$opreation = $this->api[$action];
$transformer = new CosTransformer($this->cosConfig, $opreation);
$this->action = $command->GetName();
$this->operation = $this->api[$this->action];
$transformer = new CommandToRequestTransformer($this->cosConfig, $this->operation);
$seri = new Serializer($this->desc);
$request = $seri($command);
$request = $transformer->bucketStyleTransformer($command, $request);
Expand All @@ -96,36 +98,17 @@ public function commandToRequestTransformer(CommandInterface $command)

public function responseToResultTransformer(ResponseInterface $response, RequestInterface $request, CommandInterface $command)
{
$action = $command->getName();
if ($action == "GetObject") {
if (isset($command['SaveAs'])) {
$fp = fopen($command['SaveAs'], "wb");
fwrite($fp, $response->getBody());
fclose($fp);
}
}
$transformer = new ResultTransformer($this->cosConfig, $this->operation);
$transformer->writeDataToLocal($command, $request, $response);
$deseri = new Deserializer($this->desc, true);
$rsp = $deseri($response, $request, $command);
$result = $deseri($response, $request, $command);

$headers = $response->getHeaders();
$metadata = array();
foreach ($headers as $key => $value) {
if (strpos($key, "x-cos-meta-") === 0) {
$metadata[substr($key, 11)] = $value[0];
}
}
if (!empty($metadata)) {
$rsp['Metadata'] = $metadata;
}
if ($command['Key'] != null && $rsp['Key'] == null) {
$rsp['Key'] = $command['Key'];
}
if ($command['Bucket'] != null && $rsp['Bucket'] == null) {
$rsp['Bucket'] = $command['Bucket'];
}
$rsp['Location'] = $request->getHeader("Host")[0] . $request->getUri()->getPath();
return $rsp;
$result = $transformer->metaDataTransformer($command, $response, $result);
$result = $transformer->extraHeadersTransformer($command, $request, $result);
$result = $transformer->selectContentTransformer($command, $result);
return $result;
}

public function __destruct() {
}

Expand Down Expand Up @@ -264,7 +247,7 @@ public function doesObjectExist($bucket, $key, array $options = array())
return False;
}
}

public static function explodeKey($key) {
// Remove a leading slash if one is found
$split_key = explode('/', $key && $key[0] == '/' ? substr($key, 1) : $key);
Expand Down
162 changes: 162 additions & 0 deletions src/Qcloud/Cos/CommandToRequestTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

namespace Qcloud\Cos;

use Guzzle\Service\Description\Parameter;
use Guzzle\Service\Description\ServiceDescription;
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Qcloud\Cos\Signature;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Uri;
use InvalidArgumentException;


class CommandToRequestTransformer {
private $config;
private $operation;

public function __construct($config ,$operation) {
$this->config = $config;
$this->operation = $operation;
}

// format bucket style
public function bucketStyleTransformer(CommandInterface $command, RequestInterface $request) {
$action = $command->getName();
if ($action == 'ListBuckets') {
return $request->withUri(new Uri($this->config['schema']."://service.cos.myqcloud.com/"));
}
$operation = $this->operation;
$bucketname = $command['Bucket'];

$appId = $this->config['appId'];
if ($appId != null && endWith($bucketname, '-'.$appId) == False)
{
$bucketname = $bucketname.'-'.$appId;
}
$command['Bucket'] = $bucketname;
$path = '';
$http_method = $operation['httpMethod'];
$uri = $operation['uri'];

// Hoststyle is used by default
// Pathstyle
if ($this->config['pathStyle'] != true) {
if (isset($operation['parameters']['Bucket']) && $command->hasParam('Bucket')) {
$uri = str_replace("{Bucket}", '', $uri);
}
if (isset($operation['parameters']['Key']) && $command->hasParam('Key')) {
$uri = str_replace("{/Key*}", encodeKey($command['Key']), $uri);
}
}
$origin_host = $bucketname. '.cos.' . $this->config['region'] . '.' . $this->config['endpoint'];
// domain
if ($this->config['domain'] != null) {
$origin_host = $this->config['domain'];
}
$host = $origin_host;
if ($this->config['ip'] != null) {
$host = $this->config['ip'];
if ($this->config['port'] != null) {
$host = $this->config['ip'] . ":" . $this->config['port'];
}
}


$path = $this->config['schema'].'://'. $host . $uri;
$uri = new Uri($path);
$query = $request->getUri()->getQuery();
if ($uri->getQuery() != $query && $uri->getQuery() != "") {
$query = $uri->getQuery() . "&" . $request->getUri()->getQuery();
}
$uri = $uri->withQuery($query);
$request = $request->withUri($uri);
$request = $request->withHeader('Host', $origin_host);
return $request;
}

// format upload body
public function uploadBodyTransformer(CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile') {

$operation = $this->operation;
if (!isset($operation['parameters']['Body'])) {
return $request;
}
$source = isset($command[$sourceParameter]) ? $command[$sourceParameter] : null;
$body = isset($command[$bodyParameter]) ? $command[$bodyParameter] : null;
// If a file path is passed in then get the file handle
if (is_string($source) && file_exists($source)) {
$body = fopen($source, 'rb');
}
// Prepare the body parameter and remove the source file parameter
if (null !== $body) {
return $request;
} else {
throw new InvalidArgumentException(
"You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters.");
}
}

// update md5
public function md5Transformer(CommandInterface $command, $request) {
$operation = $this->operation;
if (isset($operation['data']['contentMd5'])) {
$request = $this->addMd5($request);
}
if (isset($operation['parameters']['ContentMD5']) &&
isset($command['ContentMD5'])) {
$value = $command['ContentMD5'];
if ($value === true) {
$request = $this->addMd5($request);
}
}

return $request;
}

// add meta
public function metadataTransformer(CommandInterface $command, $request) {
$operation = $this->operation;
if (isset($command['Metadata'])) {
$meta = $command['Metadata'];
foreach ($meta as $key => $value) {
$request = $request->withHeader('x-cos-meta-' . $key, $value);
}
}
return $request;
}

// count md5
private function addMd5($request) {
$body = $request->getBody();
if ($body && $body->getSize() > 0) {
$md5 = base64_encode(md5($body, true));
return $request->withHeader('Content-MD5', $md5);
}
return $request;
}

// inventoryId
public function specialParamTransformer(CommandInterface $command, $request) {
$action = $command->getName();
if ($action == 'PutBucketInventory') {
$id = $command['Id'];
$uri = $request->getUri();
$query = $uri->getQuery();
$uri = $uri->withQuery($query . "&Id=".$id);
return $request->withUri($uri);
}
return $request;
}

public function __destruct() {
}

}
Loading