Skip to content

Commit 8800630

Browse files
lewzylulewzylu
andauthored
Patch url token (#146)
* update select object * Add Demo * patch geturl with token * fix Test Co-authored-by: lewzylu <lewzylu@tencent.com>
1 parent 7153833 commit 8800630

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

src/Qcloud/Cos/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function __construct($cosConfig) {
6767
}));
6868
}
6969
$handler->push($this::handleErrors());
70-
$this->signature = new Signature($this->cosConfig['secretId'], $this->cosConfig['secretKey']);
70+
$this->signature = new Signature($this->cosConfig['secretId'], $this->cosConfig['secretKey'], $this->cosConfig['token']);
7171
$this->httpClient = new HttpClient([
7272
'base_uri' => $this->cosConfig['schema'].'://cos.' . $this->cosConfig['region'] . '.myqcloud.com/',
7373
'timeout' => $this->cosConfig['timeout'],

src/Qcloud/Cos/Signature.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
class Signature {
88
private $accessKey; // string: access key.
99
private $secretKey; // string: secret key.
10-
public function __construct($accessKey, $secretKey) {
10+
public function __construct($accessKey, $secretKey, $token=null) {
1111
$this->accessKey = $accessKey;
1212
$this->secretKey = $secretKey;
13+
$this->token = $token;
1314
date_default_timezone_set("PRC");
1415
}
1516
public function __destruct() {
@@ -34,7 +35,11 @@ public function createAuthorization(RequestInterface $request, $expires = "+30 m
3435
public function createPresignedUrl(RequestInterface $request, $expires = "+30 minutes") {
3536
$authorization = $this->createAuthorization($request, $expires);
3637
$uri = $request->getUri();
37-
$uri = $uri->withQuery("sign=".urlencode($authorization));
38+
$query = "sign=".urlencode($authorization);
39+
if ($this->token != null) {
40+
$query = $query."&x-cos-security-token=".$this->token;
41+
}
42+
$uri = $uri->withQuery($query);
3843
return $uri;
3944
}
4045
}

src/Qcloud/Cos/Tests/Test.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,4 +1627,57 @@ public function testPutObjectAclByBodyToAnyone()
16271627
}
16281628
}
16291629

1630+
/*
1631+
* selectobject,select检索数据
1632+
* 200
1633+
*/
1634+
public function testSelectObjectContent()
1635+
{
1636+
$key = '你好.txt';
1637+
try {
1638+
$body = "appid,bucket,region
1639+
12500001,22weqwe,sh
1640+
12500002,we2qwe,sh
1641+
12500003,weq3we,sh
1642+
12500004,weqw4e,sh
1643+
3278522,azxc,gz
1644+
4343,ewqew,tj";
1645+
$this->cosClient->putObject(array('Bucket' => $this->bucket,'Key' => $key, 'Body' => $body));
1646+
$result = $this->cosClient->selectObjectContent(array(
1647+
'Bucket' => $this->bucket, //格式:BucketName-APPID
1648+
'Key' => $key,
1649+
'Expression' => 'Select * from COSObject s',
1650+
'ExpressionType' => 'SQL',
1651+
'InputSerialization' => array(
1652+
'CompressionType' => 'None',
1653+
'CSV' => array(
1654+
'FileHeaderInfo' => 'USE',
1655+
'RecordDelimiter' => '\n',
1656+
'FieldDelimiter' => ',',
1657+
'QuoteEscapeCharacter' => '"',
1658+
'Comments' => '#',
1659+
'AllowQuotedRecordDelimiter' => 'FALSE'
1660+
)
1661+
),
1662+
'OutputSerialization' => array(
1663+
'CSV' => array(
1664+
'QuoteField' => 'ASNEEDED',
1665+
'RecordDelimiter' => '\n',
1666+
'FieldDelimiter' => ',',
1667+
'QuoteCharacter' => '"',
1668+
'QuoteEscapeCharacter' => '"'
1669+
)
1670+
),
1671+
'RequestProgress' => array(
1672+
'Enabled' => 'FALSE'
1673+
)
1674+
));
1675+
foreach ($result['Data'] as $data) {
1676+
}
1677+
} catch (\Exception $e) {
1678+
print ($e);
1679+
$this->assertFalse(TRUE);
1680+
}
1681+
}
1682+
16301683
}

0 commit comments

Comments
 (0)