Skip to content

Commit

Permalink
Merge b05493f into 250c8e5
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertYue19900425 authored Nov 17, 2016
2 parents 250c8e5 + b05493f commit 9511a19
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 25 deletions.
52 changes: 43 additions & 9 deletions samples/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,42 @@
if (is_null($ossClient)) exit(1);
//*******************************简单使用***************************************************************

// 简单上传变量的内容到文件
$ossClient->putObject($bucket, "b.file", "hi, oss");
// 简单上传变量的内容到oss文件
$result = $ossClient->putObject($bucket, "b.file", "hi, oss");
Common::println("b.file is created");
Common::println($result['x-oss-request-id']);
Common::println($result['etag']);
Common::println($result['content-md5']);
Common::println($result['body']);

// 使用callback上传内容到oss文件
// callbackUrl参数指定请求回调的服务器URL
// callbackBodyType参数可为application/json或application/x-www-form-urlencoded
$json =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackHost":"oss-cn-hangzhou.aliyuncs.com",
"callbackBody":"春水碧于天,画船听雨眠。垆边人似月,皓腕凝霜雪",
"callbackBodyType":"application/json"
}';
$var =
'{
"x:var1":"value1",
"x:var2":"值2"
}';
$options = array(OssClient::OSS_CALLBACK => $json,
OssClient::OSS_CALLBACK_VAR => $var
);
$result = $ossClient->putObject($bucket, "b.file", "random content", $options);

// 上传本地文件
$ossClient->uploadFile($bucket, "c.file", __FILE__);
$result = $ossClient->uploadFile($bucket, "c.file", __FILE__);
Common::println("c.file is created");
Common::println("b.file is created");
Common::println($result['x-oss-request-id']);
Common::println($result['etag']);
Common::println($result['content-md5']);
Common::println($result['body']);

// 下载object到本地变量
$content = $ossClient->getObject($bucket, "b.file");
Expand All @@ -26,28 +55,33 @@
$options = array(
OssClient::OSS_FILE_DOWNLOAD => "./c.file.localcopy",
);
$ossClient->getObject($bucket, "c.file", $options);
$result = $ossClient->getObject($bucket, "c.file", $options);
Common::println("b.file is fetched to the local file: c.file.localcopy");
Common::println("b.file is created");

// 拷贝object
$ossClient->copyObject($bucket, "c.file", $bucket, "c.file.copy");
Common::println("c.file is copied to c.file.copy");
$result = $ossClient->copyObject($bucket, "c.file", $bucket, "c.file.copy");
foreach($result as $val)
print($val);

// 判断object是否存在
$doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");
Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));

// 删除object
$ossClient->deleteObject($bucket, "c.file.copy");
$result = $ossClient->deleteObject($bucket, "c.file.copy");
Common::println("c.file.copy is deleted");
Common::println("b.file is created");
Common::println($result['x-oss-request-id']);

// 判断object是否存在
$doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");
Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));

// 批量删除object
$ossClient->deleteObjects($bucket, array("b.file", "c.file"));
Common::println("b.file, c.file are deleted");
$result = $ossClient->deleteObjects($bucket, array("b.file", "c.file"));
foreach($result as $object)
print($object);

sleep(2);
unlink("c.file.localcopy");
Expand Down
32 changes: 27 additions & 5 deletions src/OSS/OssClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
use OSS\Result\ListObjectsResult;
use OSS\Result\ListPartsResult;
use OSS\Result\PutSetDeleteResult;
use OSS\Result\DeleteObjectsResult;
use OSS\Result\CopyObjectResult;
use OSS\Result\CallbackResult;
use OSS\Result\ExistResult;
use OSS\Result\PutLiveChannelResult;
use OSS\Result\GetLiveChannelHistoryResult;
Expand Down Expand Up @@ -988,7 +991,13 @@ public function putObject($bucket, $object, $content, $options = NULL)
$options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
}
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);

if (isset($options[self::OSS_CALLBACK]) && !empty($options[self::OSS_CALLBACK])) {
$result = new CallbackResult($response);
} else {
$result = new PutSetDeleteResult($response);
}

return $result->getData();
}

Expand Down Expand Up @@ -1131,7 +1140,7 @@ public function copyObject($fromBucket, $fromObject, $toBucket, $toObject, $opti
$options[self::OSS_HEADERS] = array(self::OSS_OBJECT_COPY_SOURCE => '/' . $fromBucket . '/' . $fromObject);
}
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
$result = new CopyObjectResult($response);
return $result->getData();
}

Expand Down Expand Up @@ -1204,7 +1213,7 @@ public function deleteObjects($bucket, $objects, $options = null)
$xmlBody = OssUtil::createDeleteObjectsXmlBody($objects, $quiet);
$options[self::OSS_CONTENT] = $xmlBody;
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
$result = new DeleteObjectsResult($response);
return $result->getData();
}

Expand Down Expand Up @@ -1435,7 +1444,11 @@ public function completeMultipartUpload($bucket, $object, $uploadId, $listParts,
}
$options[self::OSS_CONTENT] = OssUtil::createCompleteMultipartUploadXmlBody($listParts);
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
if (isset($options[self::OSS_CALLBACK]) && !empty($options[self::OSS_CALLBACK])) {
$result = new CallbackResult($response);
} else {
$result = new PutSetDeleteResult($response);
}
return $result->getData();
}

Expand Down Expand Up @@ -1921,6 +1934,13 @@ private function auth($options)
$headers[self::OSS_CONTENT_MD5] = base64_encode(md5($options[self::OSS_CONTENT], true));
}

if (isset($options[self::OSS_CALLBACK])) {
$headers[self::OSS_CALLBACK] = base64_encode($options[self::OSS_CALLBACK]);
}
if (isset($options[self::OSS_CALLBACK_VAR])) {
$headers[self::OSS_CALLBACK_VAR] = base64_encode($options[self::OSS_CALLBACK_VAR]);
}

uksort($headers, 'strnatcasecmp');
foreach ($headers as $header_key => $header_value) {
$header_value = str_replace(array("\r", "\n"), '', $header_value);
Expand All @@ -1944,7 +1964,7 @@ private function auth($options)

//对?后面的要签名的string字母序排序
$string_to_sign_ordered = $this->stringToSignSorted($string_to_sign);

$signature = base64_encode(hash_hmac('sha1', $string_to_sign_ordered, $this->accessKeySecret, true));
$request->add_header('Authorization', 'OSS ' . $this->accessKeyId . ':' . $signature);

Expand Down Expand Up @@ -2457,6 +2477,8 @@ public function setConnectTimeout($connectTimeout)
const OSS_OBJECT_COPY_SOURCE = 'x-oss-copy-source';
const OSS_OBJECT_COPY_SOURCE_RANGE = "x-oss-copy-source-range";
const OSS_PROCESS = "x-oss-process";
const OSS_CALLBACK = "x-oss-callback";
const OSS_CALLBACK_VAR = "x-oss-callback-var";
//支持STS SecurityToken
const OSS_SECURITY_TOKEN = "x-oss-security-token";
const OSS_ACL_TYPE_PRIVATE = 'private';
Expand Down
21 changes: 21 additions & 0 deletions src/OSS/Result/CallbackResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace OSS\Result;


/**
* Class CallbackResult
* @package OSS\Result
*/
class CallbackResult extends PutSetDeleteResult
{
protected function isResponseOk()
{
$status = $this->rawResponse->status;
if ((int)(intval($status) / 100) == 2 && (int)(intval($status) != 203)) {
return true;
}
return false;
}

}
30 changes: 30 additions & 0 deletions src/OSS/Result/CopyObjectResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace OSS\Result;


/**
* Class CopyObjectResult
* @package OSS\Result
*/
class CopyObjectResult extends Result
{
/**
* @return array()
*/
protected function parseDataFromResponse()
{
$body = $this->rawResponse->body;
$xml = simplexml_load_string($body);
$result = array();

if (isset($xml->LastModified)) {
$result[] = $xml->LastModified;
}
if (isset($xml->ETag)) {
$result[] = $xml->ETag;
}

return $result;
}
}
27 changes: 27 additions & 0 deletions src/OSS/Result/DeleteObjectsResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace OSS\Result;


/**
* Class DeleteObjectsResult
* @package OSS\Result
*/
class DeleteObjectsResult extends Result
{
/**
* @return array()
*/
protected function parseDataFromResponse()
{
$body = $this->rawResponse->body;
$xml = simplexml_load_string($body);
$objects = array();

if (isset($xml->Deleted)) {
foreach($xml->Deleted as $deleteKey)
$objects[] = $deleteKey->Key;
}
return $objects;
}
}
7 changes: 4 additions & 3 deletions src/OSS/Result/PutSetDeleteResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
class PutSetDeleteResult extends Result
{
/**
* @return null
* @return array()
*/
protected function parseDataFromResponse()
{
return null;
$body = array('body' => $this->rawResponse->body);
return array_merge($this->rawResponse->header, $body);
}
}
}
Loading

0 comments on commit 9511a19

Please sign in to comment.