diff --git a/sample/getObjectUrl.php b/sample/getObjectUrl.php new file mode 100644 index 00000000..0f43885c --- /dev/null +++ b/sample/getObjectUrl.php @@ -0,0 +1,27 @@ + $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); +} + diff --git a/sample/getPresignetUrl.php b/sample/getPresignetUrl.php new file mode 100644 index 00000000..6f0dea5d --- /dev/null +++ b/sample/getPresignetUrl.php @@ -0,0 +1,29 @@ + $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); +} + diff --git a/src/Qcloud/Cos/Tests/Test.php b/src/Qcloud/Cos/Tests/Test.php index a954e669..c025e8be 100644 --- a/src/Qcloud/Cos/Tests/Test.php +++ b/src/Qcloud/Cos/Tests/Test.php @@ -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 **********************************/ @@ -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); @@ -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); @@ -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); @@ -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);