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
27 changes: 27 additions & 0 deletions sample/getObjectUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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)));
$local_path = "/data/exampleobject";

try {
$bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
$key = "exampleobject"; //对象在存储桶中的位置,即对象键
$signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes'); //签名的有效时间
// 请求成功
echo $signedUrl;
} catch (\Exception $e) {
// 请求失败
print_r($e);
}

29 changes: 29 additions & 0 deletions sample/getPresignetUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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)));
$local_path = "/data/exampleobject";
try {
$signedUrl = $cosClient->getPresignetUrl(
$method='putObject',
$args=['Bucket'=>'examplebucket-1250000000', //格式:BucketName-APPID
'Key'=>'exampleobject',
'Body'=>''],
$expires='+30 minutes"');
// 请求成功
echo($signedUrl);
} catch (\Exception $e) {
// 请求失败
echo($e);
}

48 changes: 40 additions & 8 deletions src/Qcloud/Cos/Tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ protected function setUp()
protected function tearDown() {
}

function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}

function generateRandomFile($size = 10, $filename = 'random-file') {
exec("dd if=/dev/urandom of=". $filename. " bs=1 count=". (string)$size);
}

/**********************************
* TestBucket
**********************************/
Expand Down Expand Up @@ -917,7 +930,7 @@ public function testUploadSmallObject() {
*/
public function testPutObjectEmpty() {
try {
$this->cosClient->upload($this->bucket, '你好.txt', '123');
$this->cosClient->upload($this->bucket, '你好.txt', '');
} catch (ServiceResponseException $e) {
print $e;
$this->assertFalse(TRUE);
Expand Down Expand Up @@ -986,7 +999,12 @@ public function testPutObjectMeta2K() {
*/
public function testUploadComplexObject() {
try {
$this->cosClient->upload($this->bucket, '→↓←→↖↗↙↘! \"#$%&\'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', 'Hello World');
$key = '→↓←→↖↗↙↘! \"#$%&\'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
$this->cosClient->upload($this->bucket, $key, 'Hello World');
$this->cosClient->headObject(array(
'Bucket' => $this->bucket,
'Key' => $key
));
} catch (ServiceResponseException $e) {
print $e;
$this->assertFalse(TRUE);
Expand All @@ -999,10 +1017,16 @@ public function testUploadComplexObject() {
*/
public function testUploadLargeObject() {
try {
$key = '你好.txt';
$body = $this->generateRandomString(3*1024*1024+1023);
$md5 = base64_encode(md5($body, true));
$this->cosClient->upload($bucket=$this->bucket,
$key='你好.txt',
$body=str_repeat('a', 3 * 1024 * 1024 + 1),
$key=$key,
$body=$body,
$options=['PartSize'=>1024 * 1024 + 1]);
$rt = $this->cosClient->getObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
$download_md5 = base64_encode(md5($rt['Body'], true));
$this->assertEquals($md5, $download_md5);
} catch (ServiceResponseException $e) {
print $e;
$this->assertFalse(TRUE);
Expand Down Expand Up @@ -1154,16 +1178,24 @@ public function testCopySmallObject() {
*/
public function testCopyLargeObject() {
try{
$key = '你好.txt';
$dst_key = 'hi.txt';
$body = $this->generateRandomString(3*1024*1024+1023);
$md5 = base64_encode(md5($body, true));
$this->cosClient->upload($bucket=$this->bucket,
$key='你好.txt',
$body=str_repeat('a', 3 * 1024 * 1024 + 1),
$key=$key,
$body=$body,
$options=['PartSize'=>1024 * 1024 + 1]);
$this->cosClient->copy($bucket=$this->bucket,
$key='hi.txt',
$key=$dst_key,
$copySource = ['Bucket'=>$this->bucket,
'Region'=>$this->region,
'Key'=>'你好.txt'],
'Key'=>$key],
$options=['PartSize'=>1024 * 1024 + 1]);

$rt = $this->cosClient->getObject(['Bucket'=>$this->bucket, 'Key'=>$dst_key]);
$download_md5 = base64_encode(md5($rt['Body'], true));
$this->assertEquals($md5, $download_md5);
} catch (ServiceResponseException $e) {
print $e;
$this->assertFalse(TRUE);
Expand Down