Skip to content

Commit

Permalink
Merge pull request #60 from aliyun/dev-head-bucket
Browse files Browse the repository at this point in the history
support wj poc
  • Loading branch information
baiyubin2020 committed Dec 21, 2017
2 parents ccd5adf + 4e53c89 commit 6d1de8d
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/OSS/Model/StorageCapacityConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace OSS\Model;

/**
* Class StorageCapacityConfig
*
* @package OSS\Model
* @link http://docs.alibaba-inc.com/pages/viewpage.action?pageId=271614763
*/
class StorageCapacityConfig implements XmlConfig
{
/**
* StorageCapacityConfig constructor.
*
* @param int $storageCapacity
*/
public function __construct($storageCapacity)
{
$this->storageCapacity = $storageCapacity;
}

/**
* Not implemented
*/
public function parseFromXml($strXml)
{
throw new OssException("Not implemented.");
}

/**
* 把StorageCapacityConfig序列化成xml
*
* @return string
*/
public function serializeToXml()
{
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><BucketUserQos></BucketUserQos>');
$xml->addChild('StorageCapacity', strval($this->storageCapacity));
return $xml->asXML();
}

/**
* To string
*
* @return string
*/
function __toString()
{
return $this->serializeToXml();
}

/**
* Set storage capacity
*
* @param int $storageCapacity
*/
public function setStorageCapacity($storageCapacity)
{
$this->storageCapacity = $storageCapacity;
}

/**
* Get storage capacity
*
* @return int
*/
public function getStorageCapacity()
{
return $this->storageCapacity;
}

private $storageCapacity = 0;
}
86 changes: 86 additions & 0 deletions src/OSS/OssClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OSS\Model\LiveChannelConfig;
use OSS\Model\LiveChannelInfo;
use OSS\Model\LiveChannelListInfo;
use OSS\Model\StorageCapacityConfig;
use OSS\Result\AclResult;
use OSS\Result\BodyResult;
use OSS\Result\GetCorsResult;
Expand All @@ -20,6 +21,7 @@
use OSS\Result\GetRefererResult;
use OSS\Result\GetWebsiteResult;
use OSS\Result\GetCnameResult;
use OSS\Result\GetLocationResult;
use OSS\Result\HeaderResult;
use OSS\Result\InitiateMultipartUploadResult;
use OSS\Result\ListBucketsResult;
Expand All @@ -37,6 +39,7 @@
use OSS\Result\GetLiveChannelInfoResult;
use OSS\Result\GetLiveChannelStatusResult;
use OSS\Result\ListLiveChannelResult;
use OSS\Result\GetStorageCapacityResult;
use OSS\Result\AppendResult;
use OSS\Model\ObjectListInfo;
use OSS\Result\UploadPartResult;
Expand Down Expand Up @@ -179,6 +182,44 @@ public function doesBucketExist($bucket)
$result = new ExistResult($response);
return $result->getData();
}

/**
* 获取bucket所属的数据中心位置信息
*
* @param string $bucket
* @param array $options
* @throws OssException
* @return string
*/
public function getBucketLocation($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = '/';
$options[self::OSS_SUB_RESOURCE] = 'location';
$response = $this->auth($options);
$result = new GetLocationResult($response);
return $result->getData();
}

/**
* 获取Bucket的Meta信息
*
* @param string $bucket
* @param array $options 具体参考SDK文档
* @return array
*/
public function getBucketMeta($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_HEAD;
$options[self::OSS_OBJECT] = '/';
$response = $this->auth($options);
$result = new HeaderResult($response);
return $result->getData();
}

/**
* 获取bucket的ACL配置情况
Expand Down Expand Up @@ -903,6 +944,51 @@ public function getBucketReferer($bucket, $options = NULL)
$result = new GetRefererResult($response);
return $result->getData();
}

/**
* 设置bucket的容量大小,单位GB
* 当bucket的容量大于设置的容量时,禁止继续写入
*
* @param string $bucket bucket名称
* @param int $storageCapacity
* @param array $options
* @return ResponseCore
* @throws null
*/
public function putBucketStorageCapacity($bucket, $storageCapacity, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_OBJECT] = '/';
$options[self::OSS_SUB_RESOURCE] = 'qos';
$options[self::OSS_CONTENT_TYPE] = 'application/xml';
$storageCapacityConfig = new StorageCapacityConfig($storageCapacity);
$options[self::OSS_CONTENT] = $storageCapacityConfig->serializeToXml();
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}

/**
* 获取bucket的容量大小,单位GB
*
* @param string $bucket bucket名称
* @param array $options
* @throws OssException
* @return int
*/
public function getBucketStorageCapacity($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = '/';
$options[self::OSS_SUB_RESOURCE] = 'qos';
$response = $this->auth($options);
$result = new GetStorageCapacityResult($response);
return $result->getData();
}

/**
* 获取bucket下的object列表
Expand Down
30 changes: 30 additions & 0 deletions src/OSS/Result/GetLocationResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace OSS\Result;

use OSS\Core\OssException;

/**
* Class GetLocationResult getBucketLocation接口返回结果类,封装了
* 返回的xml数据的解析
*
* @package OSS\Result
*/
class GetLocationResult extends Result
{

/**
* Parse data from response
*
* @return string
* @throws OssException
*/
protected function parseDataFromResponse()
{
$content = $this->rawResponse->body;
if (empty($content)) {
throw new OssException("body is null");
}
$xml = simplexml_load_string($content);
return $xml;
}
}
34 changes: 34 additions & 0 deletions src/OSS/Result/GetStorageCapacityResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace OSS\Result;

use OSS\Core\OssException;

/**
* Class AclResult getBucketAcl接口返回结果类,封装了
* 返回的xml数据的解析
*
* @package OSS\Result
*/
class GetStorageCapacityResult extends Result
{
/**
* Parse data from response
*
* @return string
* @throws OssException
*/
protected function parseDataFromResponse()
{
$content = $this->rawResponse->body;
if (empty($content)) {
throw new OssException("body is null");
}
$xml = simplexml_load_string($content);
if (isset($xml->StorageCapacity)) {
return intval($xml->StorageCapacity);
} else {
throw new OssException("xml format exception");
}
}
}
55 changes: 55 additions & 0 deletions tests/OSS/Tests/OssClientBucketStorageCapacityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace OSS\Tests;

use OSS\Core\OssException;

require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';

class OssClientBucketStorageCapacityTest extends TestOssClientBase
{
public function testBucket()
{
try {
$storageCapacity = $this->ossClient->getBucketStorageCapacity($this->bucket);
$this->assertEquals($storageCapacity, - 1);
} catch (OssException $e) {
$this->assertTrue(false);
}

try {
$this->ossClient->putBucketStorageCapacity($this->bucket, 1000);
} catch (OssException $e) {
$this->assertTrue(false);
}

try {
Common::waitMetaSync();
$storageCapacity = $this->ossClient->getBucketStorageCapacity($this->bucket);
$this->assertEquals($storageCapacity, 1000);
} catch (OssException $e) {
$this->assertTrue(false);
}

try {
$this->ossClient->putBucketStorageCapacity($this->bucket, 0);

Common::waitMetaSync();

$storageCapacity = $this->ossClient->getBucketStorageCapacity($this->bucket);
$this->assertEquals($storageCapacity, 0);

$this->ossClient->putObject($this->bucket, 'test-storage-capacity','test-content');
$this->assertTrue(false);
} catch (OssException $e) {
$this->assertEquals('Bucket storage exceed max storage capacity.',$e->getErrorMessage());
}

try {
$this->ossClient->putBucketStorageCapacity($this->bucket, - 2);
$this->assertTrue(false);
} catch (OssException $e) {
$this->assertEquals(400, $e->getHTTPStatus());
$this->assertEquals('InvalidArgument', $e->getErrorCode());
}
}
}
8 changes: 8 additions & 0 deletions tests/OSS/Tests/OssClientBucketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@ public function testBucket()

$bucketListInfo = $this->ossClient->listBuckets();
$this->assertNotNull($bucketListInfo);

$bucketList = $bucketListInfo->getBucketList();
$this->assertTrue(is_array($bucketList));
$this->assertGreaterThan(0, count($bucketList));

$this->ossClient->putBucketAcl($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
Common::waitMetaSync();
$this->assertEquals($this->ossClient->getBucketAcl($this->bucket), OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);

$this->assertTrue($this->ossClient->doesBucketExist($this->bucket));
$this->assertFalse($this->ossClient->doesBucketExist($this->bucket . '-notexist'));

$this->assertEquals($this->ossClient->getBucketLocation($this->bucket), 'oss-us-west-1');

$res = $this->ossClient->getBucketMeta($this->bucket);
$this->assertEquals('200', $res['info']['http_code']);
$this->assertEquals('oss-us-west-1', $res['x-oss-bucket-region']);
}
}

0 comments on commit 6d1de8d

Please sign in to comment.