Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add url encoding to ListObjects and ListMultipartUploads #4

Merged
merged 1 commit into from
Dec 12, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion samples/MultipartUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ function uploadDir($ossClient, $bucket)
function listMultipartUploads($ossClient, $bucket)
{
$options = array(
'delimiter' => '/',
'max-uploads' => 100,
'key-marker' => '',
'prefix' => '',
Expand Down
20 changes: 20 additions & 0 deletions src/OSS/Core/OssUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,24 @@ public static function readDir($dir, $exclude = ".|..|.svn|.git", $recursive = f
}
return $file_list_array;
}

/**
* Decode key based on the encoding type
*
* @param string $key
* @param string $encoding
* @return string
*/
public static function decodeKey($key, $encoding)
{
if ($encoding == "") {
return $key;
}

if ($encoding == "url") {
return rawurldecode($key);
} else {
throw new OssException("Unrecognized encoding type: " . $encoding);
}
}
}
15 changes: 15 additions & 0 deletions src/OSS/OssClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ public function listObjects($bucket, $options = NULL)
self::OSS_MAX_KEYS => isset($options[self::OSS_MAX_KEYS]) ? $options[self::OSS_MAX_KEYS] : self::OSS_MAX_KEYS_VALUE,
self::OSS_MARKER => isset($options[self::OSS_MARKER]) ? $options[self::OSS_MARKER] : '',
);
$query = isset($options[self::OSS_QUERY_STRING]) ? $options[self::OSS_QUERY_STRING] : array();
$options[self::OSS_QUERY_STRING] = array_merge(
$query,
array(self::OSS_ENCODING_TYPE => self::OSS_ENCODING_TYPE_URL)
);

$response = $this->auth($options);
$result = new ListObjectsResult($response);
return $result->getData();
Expand Down Expand Up @@ -1011,6 +1017,12 @@ public function listMultipartUploads($bucket, $options = null)
unset($options[$param]);
}
}
$query = isset($options[self::OSS_QUERY_STRING]) ? $options[self::OSS_QUERY_STRING] : array();
$options[self::OSS_QUERY_STRING] = array_merge(
$query,
array(self::OSS_ENCODING_TYPE => self::OSS_ENCODING_TYPE_URL)
);

$response = $this->auth($options);
$result = new ListMultipartUploadResult($response);
return $result->getData();
Expand Down Expand Up @@ -1962,6 +1974,9 @@ public function setConnectTimeout($connectTimeout)
const OSS_ACL_TYPE_PRIVATE = 'private';
const OSS_ACL_TYPE_PUBLIC_READ = 'public-read';
const OSS_ACL_TYPE_PUBLIC_READ_WRITE = 'public-read-write';
const OSS_ENCODING_TYPE = "encoding-type";
const OSS_ENCODING_TYPE_URL = "url";

// 域名类型
const OSS_HOST_TYPE_NORMAL = "normal";//http://bucket.oss-cn-hangzhou.aliyuncs.com/object
const OSS_HOST_TYPE_IP = "ip"; //http://1.1.1.1/bucket/object
Expand Down
7 changes: 7 additions & 0 deletions src/OSS/Result/ListMultipartUploadResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OSS\Result;

use OSS\Core\OssUtil;
use OSS\Model\ListMultipartUploadInfo;
use OSS\Model\UploadInfo;

Expand All @@ -22,20 +23,26 @@ protected function parseDataFromResponse()
$content = $this->rawResponse->body;
$xml = simplexml_load_string($content);

$encodingType = isset($xml->EncodingType) ? strval($xml->EncodingType) : "";
$bucket = isset($xml->Bucket) ? strval($xml->Bucket) : "";
$keyMarker = isset($xml->KeyMarker) ? strval($xml->KeyMarker) : "";
$keyMarker = OssUtil::decodeKey($keyMarker, $encodingType);
$uploadIdMarker = isset($xml->UploadIdMarker) ? strval($xml->UploadIdMarker) : "";
$nextKeyMarker = isset($xml->NextKeyMarker) ? strval($xml->NextKeyMarker) : "";
$nextKeyMarker = OssUtil::decodeKey($nextKeyMarker, $encodingType);
$nextUploadIdMarker = isset($xml->NextUploadIdMarker) ? strval($xml->NextUploadIdMarker) : "";
$delimiter = isset($xml->Delimiter) ? strval($xml->Delimiter) : "";
$delimiter = OssUtil::decodeKey($delimiter, $encodingType);
$prefix = isset($xml->Prefix) ? strval($xml->Prefix) : "";
$prefix = OssUtil::decodeKey($prefix, $encodingType);
$maxUploads = isset($xml->MaxUploads) ? intval($xml->MaxUploads) : 0;
$isTruncated = isset($xml->IsTruncated) ? strval($xml->IsTruncated) : "";
$listUpload = array();

if (isset($xml->Upload)) {
foreach ($xml->Upload as $upload) {
$key = isset($upload->Key) ? strval($upload->Key) : "";
$key = OssUtil::decodeKey($key, $encodingType);
$uploadId = isset($upload->UploadId) ? strval($upload->UploadId) : "";
$initiated = isset($upload->Initiated) ? strval($upload->Initiated) : "";
$listUpload[] = new UploadInfo($key, $uploadId, $initiated);
Expand Down
16 changes: 12 additions & 4 deletions src/OSS/Result/ListObjectsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OSS\Result;

use OSS\Core\OssUtil;
use OSS\Model\ObjectInfo;
use OSS\Model\ObjectListInfo;
use OSS\Model\PrefixInfo;
Expand All @@ -20,24 +21,30 @@ class ListObjectsResult extends Result
protected function parseDataFromResponse()
{
$xml = new \SimpleXMLElement($this->rawResponse->body);
$objectList = $this->parseObjectList($xml);
$prefixList = $this->parsePrefixList($xml);
$encodingType = isset($xml->EncodingType) ? strval($xml->EncodingType) : "";
$objectList = $this->parseObjectList($xml, $encodingType);
$prefixList = $this->parsePrefixList($xml, $encodingType);
$bucketName = isset($xml->Name) ? strval($xml->Name) : "";
$prefix = isset($xml->Prefix) ? strval($xml->Prefix) : "";
$prefix = OssUtil::decodeKey($prefix, $encodingType);
$marker = isset($xml->Marker) ? strval($xml->Marker) : "";
$marker = OssUtil::decodeKey($marker, $encodingType);
$maxKeys = isset($xml->MaxKeys) ? intval($xml->MaxKeys) : 0;
$delimiter = isset($xml->Delimiter) ? strval($xml->Delimiter) : "";
$delimiter = OssUtil::decodeKey($delimiter, $encodingType);
$isTruncated = isset($xml->IsTruncated) ? strval($xml->IsTruncated) : "";
$nextMarker = isset($xml->NextMarker) ? strval($xml->NextMarker) : "";
$nextMarker = OssUtil::decodeKey($nextMarker, $encodingType);
return new ObjectListInfo($bucketName, $prefix, $marker, $nextMarker, $maxKeys, $delimiter, $isTruncated, $objectList, $prefixList);
}

private function parseObjectList($xml)
private function parseObjectList($xml, $encodingType)
{
$retList = array();
if (isset($xml->Contents)) {
foreach ($xml->Contents as $content) {
$key = isset($content->Key) ? strval($content->Key) : "";
$key = OssUtil::decodeKey($key, $encodingType);
$lastModified = isset($content->LastModified) ? strval($content->LastModified) : "";
$eTag = isset($content->ETag) ? strval($content->ETag) : "";
$type = isset($content->Type) ? strval($content->Type) : "";
Expand All @@ -49,12 +56,13 @@ private function parseObjectList($xml)
return $retList;
}

private function parsePrefixList($xml)
private function parsePrefixList($xml, $encodingType)
{
$retList = array();
if (isset($xml->CommonPrefixes)) {
foreach ($xml->CommonPrefixes as $commonPrefix) {
$prefix = isset($commonPrefix->Prefix) ? strval($commonPrefix->Prefix) : "";
$prefix = OssUtil::decodeKey($prefix, $encodingType);
$retList[] = new PrefixInfo($prefix);
}
}
Expand Down
50 changes: 50 additions & 0 deletions tests/OSS/Tests/ListMultipartUploadResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ class ListMultipartUploadResultTest extends \PHPUnit_Framework_TestCase
<Initiated>2012-02-23T06:14:27.000Z</Initiated>
</Upload>
</ListMultipartUploadsResult>
BBBB;

private $validXmlWithEncodedKey = <<<BBBB
<?xml version="1.0" encoding="UTF-8"?>
<ListMultipartUploadsResult xmlns="http://doc.oss-cn-hangzhou.aliyuncs.com">
<Bucket>oss-example</Bucket>
<EncodingType>url</EncodingType>
<KeyMarker>php%2Bkey-marker</KeyMarker>
<UploadIdMarker>3</UploadIdMarker>
<NextKeyMarker>php%2Bnext-key-marker</NextKeyMarker>
<NextUploadIdMarker>0004B99B8E707874FC2D692FA5D77D3F</NextUploadIdMarker>
<Delimiter>%2F</Delimiter>
<Prefix>php%2Bprefix</Prefix>
<MaxUploads>1000</MaxUploads>
<IsTruncated>true</IsTruncated>
<Upload>
<Key>php%2Bkey-1</Key>
<UploadId>0004B999EF518A1FE585B0C9360DC4C8</UploadId>
<Initiated>2012-02-23T04:18:23.000Z</Initiated>
</Upload>
<Upload>
<Key>php%2Bkey-2</Key>
<UploadId>0004B999EF5A239BB9138C6227D69F95</UploadId>
<Initiated>2012-02-23T04:18:23.000Z</Initiated>
</Upload>
<Upload>
<Key>php%2Bkey-3</Key>
<UploadId>0004B99B8E707874FC2D692FA5D77D3F</UploadId>
<Initiated>2012-02-23T06:14:27.000Z</Initiated>
</Upload>
</ListMultipartUploadsResult>
BBBB;

public function testParseValidXml()
Expand All @@ -59,4 +90,23 @@ public function testParseValidXml()
$this->assertEquals("0004B999EF518A1FE585B0C9360DC4C8", $listMultipartUploadInfo->getUploads()[0]->getUploadId());
$this->assertEquals("2012-02-23T04:18:23.000Z", $listMultipartUploadInfo->getUploads()[0]->getInitiated());
}

public function testParseValidXmlWithEncodedKey()
{
$response = new ResponseCore(array(), $this->validXmlWithEncodedKey, 200);
$result = new ListMultipartUploadResult($response);
$listMultipartUploadInfo = $result->getData();
$this->assertEquals("oss-example", $listMultipartUploadInfo->getBucket());
$this->assertEquals("php+key-marker", $listMultipartUploadInfo->getKeyMarker());
$this->assertEquals("php+next-key-marker", $listMultipartUploadInfo->getNextKeyMarker());
$this->assertEquals(3, $listMultipartUploadInfo->getUploadIdMarker());
$this->assertEquals("0004B99B8E707874FC2D692FA5D77D3F", $listMultipartUploadInfo->getNextUploadIdMarker());
$this->assertEquals("/", $listMultipartUploadInfo->getDelimiter());
$this->assertEquals("php+prefix", $listMultipartUploadInfo->getPrefix());
$this->assertEquals(1000, $listMultipartUploadInfo->getMaxUploads());
$this->assertEquals("true", $listMultipartUploadInfo->getIsTruncated());
$this->assertEquals("php+key-1", $listMultipartUploadInfo->getUploads()[0]->getKey());
$this->assertEquals("0004B999EF518A1FE585B0C9360DC4C8", $listMultipartUploadInfo->getUploads()[0]->getUploadId());
$this->assertEquals("2012-02-23T04:18:23.000Z", $listMultipartUploadInfo->getUploads()[0]->getInitiated());
}
}
50 changes: 50 additions & 0 deletions tests/OSS/Tests/ListObjectsResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ class ListObjectsResultTest extends \PHPUnit_Framework_TestCase
</ListBucketResult>
BBBB;

private $validXmlWithEncodedKey = <<<BBBB
<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult>
<Name>testbucket-hf</Name>
<EncodingType>url</EncodingType>
<Prefix>php%2Fprefix</Prefix>
<Marker>php%2Fmarker</Marker>
<NextMarker>php%2Fnext-marker</NextMarker>
<MaxKeys>1000</MaxKeys>
<Delimiter>%2F</Delimiter>
<IsTruncated>true</IsTruncated>
<Contents>
<Key>php/a%2Bb</Key>
<LastModified>2015-11-18T03:36:00.000Z</LastModified>
<ETag>"89B9E567E7EB8815F2F7D41851F9A2CD"</ETag>
<Type>Normal</Type>
<Size>13115</Size>
<StorageClass>Standard</StorageClass>
<Owner>
<ID>cname_user</ID>
<DisplayName>cname_user</DisplayName>
</Owner>
</Contents>
</ListBucketResult>
BBBB;

public function testParseValidXml1()
{
Expand Down Expand Up @@ -95,4 +120,29 @@ public function testParseValidXml2()
$this->assertEquals(13115, $objectListInfo->getObjectList()[0]->getSize());
$this->assertEquals('Standard', $objectListInfo->getObjectList()[0]->getStorageClass());
}

public function testParseValidXmlWithEncodedKey()
{
$response = new ResponseCore(array(), $this->validXmlWithEncodedKey, 200);
$result = new ListObjectsResult($response);
$this->assertTrue($result->isOK());
$this->assertNotNull($result->getData());
$this->assertNotNull($result->getRawResponse());
$objectListInfo = $result->getData();
$this->assertEquals(0, count($objectListInfo->getPrefixList()));
$this->assertEquals(1, count($objectListInfo->getObjectList()));
$this->assertEquals('testbucket-hf', $objectListInfo->getBucketName());
$this->assertEquals('php/prefix', $objectListInfo->getPrefix());
$this->assertEquals('php/marker', $objectListInfo->getMarker());
$this->assertEquals('php/next-marker', $objectListInfo->getNextMarker());
$this->assertEquals(1000, $objectListInfo->getMaxKeys());
$this->assertEquals('/', $objectListInfo->getDelimiter());
$this->assertEquals('true', $objectListInfo->getIsTruncated());
$this->assertEquals('php/a+b', $objectListInfo->getObjectList()[0]->getKey());
$this->assertEquals('2015-11-18T03:36:00.000Z', $objectListInfo->getObjectList()[0]->getLastModified());
$this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objectListInfo->getObjectList()[0]->getETag());
$this->assertEquals('Normal', $objectListInfo->getObjectList()[0]->getType());
$this->assertEquals(13115, $objectListInfo->getObjectList()[0]->getSize());
$this->assertEquals('Standard', $objectListInfo->getObjectList()[0]->getStorageClass());
}
}