Skip to content

Commit

Permalink
Merge 747b455 into 250c8e5
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertYue19900425 committed Nov 21, 2016
2 parents 250c8e5 + 747b455 commit 4005bb3
Show file tree
Hide file tree
Showing 12 changed files with 600 additions and 26 deletions.
81 changes: 81 additions & 0 deletions samples/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
require_once __DIR__ . '/Common.php';

use OSS\OssClient;
use OSS\Core\OssException;

$bucket = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);
//*******************************简单使用***************************************************************

/** putObject 使用callback上传内容到oss文件
* callbackurl参数指定请求回调的服务器url
* callbackbodytype参数可为application/json或application/x-www-form-urlencoded, 可选参数,默认为application/x-www-form-urlencoded
* OSS_CALLBACK_VAR参数可以不设置
*/
$url =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackHost":"oss-cn-hangzhou.aliyuncs.com",
"callbackBody":"bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}&my_var1=${x:var1}&my_var2=${x:var2}",
"callbackBodyType":"application/x-www-form-urlencoded"
}';
$var =
'{
"x:var1":"value1",
"x:var2":"值2"
}';
$options = array(OssClient::OSS_CALLBACK => $url,
OssClient::OSS_CALLBACK_VAR => $var
);
$result = $ossClient->putObject($bucket, "b.file", "random content", $options);
Common::println($result['body']);
Common::println($result['info']['http_code']);
/**
* completeMultipartUpload 使用callback上传内容到oss文件
* callbackurl参数指定请求回调的服务器url
* callbackbodytype参数可为application/json或application/x-www-form-urlencoded, 可选参数,默认为application/x-www-form-urlencoded
* OSS_CALLBACK_VAR参数可以不设置
*/
$object = "multipart-callback-test.txt";
$copiedObject = "multipart-callback-test.txt.copied";
$ossClient->putObject($bucket, $copiedObject, file_get_contents(__FILE__));
/**
* step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
*/
$upload_id = $ossClient->initiateMultipartUpload($bucket, $object);
/*
* step 2. uploadPartCopy
*/
$copyId = 1;
$eTag = $ossClient->uploadPartCopy($bucket, $copiedObject, $bucket, $object, $copyId, $upload_id);
$upload_parts[] = array(
'PartNumber' => $copyId,
'ETag' => $eTag,
);
$listPartsInfo = $ossClient->listParts($bucket, $object, $upload_id);
/**
* step 3.
*/
$json =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackHost":"oss-cn-hangzhou.aliyuncs.com",
"callbackBody":"{\"mimeType\":${mimeType},\"size\":${size},\"x:var1\":${x:var1},\"x:var2\":${x:var2}}",
"callbackBodyType":"application/json"
}';

$var =
'{
"x:var1":"value1",
"x:var2":"值2"
}';
$options = array(OssClient::OSS_CALLBACK => $json,
OssClient::OSS_CALLBACK_VAR => $var);

$result = $ossClient->completeMultipartUpload($bucket, $object, $upload_id, $upload_parts, $options);
Common::println($result['body']);
Common::println($result['info']['http_code']);

30 changes: 22 additions & 8 deletions samples/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@
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']);

// 上传本地文件
$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 @@ -28,26 +37,31 @@
);
$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");
Common::println("lastModifiedTime: " . $result[0]);
Common::println("ETag: " . $result[1]);

// 判断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)
Common::println($object);

sleep(2);
unlink("c.file.localcopy");
Expand Down
4 changes: 2 additions & 2 deletions src/OSS/Http/RequestCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,8 @@ public function process_response($curl_handle = null, $response = null)

// Reset the headers to the appropriate property.
$this->response_headers = $header_assoc;
$this->response_headers['_info'] = $this->response_info;
$this->response_headers['_info']['method'] = $this->method;
$this->response_headers['info'] = $this->response_info;
$this->response_headers['info']['method'] = $this->method;

if ($curl_handle && $response) {
//return new $this->response_class($this->response_headers, $this->response_body, $this->response_code, $this->curl_handle);
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 4005bb3

Please sign in to comment.