diff --git a/src/OSS/Model/BucketReplicationLocation.php b/src/OSS/Model/BucketReplicationLocation.php new file mode 100644 index 00000000..51a9fd5c --- /dev/null +++ b/src/OSS/Model/BucketReplicationLocation.php @@ -0,0 +1,45 @@ +locations = $locations; + $this->locationTransferTypes = $locationTransferTypes; + } + + + /** + * @return array + */ + public function getLocations(){ + return $this->locations; + } + + /** + * @return array + */ + public function getLocationTransferTypes(){ + return $this->locationTransferTypes; + } + +} + + diff --git a/src/OSS/Model/BucketReplicationProgress.php b/src/OSS/Model/BucketReplicationProgress.php new file mode 100644 index 00000000..edb45351 --- /dev/null +++ b/src/OSS/Model/BucketReplicationProgress.php @@ -0,0 +1,95 @@ +id = $id; + $this->prefixSet = $prefixSet; + $this->destination = $destination; + $this->action = $action; + $this->status = $status; + $this->historicalObjectReplication = $historicalObjectReplication; + $this->progress = $progress; + } + + /** + * @return string + */ + public function getId(){ + return $this->id; + } + + /** + * @return array + */ + public function getPrefixSet(){ + return $this->prefixSet; + } + + + /** + * @return string + */ + public function getAction(){ + return $this->action; + } + + /** + * @return array + */ + public function getDestination(){ + return $this->destination; + } + + /** + * @return string + */ + public function getStatus(){ + return $this->status; + } + + /** + * @return string + */ + public function getHistoricalObjectReplication(){ + return $this->historicalObjectReplication; + } + + + /** + * @return array + */ + public function getProgress(){ + return $this->progress; + } + +} + + diff --git a/src/OSS/Model/ReplicationConfig.php b/src/OSS/Model/ReplicationConfig.php new file mode 100644 index 00000000..2d8c6fc1 --- /dev/null +++ b/src/OSS/Model/ReplicationConfig.php @@ -0,0 +1,273 @@ +config['Rule'][] = $rule->rule; + } + + + /** + * @param $rule ReplicationRule + */ + public function setRule($rule){ + $this->rule[] = $rule; + } + + + /** + * @return ReplicationRule[] + */ + public function getRule(){ + return $this->rule; + } + + /** + * @param $configs array + */ + + public function addPrefixSet($prefix) + { + $this->config['PrefixSet']['Prefix'][] = $prefix; + } + + + /** + * @param $action string + */ + public function addAction($action) + { + $this->config['Action'] = $action; + } + + public function addDestination($destination) + { + $this->config['Destination'] = $destination->oSSBucketDestination;; + } + + /** + * @param $historicalObjectReplication boolean + */ + public function addHistoricalObjectReplication($historicalObjectReplication) + { + $this->config['HistoricalObjectReplication'] = $historicalObjectReplication; + } + + /** + * @param $syncRole string + */ + public function addSyncRole($syncRole) + { + $this->config['SyncRole']= $syncRole; + } + + + /** + * @param $sourceSelectionCriteria array + */ + + + /** + * @param $sourceSelectionCriteria ReplicationSourceSelectionCriteria + */ + public function addSourceSelectionCriteria($sourceSelectionCriteria) + { + $this->config['SourceSelectionCriteria'] = $sourceSelectionCriteria->sourceSelectionCriteria; + $this->config['EncryptionConfiguration'] = $sourceSelectionCriteria->encryptionConfiguration; + } + + /** + * Parse the xml into this object. + * + * @param string $strXml + * @throws OssException + * @return null + */ + public function parseFromXml($strXml) + { + $this->config = array(); + $xml = simplexml_load_string($strXml); + $xmlJson= json_encode($xml);//将对象转换个JSON + $xmlObj = json_decode($xmlJson,true); + if(isset($xmlObj['Rule'])){ + $this->config = $xmlObj['Rule']; + if (is_numeric(key($xmlObj['Rule']))){ + $this->parseRule($xmlObj['Rule']); + }else{ + $this->parseRule(array(0=>$xmlObj['Rule'])); + } + } + } + + /** + * @param $rules + */ + private function parseRule($rules) + { + if(isset($rules)){ + + foreach ($rules as $rule){ + $replicationRule = new ReplicationRule(); + if (isset($rule['ID'])){ + $replicationRule->setId($rule['ID']); + } + if (isset($rule['PrefixSet'])){ + foreach ($rule['PrefixSet']['Prefix'] as $prefix){ + $replicationRule->setPrefixSet($prefix); + } + } + if (isset($rule['Action'])){ + $replicationRule->setAction($rule['Action']); + } + if (isset($rule['Status'])){ + $replicationRule->setStatus($rule['Status']); + } + + if (isset($rule['Status'])){ + $replicationRule->setStatus($rule['Status']); + } + + if (isset($rule['Destination'])){ + $this->parseDestination($rule['Destination'],$replicationRule); + } + + if (isset($rule['HistoricalObjectReplication'])){ + $replicationRule->setHistoricalObjectReplication($rule['HistoricalObjectReplication']); + } + + if (isset($rule['SyncRole'])){ + $replicationRule->setSyncRole($rule['SyncRole']); + } + $this->setRule($replicationRule); + + } + } + + } + + + /** + * @param $rules + * @param $replicationRule ReplicationRule + */ + private function parseDestination($destination,&$replicationRule) + { + if(isset($destination)){ + $replicationDestination = new ReplicationDestination(); + if (isset($destination['Bucket'])){ + $replicationDestination->setBucket($destination['Bucket']); + } + if (isset($destination['Location'])){ + $replicationDestination->setLocation($destination['Location']); + } + if (isset($destination['TransferType'])){ + $replicationDestination->setTransferType($destination['TransferType']); + } + $replicationRule->setDestination($replicationDestination); + } + + } + + + /** + * Serialize the object to xml + * + * @return string + */ + public function serializeToXml() + { + return $this->arrToXml($this->config,null,null,'ReplicationConfiguration'); + } + + /** + * array turn to xml + * @param $arr + * @param $dom + * @param $item + * @param string $rootNode + * @return string + */ + public function arrToXml($arr,$dom,$item,$rootNode="Configuration") + { + if (!$dom) { + $dom = new \DOMDocument("1.0",'utf-8'); + } + if (!$item) { + $item = $dom->createElement($rootNode); + $dom->appendChild($item); + } + foreach ($arr as $key => $val) { + if(!is_string($key)){ + continue; + } + if ($key == 'Rule'){ + foreach ($val as $value){ + $node = $dom->createElement($key); + $item->appendChild($node); + $this->arrToXml($value, $dom, $node); + } + continue; + } + if($key != 'Prefix'){ + $node = $dom->createElement($key); + $item->appendChild($node); + }else{ + foreach ($val as $value){ + $node = $dom->createElement("Prefix",$value); + $item->appendChild($node); + } + } + if (!is_array($val)) { + if(is_bool($val)){ + if($val == true){ + $text = $dom->createTextNode('true'); + }else{ + $text = $dom->createTextNode('false'); + } + }else{ + $text = $dom->createTextNode($val); + } + $node->appendChild($text); + } else { + $this->arrToXml($val, $dom, $node); + } + } + return $dom->saveXML(null,LIBXML_NOEMPTYTAG); + } + + /** + * Serialize the object into xml string. + * + * @return string + */ + public function __toString() + { + return $this->serializeToXml(); + } + + /** + * @return array + */ + public function getConfig(){ + return $this->config; + } +} + + diff --git a/src/OSS/Model/ReplicationDestination.php b/src/OSS/Model/ReplicationDestination.php new file mode 100644 index 00000000..7588ff27 --- /dev/null +++ b/src/OSS/Model/ReplicationDestination.php @@ -0,0 +1,82 @@ +array( + 'Bucket'=>'target-bucket', + 'Location'=>'oss-cn-beijing', + 'TransferType'=>'oss_acc', + ) + ) + */ + public function __construct() + { + $this->destination = array(); + } + + + /** + * @param $bucket string bucket name + */ + public function setBucket($bucket) + { + $this->destination['Bucket'] = $bucket; + } + + /** + * @return string |null + */ + public function getBucket(){ + return isset($this->destination['Bucket']) ? $this->destination['Bucket'] : null; + } + + /** + * @param $location string + */ + public function setLocation($location) + { + $this->destination['Location'] = $location; + } + + /** + * @return string |null + */ + public function getLocation(){ + return isset($this->destination['Location']) ? $this->destination['Location'] : null; + } + /** + * @param $transferType string + */ + public function setTransferType($transferType) + { + $this->destination['TransferType'] = $transferType; + } + + /** + * @return string |null + */ + public function getTransferType(){ + return isset($this->destination['TransferType']) ? $this->destination['TransferType'] : null; + } + + /** + * @return array + */ + public function getDestination(){ + return $this->destination; + } + +} + + diff --git a/src/OSS/Model/ReplicationRule.php b/src/OSS/Model/ReplicationRule.php new file mode 100644 index 00000000..dc39e103 --- /dev/null +++ b/src/OSS/Model/ReplicationRule.php @@ -0,0 +1,157 @@ +rule = array(); + } + + /** + * @param $id string + */ + public function setId($id){ + $this->rule['ID'] = $id; + } + + /** + * @return string|null + */ + public function getId(){ + return isset($this->rule['ID']) ? $this->rule['ID'] : null; + } + + /** + * @param $prefix string + */ + public function setPrefixSet($prefix){ + $this->rule['PrefixSet']['Prefix'][] = $prefix; + } + + /** + * @return array|null + */ + public function getPrefixSet(){ + return isset($this->rule['PrefixSet']['Prefix']) ? $this->rule['PrefixSet']['Prefix'] : null; + } + + + /** + * @param $action string ALL|PUT + */ + public function setAction($action){ + $this->rule['Action'] = $action; + } + + /** + * @return string|null + */ + public function getAction(){ + return isset($this->rule['Action']) ? $this->rule['Action'] : null; + } + + + /** + * @param $status string starting|doing|closing + */ + public function setStatus($status){ + $this->rule['Status'] = $status; + } + + + /** + * @return string |null + */ + public function getStatus(){ + return isset($this->rule['Status']) ? $this->rule['Status'] : null; + } + + /** + * @param $destination ReplicationDestination + */ + public function addDestination($destination){ + $this->rule['Destination'] = $destination->destination; + } + + /** + * @return ReplicationDestination + */ + public function getDestination(){ + return $this->destination; + } + + /** + * @param $destination ReplicationDestination + */ + public function setDestination($destination){ + $this->destination = $destination; + } + + /** + * @param $status string enabled | disabled + */ + public function setHistoricalObjectReplication($status){ + $this->rule['HistoricalObjectReplication'] = $status; + } + + /** + * @return string |null + */ + public function getHistoricalObjectReplication(){ + return isset($this->rule['HistoricalObjectReplication']) ? $this->rule['HistoricalObjectReplication'] : null; + } + + /** + * @param $syncRole + */ + public function setSyncRole($syncRole){ + $this->rule['SyncRole'] = $syncRole; + } + + /** + * @return string |null + */ + public function getSyncRole(){ + return isset($this->rule['SyncRole']) ? $this->rule['SyncRole'] : null; + } + + + + /** + * @param $sourceSelectionCriteria ReplicationSourceSelectionCriteria + */ + public function addSourceSelectionCriteria($sourceSelectionCriteria){ + $this->rule['SourceSelectionCriteria'] = $sourceSelectionCriteria->sourceSelectionCriteria; + } + + + /** + * @param $sourceSelectionCriteria ReplicationSourceSelectionCriteria + */ + public function addEncryptionConfiguration($sourceSelectionCriteria){ + $this->rule['EncryptionConfiguration'] = $sourceSelectionCriteria->encryptionConfiguration; + } + + /** + * @return array + */ + public function getRule(){ + return $this->rule; + } + +} \ No newline at end of file diff --git a/src/OSS/Model/ReplicationSourceSelectionCriteria.php b/src/OSS/Model/ReplicationSourceSelectionCriteria.php new file mode 100644 index 00000000..8b819d1c --- /dev/null +++ b/src/OSS/Model/ReplicationSourceSelectionCriteria.php @@ -0,0 +1,49 @@ +array( + 'SseKmsEncryptedObjects'=>array( + * 'Status'=>"Enabled" + * ) + ) + */ + public function __construct() + { + $this->sourceSelectionCriteria = array(); + } + + /** + * @param $status string + */ + public function setStatus($status) + { + $this->sourceSelectionCriteria['SseKmsEncryptedObjects']['Status'] = $status; + } + + + /** + * @param $kmsId string + */ + public function setReplicaKmsKeyID($kmsId) + { + $this->encryptionConfiguration['ReplicaKmsKeyID'] = $kmsId; + } + +} + + diff --git a/src/OSS/OssClient.php b/src/OSS/OssClient.php index 0922a0b7..ea1343e0 100644 --- a/src/OSS/OssClient.php +++ b/src/OSS/OssClient.php @@ -6,15 +6,21 @@ use OSS\Http\RequestCore; use OSS\Http\RequestCore_Exception; use OSS\Http\ResponseCore; +use OSS\Model\BucketReplicationLocation; +use OSS\Model\BucketReplicationProgress; use OSS\Model\CorsConfig; use OSS\Model\CnameConfig; use OSS\Model\LoggingConfig; use OSS\Model\LiveChannelConfig; use OSS\Model\LiveChannelInfo; use OSS\Model\LiveChannelListInfo; +use OSS\Model\ReplicationConfig; use OSS\Model\StorageCapacityConfig; use OSS\Result\AclResult; use OSS\Result\BodyResult; +use OSS\Result\GetBucketReplicationLocationResult; +use OSS\Result\GetBucketReplicationProgressResult; +use OSS\Result\GetBucketReplicationResult; use OSS\Result\GetCorsResult; use OSS\Result\GetLifecycleResult; use OSS\Result\GetLocationResult; @@ -66,11 +72,7 @@ use OSS\Result\InitiateBucketWormResult; use OSS\Model\ExtendWormConfig; use OSS\Result\GetBucketWormResult; -use OSS\Model\RestoreConfig; -use OSS\Model\ObjectVersionListInfo; use OSS\Result\ListObjectVersionsResult; -use OSS\Model\DeleteObjectInfo; -use OSS\Model\DeletedObjectInfo; use OSS\Result\DeleteObjectVersionsResult; /** @@ -2578,8 +2580,118 @@ public function uploadDir($bucket, $prefix, $localDirectory, $exclude = '.|..|.s } return $retArray; } - - /** + + + /** + * Sets the bucket Replication rule + * + * @param string $bucket bucket name + * @param $replicationConfig ReplicationConfig + * @param array $options + * @throws OssException + * @return null + */ + public function putBucketReplication($bucket,$replicationConfig) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_POST; + $options[self::OSS_OBJECT] = '/'; + $options[self::OSS_SUB_RESOURCE] = 'replication&comp=add'; + $options[self::OSS_CONTENT_TYPE] = 'application/xml'; + $options[self::OSS_CONTENT] = $replicationConfig->serializeToXml(); + $response = $this->auth($options); + $result = new HeaderResult($response); + return $result->getData(); + } + + + /** + * Get the bucket Replication rule + * + * @param string $bucket bucket name + * @param array $options + * @throws OssException + * @return ReplicationConfig + */ + public function getBucketReplication($bucket) + { + $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] = 'replication'; + $response = $this->auth($options); + $result = new GetBucketReplicationResult($response); + return $result->getData(); + } + + + /** + * Get the bucket Replication location + * + * @param string $bucket bucket name + * @throws OssException + * @return BucketReplicationLocation + */ + public function getBucketReplicationLocation($bucket) + { + $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] = 'replicationLocation'; + $response = $this->auth($options); + $result = new GetBucketReplicationLocationResult($response); + return $result->getData(); + } + + /** + * Get the bucket Replication Progress + * + * @param string $bucket bucket name + * @throws OssException + * @return BucketReplicationProgress + */ + public function getBucketReplicationProgress($bucket,$ruleId) + { + $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] = 'replicationProgress'; + $options[self::OSS_QUERY_STRING] = array( + 'rule-id=' => $ruleId, + ); + $response = $this->auth($options); + $result = new GetBucketReplicationProgressResult($response); + return $result->getData(); + } + + + /** + * Delete the bucket Replication rule + * + * @param string $bucket bucket name + * @param string $replicationId replication id string + * @throws OssException + * @return null + */ + public function deleteBucketReplication($bucket,$replicationId) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_POST; + $options[self::OSS_OBJECT] = '/'; + $options[self::OSS_SUB_RESOURCE] = 'replication&comp=delete'; + $options[self::OSS_CONTENT_TYPE] = 'application/xml'; + $options[self::OSS_CONTENT] = ''.$replicationId.''; + $response = $this->auth($options); + $result = new HeaderResult($response); + return $result->getData(); + } + + /** * Sign URL with specified expiration time in seconds (timeout) and HTTP method. * The signed URL could be used to access the object directly. * diff --git a/src/OSS/Result/GetBucketReplicationLocationResult.php b/src/OSS/Result/GetBucketReplicationLocationResult.php new file mode 100644 index 00000000..429054ef --- /dev/null +++ b/src/OSS/Result/GetBucketReplicationLocationResult.php @@ -0,0 +1,49 @@ +rawResponse->body; + if (empty($content)) { + throw new OssException("body is null"); + } + $xml = new \SimpleXMLElement($this->rawResponse->body); + $locations = array(); + + if (isset($xml->Location) && isset($xml->Location)) { + foreach ($xml->Location as $location) { + $locations[] = strval($location); + } + } + $locationTypes = array(); + if (isset($xml->LocationTransferTypeConstraint) && isset($xml->LocationTransferTypeConstraint)) { + $objLocationTransferTypes = $xml->LocationTransferTypeConstraint; + $i = 0; + foreach ($objLocationTransferTypes->LocationTransferType as $key=> $type) { + $locationTypes[$i]['location'] = strval($type->Location); + $locationTypes[$i]['type'] = strval($type->TransferTypes->Type); + $i++; + } + } + + return new BucketReplicationLocation($locations,$locationTypes); + + } +} \ No newline at end of file diff --git a/src/OSS/Result/GetBucketReplicationProgressResult.php b/src/OSS/Result/GetBucketReplicationProgressResult.php new file mode 100644 index 00000000..8c175a32 --- /dev/null +++ b/src/OSS/Result/GetBucketReplicationProgressResult.php @@ -0,0 +1,43 @@ +rawResponse->body; + if (empty($content)) { + throw new OssException("body is null"); + } + $xml = new \SimpleXMLElement($this->rawResponse->body); + + if (isset($xml->Rule) && isset($xml->Rule)) { + $ruleObj = $xml->Rule; + $id = isset($ruleObj->ID) ? strval($ruleObj->ID) : ""; + $prefixSet = isset($ruleObj->PrefixSet) ? json_decode(json_encode($ruleObj->PrefixSet,LIBXML_NOCDATA),true) : ""; + $action = isset($ruleObj->Action) ? strval($ruleObj->Action) : ""; + $destination = isset($ruleObj->Destination) ? json_decode(json_encode($ruleObj->Destination,LIBXML_NOCDATA),true) : ""; + $historicalObjectReplication = isset($ruleObj->HistoricalObjectReplication) ? strval($ruleObj->HistoricalObjectReplication) : ""; + $status = isset($ruleObj->Status) ? strval($ruleObj->Status) : ""; + $progress = isset($ruleObj->Progress) ? json_decode(json_encode($ruleObj->Progress,LIBXML_NOCDATA),true) : ""; + return new BucketReplicationProgress($id,$prefixSet,$action,$destination,$status,$historicalObjectReplication,$progress); + } + } +} \ No newline at end of file diff --git a/src/OSS/Result/GetBucketReplicationResult.php b/src/OSS/Result/GetBucketReplicationResult.php new file mode 100644 index 00000000..1b77dc47 --- /dev/null +++ b/src/OSS/Result/GetBucketReplicationResult.php @@ -0,0 +1,33 @@ +rawResponse->body; + if (empty($content)) { + throw new OssException("body is null"); + } + + $replicationConfig = new ReplicationConfig(); + $replicationConfig->parseFromXml($content); + return $replicationConfig; + } + +} \ No newline at end of file diff --git a/tests/OSS/Tests/GetBucketReplicationLocationResultTest.php b/tests/OSS/Tests/GetBucketReplicationLocationResultTest.php new file mode 100644 index 00000000..6675b0ab --- /dev/null +++ b/tests/OSS/Tests/GetBucketReplicationLocationResultTest.php @@ -0,0 +1,74 @@ + + + oss-cn-beijing + oss-cn-qingdao + + + oss-cn-hongkong + + oss_acc + + + + oss-us-west-1 + + oss_acc + + + + +BBBB; + private $invalidXml = << + +BBBB; + + public function testValidXml() + { + $response = new ResponseCore(array(), $this->validXml, 200); + $result = new GetBucketReplicationLocationResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $this->assertNotNull($result->getRawResponse()->body); + $info = $result->getData(); + + if ($info->getLocations()){ + $this->assertEquals("oss-cn-beijing", $info->getLocations()[0]); + $this->assertEquals("oss-cn-qingdao", $info->getLocations()[1]); + + } + + + if ($info->getLocationTransferTypes()){ + $this->assertEquals("oss-cn-hongkong", $info->getLocationTransferTypes()[0]['location']); + $this->assertEquals("oss-us-west-1", $info->getLocationTransferTypes()[1]['location']); + } + } + + public function testInvalidXml() + { + $response = new ResponseCore(array(), $this->invalidXml, 200); + $result = new GetBucketReplicationLocationResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $this->assertNotNull($result->getRawResponse()->body); + } + + private function cleanXml($xml) + { + return str_replace("\n", "", str_replace("\r", "", $xml)); + } +} diff --git a/tests/OSS/Tests/GetBucketReplicationProgressResultTest.php b/tests/OSS/Tests/GetBucketReplicationProgressResultTest.php new file mode 100644 index 00000000..7c01bb8a --- /dev/null +++ b/tests/OSS/Tests/GetBucketReplicationProgressResultTest.php @@ -0,0 +1,81 @@ + + + + test_replication_1 + + source_image + video + + PUT + + target-bucket + oss-cn-beijing + oss_acc + + doing + enabled + + 0.85 + 2015-09-24T15:28:14.000Z + + + +BBBB; + private $invalidXml = << + +BBBB; + + public function testValidXml() + { + $response = new ResponseCore(array(), $this->validXml, 200); + $result = new GetBucketReplicationProgressResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $this->assertNotNull($result->getRawResponse()->body); + $info = $result->getData(); + + $this->assertEquals("test_replication_1", $info->getId()); + + if ($info->getPrefixSet()['Prefix']) { + $this->assertEquals("source_image", $info->getPrefixSet()['Prefix'][0]); + $this->assertEquals("video", $info->getPrefixSet()['Prefix'][1]); + } + + $this->assertEquals("PUT", $info->getAction()); + $this->assertEquals("doing", $info->getStatus()); + $this->assertEquals("enabled", $info->getHistoricalObjectReplication()); + + $destination = $info->getDestination(); + $this->assertEquals("target-bucket", $destination['Bucket']); + $this->assertEquals("oss-cn-beijing", $destination['Location']); + $this->assertEquals("oss_acc", $destination['TransferType']); + } + + public function testInvalidXml() + { + $response = new ResponseCore(array(), $this->invalidXml, 200); + $result = new GetBucketReplicationProgressResult($response); + $this->assertTrue($result->isOK()); + $this->assertNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $this->assertNotNull($result->getRawResponse()->body); + } + + private function cleanXml($xml) + { + return str_replace("\n", "", str_replace("\r", "", $xml)); + } +} diff --git a/tests/OSS/Tests/GetBucketReplicationResultTest.php b/tests/OSS/Tests/GetBucketReplicationResultTest.php new file mode 100644 index 00000000..e1829243 --- /dev/null +++ b/tests/OSS/Tests/GetBucketReplicationResultTest.php @@ -0,0 +1,107 @@ + + + + test_replication_1 + + source_image + video + + PUT + + target-bucket + oss-cn-beijing + oss_acc + + doing + enabled + aliyunramrole + + + test_replication_2 + + source_image + video + + PUT + + target-bucket-2 + oss-cn-shanghai + oss_acc + + doing + enabled + aliyunramrole + + +BBBB; + private $invalidXml = << + +BBBB; + + public function testValidXml() + { + $response = new ResponseCore(array(), $this->validXml, 200); + $result = new GetBucketReplicationResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $this->assertNotNull($result->getRawResponse()->body); + $lists = $result->getData(); + $rule = $lists->getRule()[0]; + + $this->assertEquals("test_replication_1", $rule->getId()); + $this->assertEquals("PUT", $rule->getAction()); + $this->assertEquals("doing", $rule->getStatus()); + $this->assertEquals("enabled", $rule->getHistoricalObjectReplication()); + $this->assertEquals("aliyunramrole", $rule->getSyncRole()); + + $ruleOne = $lists->getRule()[1]; + $this->assertEquals("test_replication_2", $ruleOne->getId()); + $this->assertEquals("PUT", $ruleOne->getAction()); + $this->assertEquals("doing", $ruleOne->getStatus()); + $this->assertEquals("enabled", $ruleOne->getHistoricalObjectReplication()); + $this->assertEquals("aliyunramrole", $ruleOne->getSyncRole()); + + + if ($rule->getPrefixSet()['Prefix']) { + $this->assertEquals("source_image", $rule->getPrefixSet()['Prefix'][0]); + $this->assertEquals("video", $rule->getPrefixSet()['Prefix'][1]); + } + + + $destination = $rule->getDestination(); + + $this->assertEquals("target-bucket", $destination->getBucket()); + $this->assertEquals("oss-cn-beijing", $destination->getLocation()); + $this->assertEquals("oss_acc", $destination->getTransferType()); + } + + public function testInvalidXml() + { + $response = new ResponseCore(array(), $this->invalidXml, 200); + $result = new GetBucketReplicationResult($response); + $this->assertTrue($result->isOK()); + $this->assertEquals(array(),$result->getData()->getConfig()); + $this->assertNotNull($result->getRawResponse()); + $this->assertNotNull($result->getRawResponse()->body); + } + + private function cleanXml($xml) + { + return str_replace("\n", "", str_replace("\r", "", $xml)); + } +} diff --git a/tests/OSS/Tests/OssClientBucketReplicationTest.php b/tests/OSS/Tests/OssClientBucketReplicationTest.php new file mode 100644 index 00000000..1c16ac26 --- /dev/null +++ b/tests/OSS/Tests/OssClientBucketReplicationTest.php @@ -0,0 +1,91 @@ +setPrefixSet('prefix_5'); + $rule->setPrefixSet('prefix_6'); + $rule->setAction(ReplicationRule::ACTION_PUT); + $rule->setHistoricalObjectReplication('enabled'); + $rule->setSyncRole('aliyunramrole'); + $destination = new ReplicationDestination(); + $destination->setBucket('test-demo5'); + $destination->setLocation('oss-cn-beijing'); + $destination->setTransferType('internal'); + $rule->addDestination($destination); + $criteria = new ReplicationSourceSelectionCriteria(); + $criteria->setStatus("Enabled"); + $rule->addSourceSelectionCriteria($criteria); + $criteria->setReplicaKmsKeyID("c4d49f85-ee30-426b-a5ed-95e9139d"); + $rule->addEncryptionConfiguration($criteria); + $replicationConfig->addRule($rule); + + try { + $this->ossClient->putBucketReplication($this->bucket, $replicationConfig); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + + try { + Common::waitMetaSync(); + $replicationConfig2 = $this->ossClient->getBucketReplication($this->bucket); + var_dump($replicationConfig2->serializeToXml()); + } catch (OssException $e) { + $this->assertTrue(false); + } + + try { + $result = $this->ossClient->getBucketReplicationLocation($this->bucket); + var_dump($result); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + + + try { + $result = $this->ossClient->getBucketReplicationProgress($this->bucket,'68331f6c-dff8-42aa-8c09-750f670b2323'); + var_dump($result); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + + } + + public function testDeleteBucketReplication(){ + $id = "3358cd89-1d43-4b5a-963c-aee9cf8dcde6"; + try { + Common::waitMetaSync(); + $result = $this->ossClient->deleteBucketReplication($this->bucket,$id); + var_dump($result); + } catch (OssException $e) { + $this->assertTrue(false); + } + } + + + public function testBucketReplicationLocation(){ + try { + $result = $this->ossClient->getBucketReplicationLocation($this->bucket); + var_dump($result); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + } + +} diff --git a/tests/OSS/Tests/ReplicationConfigTest.php b/tests/OSS/Tests/ReplicationConfigTest.php new file mode 100644 index 00000000..3cdd1b0c --- /dev/null +++ b/tests/OSS/Tests/ReplicationConfigTest.php @@ -0,0 +1,73 @@ + + + + +prefix_1 +prefix_2 + +ALL + +Target Bucket Name +oss-cn-hangzhou +oss_acc + +enabled + + +BBBB; + private $invalidXml = << + +BBBB; + + public function testValidXmlOne() + { + $replicationConfig = new ReplicationConfig(); + $replicationConfig->addPrefixSet('prefix_1'); + $replicationConfig->addPrefixSet('prefix_2'); + $replicationConfig->addAction('ALL'); + $replicationOssBucketDestination = new ReplicationDestination(); + $replicationOssBucketDestination->addBucket('Target Bucket Name'); + $replicationOssBucketDestination->addLocation('oss-cn-hangzhou'); + $replicationOssBucketDestination->addTransferType('oss_acc'); + $replicationConfig->addDestination($replicationOssBucketDestination); + + // Specify whether to copy historical data + $replicationConfig->addHistoricalObjectReplication('enabled'); + + $this->assertEquals($this->cleanXml($replicationConfig->serializeToXml()), $this->cleanXml($this->validXml)); + } + + public function testInvalidXmlOne() + { + $replicationConfig = new ReplicationConfig(); + $replicationConfig->parseFromXml($this->cleanXml($this->invalidXml)); + $this->assertEquals($this->cleanXml($this->invalidXml), $this->cleanXml($replicationConfig->serializeToXml())); + } + + public function testValidXmlTwo() + { + $replicationConfig = new ReplicationConfig(); + + $replicationConfig->parseFromXml($this->cleanXml($this->validXml)); + + $this->assertEquals($this->cleanXml($replicationConfig->serializeToXml()), $this->cleanXml($this->validXml)); + } + + + private function cleanXml($xml) + { + return str_replace("\n", "", str_replace("\r", "", $xml)); + } +}