Skip to content

Commit b81ee82

Browse files
authored
Feature header (#124)
* update service * update interface of copy * update UPGRADE * update Test * update sample
1 parent d0c6053 commit b81ee82

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

sample/getObjectUrl.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
require dirname(__FILE__) . '/../vendor/autoload.php';
4+
5+
$secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
6+
$secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
7+
$region = "ap-beijing"; //设置一个默认的存储桶地域
8+
$cosClient = new Qcloud\Cos\Client(
9+
array(
10+
'region' => $region,
11+
'schema' => 'https', //协议头部,默认为http
12+
'credentials'=> array(
13+
'secretId' => $secretId ,
14+
'secretKey' => $secretKey)));
15+
$local_path = "/data/exampleobject";
16+
17+
try {
18+
$bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
19+
$key = "exampleobject"; //对象在存储桶中的位置,即对象键
20+
$signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes'); //签名的有效时间
21+
// 请求成功
22+
echo $signedUrl;
23+
} catch (\Exception $e) {
24+
// 请求失败
25+
print_r($e);
26+
}
27+

sample/getPresignetUrl.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
require dirname(__FILE__) . '/../vendor/autoload.php';
4+
5+
$secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
6+
$secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
7+
$region = "ap-beijing"; //设置一个默认的存储桶地域
8+
$cosClient = new Qcloud\Cos\Client(
9+
array(
10+
'region' => $region,
11+
'schema' => 'https', //协议头部,默认为http
12+
'credentials'=> array(
13+
'secretId' => $secretId ,
14+
'secretKey' => $secretKey)));
15+
$local_path = "/data/exampleobject";
16+
try {
17+
$signedUrl = $cosClient->getPresignetUrl(
18+
$method='putObject',
19+
$args=['Bucket'=>'examplebucket-1250000000', //格式:BucketName-APPID
20+
'Key'=>'exampleobject',
21+
'Body'=>''],
22+
$expires='+30 minutes"');
23+
// 请求成功
24+
echo($signedUrl);
25+
} catch (\Exception $e) {
26+
// 请求失败
27+
echo($e);
28+
}
29+

src/Qcloud/Cos/Tests/Test.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ protected function setUp()
3030
protected function tearDown() {
3131
}
3232

33+
function generateRandomString($length = 10) {
34+
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
35+
$randomString = '';
36+
for ($i = 0; $i < $length; $i++) {
37+
$randomString .= $characters[rand(0, strlen($characters) - 1)];
38+
}
39+
return $randomString;
40+
}
41+
42+
function generateRandomFile($size = 10, $filename = 'random-file') {
43+
exec("dd if=/dev/urandom of=". $filename. " bs=1 count=". (string)$size);
44+
}
45+
3346
/**********************************
3447
* TestBucket
3548
**********************************/
@@ -917,7 +930,7 @@ public function testUploadSmallObject() {
917930
*/
918931
public function testPutObjectEmpty() {
919932
try {
920-
$this->cosClient->upload($this->bucket, '你好.txt', '123');
933+
$this->cosClient->upload($this->bucket, '你好.txt', '');
921934
} catch (ServiceResponseException $e) {
922935
print $e;
923936
$this->assertFalse(TRUE);
@@ -986,7 +999,12 @@ public function testPutObjectMeta2K() {
986999
*/
9871000
public function testUploadComplexObject() {
9881001
try {
989-
$this->cosClient->upload($this->bucket, '→↓←→↖↗↙↘! \"#$%&\'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', 'Hello World');
1002+
$key = '→↓←→↖↗↙↘! \"#$%&\'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
1003+
$this->cosClient->upload($this->bucket, $key, 'Hello World');
1004+
$this->cosClient->headObject(array(
1005+
'Bucket' => $this->bucket,
1006+
'Key' => $key
1007+
));
9901008
} catch (ServiceResponseException $e) {
9911009
print $e;
9921010
$this->assertFalse(TRUE);
@@ -999,10 +1017,16 @@ public function testUploadComplexObject() {
9991017
*/
10001018
public function testUploadLargeObject() {
10011019
try {
1020+
$key = '你好.txt';
1021+
$body = $this->generateRandomString(3*1024*1024+1023);
1022+
$md5 = base64_encode(md5($body, true));
10021023
$this->cosClient->upload($bucket=$this->bucket,
1003-
$key='你好.txt',
1004-
$body=str_repeat('a', 3 * 1024 * 1024 + 1),
1024+
$key=$key,
1025+
$body=$body,
10051026
$options=['PartSize'=>1024 * 1024 + 1]);
1027+
$rt = $this->cosClient->getObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
1028+
$download_md5 = base64_encode(md5($rt['Body'], true));
1029+
$this->assertEquals($md5, $download_md5);
10061030
} catch (ServiceResponseException $e) {
10071031
print $e;
10081032
$this->assertFalse(TRUE);
@@ -1154,16 +1178,24 @@ public function testCopySmallObject() {
11541178
*/
11551179
public function testCopyLargeObject() {
11561180
try{
1181+
$key = '你好.txt';
1182+
$dst_key = 'hi.txt';
1183+
$body = $this->generateRandomString(3*1024*1024+1023);
1184+
$md5 = base64_encode(md5($body, true));
11571185
$this->cosClient->upload($bucket=$this->bucket,
1158-
$key='你好.txt',
1159-
$body=str_repeat('a', 3 * 1024 * 1024 + 1),
1186+
$key=$key,
1187+
$body=$body,
11601188
$options=['PartSize'=>1024 * 1024 + 1]);
11611189
$this->cosClient->copy($bucket=$this->bucket,
1162-
$key='hi.txt',
1190+
$key=$dst_key,
11631191
$copySource = ['Bucket'=>$this->bucket,
11641192
'Region'=>$this->region,
1165-
'Key'=>'你好.txt'],
1193+
'Key'=>$key],
11661194
$options=['PartSize'=>1024 * 1024 + 1]);
1195+
1196+
$rt = $this->cosClient->getObject(['Bucket'=>$this->bucket, 'Key'=>$dst_key]);
1197+
$download_md5 = base64_encode(md5($rt['Body'], true));
1198+
$this->assertEquals($md5, $download_md5);
11671199
} catch (ServiceResponseException $e) {
11681200
print $e;
11691201
$this->assertFalse(TRUE);

0 commit comments

Comments
 (0)