Skip to content

Commit

Permalink
Merge a9392b8 into f7c787b
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertYue19900425 committed Dec 7, 2017
2 parents f7c787b + a9392b8 commit c28f6b1
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: php
php:
- 7.1
- 7.0
- 5.6
- 5.5
- 5.4
- 5.3
install:
- composer self-update
- composer install --no-interaction
Expand Down
2 changes: 1 addition & 1 deletion src/OSS/Http/RequestCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public function prep_request()

// Enable a proxy connection if requested.
if ($this->proxy) {
curl_setopt($curl_handle, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($curl_handle, CURLOPT_HTTPPROXYTUNNEL, false);

$host = $this->proxy['host'];
$host .= ($this->proxy['port']) ? ':' . $this->proxy['port'] : '';
Expand Down
33 changes: 28 additions & 5 deletions src/OSS/OssClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class OssClient
* @param string $securityToken
* @throws OssException
*/
public function __construct($accessKeyId, $accessKeySecret, $endpoint, $isCName = false, $securityToken = NULL)
public function __construct($accessKeyId, $accessKeySecret, $endpoint, $isCName = false, $securityToken = NULL ,$requestProxy = NULL)
{
$accessKeyId = trim($accessKeyId);
$accessKeySecret = trim($accessKeySecret);
Expand All @@ -94,6 +94,7 @@ public function __construct($accessKeyId, $accessKeySecret, $endpoint, $isCName
$this->accessKeyId = $accessKeyId;
$this->accessKeySecret = $accessKeySecret;
$this->securityToken = $securityToken;
$this->requestProxy = $requestProxy;
self::checkEnv();
}

Expand Down Expand Up @@ -975,7 +976,6 @@ public function putObject($bucket, $object, $content, $options = NULL)
{
$this->precheckCommon($bucket, $object, $options);

OssUtil::validateContent($content);
$options[self::OSS_CONTENT] = $content;
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
Expand Down Expand Up @@ -1058,7 +1058,6 @@ public function appendObject($bucket, $object, $content, $position, $options = N
{
$this->precheckCommon($bucket, $object, $options);

OssUtil::validateContent($content);
$options[self::OSS_CONTENT] = $content;
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_POST;
Expand Down Expand Up @@ -1151,6 +1150,7 @@ public function copyObject($fromBucket, $fromObject, $toBucket, $toObject, $opti
} else {
$options[self::OSS_HEADERS] = array(self::OSS_OBJECT_COPY_SOURCE => '/' . $fromBucket . '/' . $fromObject);
}
$options[self::OSS_HEADERS][self::OSS_METADATA_DIRECTIVE] = 'REPLACE';
$response = $this->auth($options);
$result = new CopyObjectResult($response);
return $result->getData();
Expand Down Expand Up @@ -1205,7 +1205,7 @@ public function deleteObject($bucket, $object, $options = NULL)
*/
public function deleteObjects($bucket, $objects, $options = null)
{
$this->precheckCommon($bucket, NULL, $options, false);
$this->precheckCommon($bucket, NUll, $options, false);
if (!is_array($objects) || !$objects) {
throw new OssException('objects must be array');
}
Expand Down Expand Up @@ -1750,6 +1750,25 @@ private function precheckObject($object)
OssUtil::throwOssExceptionWithMessageIfEmpty($object, "object name is empty");
}

/**
* object字符转译
*
* @param string $object
*
*/
private function objectCharacterTranslation(& $option)
{
if(isset($option[self::OSS_OBJECT])){
$option[self::OSS_OBJECT] = str_replace("+","%2B",$option[self::OSS_OBJECT]);
$option[self::OSS_OBJECT] = str_replace(" ","%20",$option[self::OSS_OBJECT]);
$option[self::OSS_OBJECT] = str_replace("%","%25",$option[self::OSS_OBJECT]);
$option[self::OSS_OBJECT] = str_replace("#","%23",$option[self::OSS_OBJECT]);
$option[self::OSS_OBJECT] = str_replace("?","%3F",$option[self::OSS_OBJECT]);
$option[self::OSS_OBJECT] = str_replace("=","%3D",$option[self::OSS_OBJECT]);
}

}

/**
* 校验bucket,options参数
*
Expand Down Expand Up @@ -1862,6 +1881,8 @@ private function auth($options)
$this->authPrecheckBucket($options);
//验证object
$this->authPrecheckObject($options);
//object字符转译
$this->objectCharacterTranslation($options);
//Object名称的编码必须是utf8
$this->authPrecheckObjectEncoding($options);
//验证ACL
Expand Down Expand Up @@ -1893,7 +1914,7 @@ private function auth($options)
$this->requestUrl = $scheme . $hostname . $resource_uri . $signable_query_string . $non_signable_resource;

//创建请求
$request = new RequestCore($this->requestUrl);
$request = new RequestCore($this->requestUrl,$this->requestProxy);
$request->set_useragent($this->generateUserAgent());
// Streaming uploads
if (isset($options[self::OSS_FILE_UPLOAD])) {
Expand Down Expand Up @@ -2499,6 +2520,7 @@ public function setConnectTimeout($connectTimeout)
const OSS_PROCESS = "x-oss-process";
const OSS_CALLBACK = "x-oss-callback";
const OSS_CALLBACK_VAR = "x-oss-callback-var";
const OSS_METADATA_DIRECTIVE = 'x-oss-metadata-directive';
//支持STS SecurityToken
const OSS_SECURITY_TOKEN = "x-oss-security-token";
const OSS_ACL_TYPE_PRIVATE = 'private';
Expand Down Expand Up @@ -2538,6 +2560,7 @@ public function setConnectTimeout($connectTimeout)
private $accessKeyId;
private $accessKeySecret;
private $hostname;
private $requestProxy = null;
private $securityToken;
private $enableStsInUrl = false;
private $timeout = 0;
Expand Down
16 changes: 8 additions & 8 deletions tests/OSS/Tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testMultipartUploadCallbackNormal()

$json =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackUrl":"oss-demo.aliyuncs.com:23450",
"callbackHost":"oss-cn-hangzhou.aliyuncs.com",
"callbackBody":"{\"mimeType\":${mimeType},\"size\":${size},\"x:var1\":${x:var1},\"x:var2\":${x:var2}}",
"callbackBodyType":"application/json"
Expand Down Expand Up @@ -139,7 +139,7 @@ public function testPutObjectCallbackNormal()
{
$json =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackUrl":"oss-demo.aliyuncs.com:23450",
"callbackHost":"oss-cn-hangzhou.aliyuncs.com",
"callbackBody":"{\"mimeType\":${mimeType},\"size\":${size}}",
"callbackBodyType":"application/json"
Expand All @@ -151,7 +151,7 @@ public function testPutObjectCallbackNormal()
{
$url =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackUrl":"oss-demo.aliyuncs.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}",
"callbackBodyType":"application/x-www-form-urlencoded"
Expand All @@ -163,7 +163,7 @@ public function testPutObjectCallbackNormal()
{
$url =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackUrl":"oss-demo.aliyuncs.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}"
}';
Expand All @@ -174,7 +174,7 @@ public function testPutObjectCallbackNormal()
{
$json =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackUrl":"oss-demo.aliyuncs.com:23450",
"callbackHost":"oss-cn-hangzhou.aliyuncs.com",
"callbackBody":"{\" 春水碧于天,画船听雨眠。\":\"垆边人似月,皓腕凝霜雪。\"}",
"callbackBodyType":"application/json"
Expand All @@ -186,7 +186,7 @@ public function testPutObjectCallbackNormal()
{
$url =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackUrl":"oss-demo.aliyuncs.com:23450",
"callbackHost":"oss-cn-hangzhou.aliyuncs.com",
"callbackBody":"春水碧于天,画船听雨眠。垆边人似月,皓腕凝霜雪",
"callbackBodyType":"application/x-www-form-urlencoded"
Expand All @@ -198,7 +198,7 @@ public function testPutObjectCallbackNormal()
{
$json =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackUrl":"oss-demo.aliyuncs.com:23450",
"callbackHost":"oss-cn-hangzhou.aliyuncs.com",
"callbackBody":"{\"mimeType\":${mimeType},\"size\":${size},\"x:var1\":${x:var1},\"x:var2\":${x:var2}}",
"callbackBodyType":"application/json"
Expand All @@ -218,7 +218,7 @@ public function testPutObjectCallbackNormal()
{
$url =
'{
"callbackUrl":"callback.oss-demo.com:23450",
"callbackUrl":"oss-demo.aliyuncs.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"
Expand Down
1 change: 1 addition & 0 deletions tests/OSS/Tests/OssClientBucketCorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function testBucket()
Common::waitMetaSync();
$corsConfig3 = $this->ossClient->getBucketCors($this->bucket);
$this->assertNotNull($corsConfig3);

$this->assertNotEquals($corsConfig->serializeToXml(), $corsConfig3->serializeToXml());
} catch (OssException $e) {
$this->assertFalse(True);
Expand Down
19 changes: 19 additions & 0 deletions tests/OSS/Tests/OssClientObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testGetObjectWithAcceptEncoding()

try {
$res = $this->ossClient->getObject($this->bucket, $object, $options);

$this->assertEquals(file_get_contents(__FILE__), $res);
} catch (OssException $e) {
$this->assertTrue(false);
Expand Down Expand Up @@ -337,6 +338,22 @@ public function testObject()
} catch (OssException $e) {
$this->assertFalse(true);
}

$object = "emptybodytest";
try {
$this->ossClient->putObject($this->bucket, $object, "", $options);
} catch (OssException $e) {
$this->assertFalse(true);
}

try {
$content = $this->ossClient->getObject($this->bucket, $object);
$this->assertEquals($content, "");
} catch (OssException $e) {
$this->assertFalse(true);
}


}

public function testAppendObject()
Expand Down Expand Up @@ -423,6 +440,8 @@ public function testAppendObject()
try {
$position = $this->ossClient->appendObject($this->bucket, $object, "Hello OSS, ", 0, $options);
$position = $this->ossClient->appendObject($this->bucket, $object, "Hi OSS.", $position);
$position1 = $this->ossClient->appendObject($this->bucket, $object, "", $position);
$this->assertEquals($position, $position1);
} catch (OssException $e) {
$this->assertFalse(true);
}
Expand Down
84 changes: 80 additions & 4 deletions tests/OSS/Tests/OssClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

use OSS\Core\OssUtil;
class OssClientTest extends \PHPUnit_Framework_TestCase
{
public function testConstrunct()
Expand Down Expand Up @@ -98,14 +98,90 @@ public function testConstrunct8()
public function testConstrunct9()
{
try {
$accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
$accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
$endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
$accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
$accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
$endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
$ossClient = new OssClient($accessKeyId, $accessKeySecret , $endpoint, false);
$ossClient->listBuckets();
} catch (OssException $e) {
$this->assertFalse(true);
}
}

public function testCreateObjectDir()
{
try {
$accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
$accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
$endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
$bucket = getenv('OSS_BUCKET');
$object='test-dir';
$ossClient = new OssClient($accessKeyId, $accessKeySecret , $endpoint, false);
$ossClient->createObjectDir($bucket,$object);
} catch (OssException $e) {
$this->assertFalse(true);
}
}

public function testGetBucketCors()
{
try {
$accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
$accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
$endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
$bucket = getenv('OSS_BUCKET');
$ossClient = new OssClient($accessKeyId, $accessKeySecret , $endpoint, false);
$ossClient->getBucketCors($bucket);
} catch (OssException $e) {
$this->assertFalse(true);
}
}

public function testProxySupportGetBucketCors()
{
try{
$accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
$accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
$endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
$bucket = getenv('OSS_BUCKET');
$proxy = 'http://tester:Hello12345@47.88.84.40:3128';
$ossClient = new OssClient($accessKeyId, $accessKeySecret , $endpoint, false,null,$proxy);
$ossClient->getBucketCors($bucket);
}catch (OssException $e){
$this->assertFalse(true);
}
}

public function testProxySupportGetBucketCname()
{
try{
$accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
$accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
$endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
$bucket = getenv('OSS_BUCKET');
$proxy = 'http://tester:Hello12345@47.88.84.40:3128';
$ossClient = new OssClient($accessKeyId, $accessKeySecret , $endpoint, false,null,$proxy);
$ossClient->getBucketCname($bucket);
}catch (OssException $e){
$this->assertFalse(true);
}
}

public function testObjectCharacterTranslation()
{
try{

$accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
$accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
$endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
$bucket = getenv('OSS_BUCKET');
$object = 'text/%.txt';
$ossClient = new OssClient($accessKeyId, $accessKeySecret , $endpoint, false);
$ossClient->putObject($bucket,$object,'test content');
}catch (OssException $e){
$this->assertFalse(true);
}
}


}
3 changes: 2 additions & 1 deletion tests/OSS/Tests/TestOssClientBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function setUp()
$this->bucket = Common::getBucketName() . rand(100000, 999999);
$this->ossClient = Common::getOssClient();
$this->ossClient->createBucket($this->bucket);
Common::waitMetaSync();
Common::waitMetaSync();

}

public function tearDown()
Expand Down

0 comments on commit c28f6b1

Please sign in to comment.