diff --git a/samples/BucketReplication.php b/samples/BucketReplication.php new file mode 100644 index 00000000..4c707922 --- /dev/null +++ b/samples/BucketReplication.php @@ -0,0 +1,396 @@ +setPrefixSet('prefix_5'); +$rule->setPrefixSet('prefix_6'); +// Specify the operations that can be copied to the target bucket (ALL or PUT) +$rule->setAction(ReplicationRule::ACTION_PUT); +// Specify whether to copy historical data +$rule->setHistoricalObjectReplication('enabled'); +// Which role is authorized for OSS to use for data replication +$rule->setSyncRole('aliyunramrole'); +$destination = new ReplicationDestination(); +// Specify the target bucket to which the data will be copied。 +$destination->setBucket('test-demo5'); +// The region where the target bucket is located。 +$destination->setLocation('oss-cn-beijing'); +// Specify the data transmission link used when data is copied. internal (default) oss_acc: transmission acceleration link +$destination->setTransferType('internal'); + +$rule->addDestination($destination); + +$criteria = new ReplicationSourceSelectionCriteria(); + +// Specify whether OSS copies objects created through SSE-KMS encryption +$criteria->setStatus("Enabled"); +$rule->addSourceSelectionCriteria($criteria); +// Specify SSE-KMS key ID. If you specify Status as Enabled, you must specify this element +$encryption = new ReplicationEncryptionConfiguration(); +$encryption->setReplicaKmsKeyID("c4d49f85-ee30-426b-a5ed-95e9139d"); +$rule->addEncryptionConfiguration($encryption); + +$replicationConfig->addRule($rule); +$ossClient->putBucketReplication($bucket,$replicationConfig); +Common::println("Bucket replication has created"); + +//The existing cross-region replication rules are turned on or off +$ossClient->putBucketRtc($bucket,"disabled","test_replication_rule_id"); +printf("Bucket replication has closed"); + +// Get bucket Replication rule +$replicationResult = $ossClient->getBucketReplication($bucket); +Common::println("===Replication Rule start ==="); +foreach ($replicationResult->getRules() as $rule) { + if ($rule->getId()){ + Common::println("Replication Id:". $rule->getId().PHP_EOL); + } + if ($rule->getPrefixSet()){ + foreach ($rule->getPrefixSet() as $prefix){ + Common::println("Replication Prefix: ".$prefix.PHP_EOL); + } + } + if ($rule->getRTC()){ + Common::println("Replication RTC Status: ".$rule->getRTC().PHP_EOL); + } + if ($rule->getAction()){ + Common::println("Replication Action:". $rule->getAction().PHP_EOL); + } + if ($rule->getDestination()){ + Common::println("Replication Action:". $rule->getDestination()->getBucket().PHP_EOL); + Common::println("Replication Target Bucket Location: ". $rule->getDestination()->getLocation().PHP_EOL); + if($rule->getDestination()->getTransferType()) { + Common::println("Replication Target Bucket TransferType: " . $rule->getDestination()->getTransferType() . PHP_EOL); + } + } + if ($rule->getSourceSelectionCriteria()){ + Common::println("Replication Source Selection Criteria Status:". $rule->getSourceSelectionCriteria()->getStatus().PHP_EOL); + } + + if ($rule->getEncryptionConfiguration()){ + Common::println("Replication Encryption Configuration Kms Key Id:". $rule->getEncryptionConfiguration()->getReplicaKmsKeyID().PHP_EOL); + } + Common::println("Replication HistoricalObjectReplication:". $rule->getHistoricalObjectReplication().PHP_EOL); + Common::println("Replication SyncRole: ". $rule->getSyncRole().PHP_EOL); + Common::println("Replication Status: ". $rule->getStatus().PHP_EOL); +} +Common::println("===Replication Rule End ==="); + +// Get Bucket Replication Location +$replicationResult = $ossClient->getBucketReplicationLocation($bucket); + +if($replicationResult){ + Common::println("=====================Bucket replication location start=================================".PHP_EOL); + if ($replicationResult->getLocations()){ + foreach ($replicationResult->getLocations() as $location){ + Common::println("Bucket replication location is ".$location.PHP_EOL); + } + } + + if ($replicationResult->getLocationTransferTypes()){ + foreach ($replicationResult->getLocationTransferTypes() as $type){ + Common::println("Bucket replication location LocationTransferType location is: ".$type['location'].PHP_EOL); + Common::println("Bucket replication location LocationTransferType type is: ".$type['type'].PHP_EOL); + } + } + + Common::println("========================Bucket replication location end ============================".PHP_EOL); +} + +// Get Bucket Replication Progress +$result = $ossClient->getBucketReplicationProgress($bucket,'test-replication-id'); + +$replicationProcessResult = $result->getRule(); +if($replicationProcessResult){ + Common::println("=====================Bucket replication process start=================================".PHP_EOL); + Common::println("Bucket replication process id is ".$replicationProcessResult->getId().PHP_EOL); + if($replicationProcessResult->getPrefixSet()){ + $prefixSet = $replicationProcessResult->getPrefixSet(); + foreach ($prefixSet as $prefix){ + Common::println("Bucket replication process prefix is: ".$prefix.PHP_EOL); + } + } + Common::println("Bucket replication process action is ".$replicationProcessResult->getAction().PHP_EOL); + if($replicationProcessResult->getDestination()){ + $destination = $replicationProcessResult->getDestination(); + Common::println("Bucket replication process bucket name is: ".$destination->getBucket().PHP_EOL); + Common::println("Bucket replication process bucket location is: ".$destination->getLocation().PHP_EOL); + Common::println("Bucket replication process Prefix transfer type is: ".$destination->getTransferType().PHP_EOL); + } + if ($replicationProcessResult->getTransferType()){ + Common::println("Bucket replication process transfer type is ".$replicationProcessResult->getTransferType().PHP_EOL); + } + + if($replicationProcessResult->getProgress()){ + $progress = $replicationProcessResult->getProgress(); + Common::println("Bucket replication process HistoricalObject is: ".$progress->getHistoricalObject().PHP_EOL); + Common::println("Bucket replication process NewObject is: ".$progress->getNewObject().PHP_EOL); + } + Common::println("========================Bucket replication process end ============================".PHP_EOL); +} + + +// Delete Bucket replication by ID +$ossClient->deleteBucketReplication($bucket,"test_replication_1"); +Common::println("Bucket replication test_replication_1 has deleted"); + + +//******************************* For complete usage, see the following functions **************************************************** + +putBucketReplication($ossClient, $bucket); +putBucketRtc($ossClient, $bucket); +getBucketReplication($ossClient, $bucket); +deleteBucketReplication($ossClient, $bucket); +getBucketReplicationLocation($ossClient, $bucket); +getBucketReplicationProgress($ossClient, $bucket); + + +/** + * Sets bucket replication rule + * + * @param $ossClient OssClient + * @param $bucket string bucket name + * @return null + */ +function putBucketReplication($ossClient, $bucket) +{ + try { + $replicationConfig = new ReplicationConfig(); + + $rule = new ReplicationRule(); + // Set the prefix of the object to be copied + $rule->setPrefixSet('prefix_5'); + $rule->setPrefixSet('prefix_6'); + // Specify the operations that can be copied to the target bucket (ALL or PUT) + $rule->setAction(ReplicationRule::ACTION_PUT); + // Specify whether to copy historical data + $rule->setHistoricalObjectReplication('enabled'); + // Which role is authorized for OSS to use for data replication + $rule->setSyncRole('aliyunramrole'); + $destination = new ReplicationDestination(); + // Specify the target bucket to which the data will be copied。 + $destination->setBucket('test-demo5'); + // The region where the target bucket is located。 + $destination->setLocation('oss-cn-beijing'); + // Specify the data transmission link used when data is copied. internal (default) oss_acc: transmission acceleration link + $destination->setTransferType('internal'); + + $rule->addDestination($destination); + $criteria = new ReplicationSourceSelectionCriteria(); + + // Specify whether OSS copies objects created through SSE-KMS encryption + $criteria->setStatus("Disabled"); + $rule->addSourceSelectionCriteria($criteria); + // Specify SSE-KMS key ID. If you specify Status as Enabled, you must specify this element + $encryption = new ReplicationEncryptionConfiguration(); + $encryption->setReplicaKmsKeyID("c4d49f85-ee30-426b-a5ed-95e9139d"); + $rule->addEncryptionConfiguration($encryption); + + $replicationConfig->addRule($rule); + $ossClient->putBucketReplication($bucket,$replicationConfig); + printf("Bucket replication has created"); + }catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} + +/** + * Turn replication rules on or off + * + * @param $ossClient OssClient + * @param $bucket string bucket name + * @return null + * @throws RequestCore_Exception + */ +function putBucketRtc($ossClient, $bucket) +{ + try { + $ossClient->putBucketRtc($bucket,"disabled","test_replication_rule_id"); + printf("Bucket replication has closed"); + }catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} + + +/** + * Get bucket replication rule + * + * @param OssClient $ossClient OssClient instance + * @param string $bucket bucket name + * @return null + */ +function getBucketReplication($ossClient, $bucket) +{ + try { + $replicationResult = $ossClient->getBucketReplication($bucket); + printf("===Replication Rule start ==="); + foreach ($replicationResult->getRules() as $rule) { + if ($rule->getId()){ + Common::println("Replication Id:". $rule->getId().PHP_EOL); + } + if ($rule->getPrefixSet()){ + foreach ($rule->getPrefixSet() as $prefix){ + Common::println("Replication Prefix: ".$prefix.PHP_EOL); + } + } + if ($rule->getRTC()){ + Common::println("Replication RTC Status: ".$rule->getRTC().PHP_EOL); + } + if ($rule->getAction()){ + Common::println("Replication Action:". $rule->getAction().PHP_EOL); + } + + if ($rule->getDestination()){ + Common::println("Replication Action:". $rule->getDestination()->getBucket().PHP_EOL); + Common::println("Replication Target Bucket Location: ". $rule->getDestination()->getLocation().PHP_EOL); + if($rule->getDestination()->getTransferType()) { + Common::println("Replication Target Bucket TransferType: " . $rule->getDestination()->getTransferType() . PHP_EOL); + } + } + if ($rule->getSourceSelectionCriteria()){ + Common::println("Replication Source Selection Criteria Status:". $rule->getSourceSelectionCriteria()->getStatus().PHP_EOL); + } + + if ($rule->getEncryptionConfiguration()){ + Common::println("Replication Encryption Configuration Kms Key Id:". $rule->getEncryptionConfiguration()->getReplicaKmsKeyID().PHP_EOL); + } + Common::println("Replication HistoricalObjectReplication:". $rule->getHistoricalObjectReplication().PHP_EOL); + Common::println("Replication SyncRole: ". $rule->getSyncRole().PHP_EOL); + Common::println("Replication Status: ". $rule->getStatus().PHP_EOL); + } + printf("===Replication Rule End ==="); + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} + + +/** + * Get bucket replication location + * + * @param OssClient $ossClient OssClient instance + * @param string $bucket bucket name + * @return null + */ +function getBucketReplicationLocation($ossClient, $bucket){ + try { + $replicationResult = $ossClient->getBucketReplicationLocation($bucket); + printf("=====================Bucket replication location start=================================".PHP_EOL); + if ($replicationResult->getLocations()){ + foreach ($replicationResult->getLocations() as $location){ + printf("Bucket replication location is ".$location.PHP_EOL); + } + } + + if ($replicationResult->getLocationTransferTypes()){ + foreach ($replicationResult->getLocationTransferTypes() as $type){ + printf("Bucket replication location LocationTransferType location is: ".$type['location'].PHP_EOL); + printf("Bucket replication location LocationTransferType type is: ".$type['type'].PHP_EOL); + } + } + printf("========================Bucket replication location end ============================".PHP_EOL); + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); + +} + +/** + * Get bucket replication progress + * + * @param OssClient $ossClient OssClient instance + * @param string $bucket bucket name + * @return null + * @throws RequestCore_Exception + */ +function getBucketReplicationProgress($ossClient, $bucket){ + try { + $result = $ossClient->getBucketReplicationProgress($bucket,"test-replication-id"); + $replicationProcessResult = $result->getRule(); + printf("=====================Bucket replication progress start=================================".PHP_EOL); + printf("Bucket replication process id is ".$replicationProcessResult->getId().PHP_EOL); + if($replicationProcessResult->getPrefixSet()){ + $prefixSet = $replicationProcessResult->getPrefixSet(); + foreach ($prefixSet['Prefix'] as $prefix){ + Common::println("Bucket replication process prefix is: ".$prefix.PHP_EOL); + } + } + Common::println("Bucket replication process action is ".$replicationProcessResult->getAction().PHP_EOL); + if($replicationProcessResult->getDestination()){ + $destination = $replicationProcessResult->getDestination(); + printf("Bucket replication process bucket name is: ".$destination->getBucket().PHP_EOL); + printf("Bucket replication process bucket location is: ".$destination->getLocation().PHP_EOL); + printf("Bucket replication process Prefix transfer type is: ".$destination->getTransferType().PHP_EOL); + } + if ($replicationProcessResult->getTransferType()){ + Common::println("Bucket replication process transfer type is ".$replicationProcessResult->getTransferType().PHP_EOL); + } + printf("Bucket replication process status is ".$replicationProcessResult->getStatus().PHP_EOL); + printf("Bucket replication process historicalObjectReplication is: ".$replicationProcessResult->getHistoricalObjectReplication().PHP_EOL); + + if($replicationProcessResult->getProgress()){ + $progress = $replicationProcessResult->getProgress(); + printf("Bucket replication process HistoricalObject is: ".$progress->getHistoricalObject().PHP_EOL); + printf("Bucket replication process NewObject is: ".$progress->getNewObject().PHP_EOL); + } + printf("========================Bucket replication progress end ============================".PHP_EOL); + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} + +/** + * Delete bucket replication rule + * @param OssClient $ossClient OssClient instance + * @param string $bucket bucket name + * @return null + */ +function deleteBucketReplication($ossClient, $bucket) +{ + $ruleId = 'test_replication_id'; + try { + $ossClient->deleteBucketReplication($bucket,$ruleId); + printf("$bucket replication rule has deleted"); + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} + diff --git a/samples/RunAll.php b/samples/RunAll.php index 0cd7ec16..82ee6dfc 100644 --- a/samples/RunAll.php +++ b/samples/RunAll.php @@ -8,6 +8,7 @@ require_once __DIR__ . '/BucketReferer.php'; require_once __DIR__ . '/BucketLogging.php'; require_once __DIR__ . '/BucketWebsite.php'; +require_once __DIR__ . '/BucketReplication.php'; require_once __DIR__ . '/Signature.php'; require_once __DIR__ . '/Object1.php'; require_once __DIR__ . '/MultipartUpload.php'; \ No newline at end of file diff --git a/src/OSS/Model/ReplicationConfig.php b/src/OSS/Model/ReplicationConfig.php new file mode 100644 index 00000000..c00252a3 --- /dev/null +++ b/src/OSS/Model/ReplicationConfig.php @@ -0,0 +1,177 @@ +rule[] = $rule; + } + + /** + * @return ReplicationRule[] + */ + public function getRules(){ + return $this->rule; + } + + /** + * Parse the xml into this object. + * + * @param string $strXml + * @throws OssException + * @return null + */ + public function parseFromXml($strXml) + { + $this->rule = array(); + $xml = simplexml_load_string($strXml); + if (!isset($xml->Rule)) return; + $this->parseRule($xml->Rule); + } + + /** + * @param $rules + */ + private function parseRule($rules) + { + if(isset($rules)){ + foreach ($rules as $rule){ + $replicationRule = new ReplicationRule(); + if (isset($rule->ID)){ + $replicationRule->setId(strval($rule->ID)); + } + if (isset($rule->PrefixSet)){ + foreach ($rule->PrefixSet->Prefix as $prefix){ + $replicationRule->setPrefixSet(strval($prefix)); + } + } + if (isset($rule->Action)){ + $replicationRule->setAction(strval($rule->Action)); + } + if (isset($rule->Destination)){ + $this->parseDestination($rule->Destination,$replicationRule); + } + if (isset($rule->Status)){ + $replicationRule->setStatus(strval($rule->Status)); + } + if (isset($rule->HistoricalObjectReplication)){ + $replicationRule->setHistoricalObjectReplication(strval($rule->HistoricalObjectReplication)); + } + if (isset($rule->SyncRole)){ + $replicationRule->setSyncRole(strval($rule->SyncRole)); + } + if (isset($rule->SourceSelectionCriteria)){ + $this->parseSourceSelectionCriteria($rule->SourceSelectionCriteria,$replicationRule); + } + if (isset($rule->EncryptionConfiguration)){ + $this->parseEncryptionConfiguration($rule->EncryptionConfiguration,$replicationRule); + } + if (isset($rule->RTC->Status)){ + $replicationRule->setRTC(strval($rule->RTC->Status)); + } + $this->addRule($replicationRule); + } + } + + } + + /** + * @param $destination array + * @param $replicationRule ReplicationRule + */ + private function parseDestination($destination,&$replicationRule) + { + if(isset($destination)){ + $replicationDestination = new ReplicationDestination(); + if (isset($destination->Bucket)){ + $replicationDestination->setBucket(strval($destination->Bucket)); + } + if (isset($destination->Location)){ + $replicationDestination->setLocation(strval($destination->Location)); + } + if (isset($destination->TransferType)){ + $replicationDestination->setTransferType(strval($destination->TransferType)); + } + $replicationRule->addDestination($replicationDestination); + } + + } + + /** + * @param $sourceSelectionCriteria array + * @param $replicationRule ReplicationRule + */ + private function parseSourceSelectionCriteria($sourceSelectionCriteria,&$replicationRule) + { + if(isset($sourceSelectionCriteria)){ + $replicationSourceSelectionCriteria = new ReplicationSourceSelectionCriteria(); + if (isset($sourceSelectionCriteria->SseKmsEncryptedObjects->Status)){ + $replicationSourceSelectionCriteria->setStatus(strval($sourceSelectionCriteria->SseKmsEncryptedObjects->Status)); + } + $replicationRule->addSourceSelectionCriteria($replicationSourceSelectionCriteria); + } + + } + + /** + * @param $encryptionConfiguration array + * @param $replicationRule ReplicationRule + */ + private function parseEncryptionConfiguration($encryptionConfiguration,&$replicationRule) + { + if(isset($encryptionConfiguration)){ + $replicationEncryptionConfiguration = new ReplicationEncryptionConfiguration(); + if (isset($encryptionConfiguration->ReplicaKmsKeyID)){ + $replicationEncryptionConfiguration->setReplicaKmsKeyID(strval($encryptionConfiguration->ReplicaKmsKeyID)); + } + $replicationRule->addEncryptionConfiguration($replicationEncryptionConfiguration); + } + } + + /** + * Serialize the object to xml + * + * @return string + */ + public function serializeToXml() + { + + $xml = new \SimpleXMLElement(''); + foreach ($this->rule as $rule) { + $xmlRule = $xml->addChild('Rule'); + $rule->appendToXml($xmlRule); + } + return $xml->asXML(); + } + + /** + * Serialize the object into xml string. + * + * @return string + */ + public function __toString() + { + return $this->serializeToXml(); + } + +} + + diff --git a/src/OSS/Model/ReplicationDestination.php b/src/OSS/Model/ReplicationDestination.php new file mode 100644 index 00000000..165d9b2c --- /dev/null +++ b/src/OSS/Model/ReplicationDestination.php @@ -0,0 +1,82 @@ +bucket = $bucket; + } + + /** + * @return string |null + */ + public function getBucket(){ + return $this->bucket; + } + + /** + * @param $location string + */ + public function setLocation($location) + { + $this->location = $location; + } + + /** + * @return string |null + */ + public function getLocation(){ + return $this->location; + } + /** + * @param $transferType string + */ + public function setTransferType($transferType) + { + $this->transferType = $transferType; + } + + /** + * @return string |null + */ + public function getTransferType(){ + return $this->transferType; + } + + /** + * @param \SimpleXMLElement $xmlRule + */ + public function appendToXml(&$xmlRule) + { + if (isset($this->bucket) || isset($this->location) || isset($this->transferType)){ + $xmlDestination = $xmlRule->addChild('Destination'); + } + if (isset($this->bucket)){ + $xmlDestination->addChild('Bucket', $this->bucket); + } + if (isset($this->location)){ + $xmlDestination->addChild('Location', $this->location); + } + if (isset($this->transferType)){ + $xmlDestination->addChild('TransferType', $this->transferType); + } + } +} + + diff --git a/src/OSS/Model/ReplicationEncryptionConfiguration.php b/src/OSS/Model/ReplicationEncryptionConfiguration.php new file mode 100644 index 00000000..ace6c274 --- /dev/null +++ b/src/OSS/Model/ReplicationEncryptionConfiguration.php @@ -0,0 +1,35 @@ +replicaKmsKeyID = $kmsId; + } + + + /** + * @return string + */ + public function getReplicaKmsKeyID(){ + return $this->replicaKmsKeyID; + } + +} + + diff --git a/src/OSS/Model/ReplicationLocation.php b/src/OSS/Model/ReplicationLocation.php new file mode 100644 index 00000000..b78fbc33 --- /dev/null +++ b/src/OSS/Model/ReplicationLocation.php @@ -0,0 +1,119 @@ +replicationLocation['location'][] = $location; + } + + /** + * @return mixed + */ + public function getLocations(){ + return $this->replicationLocation['location']; + } + /** + * @return mixed + */ + public function getLocationTransferTypes(){ + return $this->replicationLocation['LocationTransferTypeConstraint']; + } + + /** + * @param $locationTransferType array + */ + public function setLocationTransferTypes($locationTransferType){ + $this->replicationLocation['LocationTransferTypeConstraint'] = $locationTransferType; + } + + + /** + * Parse the xml into this object. + * + * @param string $strXml + * @return null + */ + public function parseFromXml($strXml) + { + $this->replicationLocation = array(); + $xml = simplexml_load_string($strXml); + if (isset($xml->Location)){ + foreach ($xml->Location as $location){ + $this->setLocations(strval($location)); + } + } + if (isset($xml->LocationTransferTypeConstraint)){ + $locationTypes = array(); + $objLocationTransferTypes = $xml->LocationTransferTypeConstraint; + $i = 0; + foreach ($objLocationTransferTypes->LocationTransferType as $type) { + $locationTypes[$i]['location'] = strval($type->Location); + $locationTypes[$i]['type'] = strval($type->TransferTypes->Type); + $i++; + } + $this->setLocationTransferTypes($locationTypes); + } + } + + + /** + * Serialize the object to xml + * + * @return string + */ + public function serializeToXml() + { + + $xml = new \SimpleXMLElement(''); + if (isset($this->replicationLocation['location'])){ + foreach ($this->replicationLocation['location'] as $location){ + $xml->addChild('Location',$location); + } + } + if (isset($this->replicationLocation['LocationTransferTypeConstraint'])){ + $xmlConstraint = $xml->addChild('LocationTransferTypeConstraint'); + foreach ($this->replicationLocation['LocationTransferTypeConstraint'] as $locationTransferType){ + $xmlTransferType = $xmlConstraint->addChild('LocationTransferType'); + if (isset($locationTransferType['location'])){ + $xmlTransferType->addChild('Location',$locationTransferType['location']); + } + if (isset($locationTransferType['type'])){ + $xmlTypes = $xmlTransferType->addChild('TransferTypes'); + $xmlTypes->addChild('Type',$locationTransferType['type']); + } + + } + } + return $xml->asXML(); + } + + /** + * Serialize the object into xml string. + * + * @return string + */ + public function __toString() + { + return $this->serializeToXml(); + } + +} + + diff --git a/src/OSS/Model/ReplicationProgress.php b/src/OSS/Model/ReplicationProgress.php new file mode 100644 index 00000000..f3564c9c --- /dev/null +++ b/src/OSS/Model/ReplicationProgress.php @@ -0,0 +1,154 @@ +rule = $rule; + } + + /** + * @return ReplicationRule + */ + public function getRule(){ + return $this->rule; + } + /** + * Parse the xml into this object. + * + * @param string $strXml + * @throws OssException + * @return null + */ + + /** + * @param $strXml + * @return false||string|void|null + */ + public function parseFromXml($strXml) + { + $this->rule = array(); + $xml = simplexml_load_string($strXml); + if (!isset($xml->Rule)) return; + $this->parseRule($xml->Rule); + } + + /** + * @param $rules + */ + private function parseRule($rule) + { + $replicationRule = new ReplicationRule(); + + if (isset($rule->ID)){ + $replicationRule->setId(strval($rule->ID)); + } + if (isset($rule->PrefixSet)){ + foreach ($rule->PrefixSet->Prefix as $prefix){ + $replicationRule->setPrefixSet(strval($prefix)); + } + } + if (isset($rule->Action)){ + $replicationRule->setAction(strval($rule->Action)); + } + if (isset($rule->Destination)){ + $this->parseDestination($rule->Destination,$replicationRule); + } + if (isset($rule->TransferType)){ + $replicationRule->setSyncRole(strval($rule->TransferType)); + } + if (isset($rule->Status)){ + $replicationRule->setStatus(strval($rule->Status)); + } + if (isset($rule->HistoricalObjectReplication)){ + $replicationRule->setHistoricalObjectReplication(strval($rule->HistoricalObjectReplication)); + } + if (isset($rule->SyncRole)){ + $replicationRule->setSyncRole(strval($rule->SyncRole)); + } + if (isset($rule->Progress)){ + $this->parseProgress($rule->Progress,$replicationRule); + } + $this->addRule($replicationRule); + } + + /** + * @param $destination \SimpleXMLElement + * @param $replicationRule ReplicationRule + */ + private function parseDestination($destination,&$replicationRule) + { + if(isset($destination)){ + $replicationDestination = new ReplicationDestination(); + if (isset($destination->Bucket)){ + $replicationDestination->setBucket(strval($destination->Bucket)); + } + if (isset($destination->Location)){ + $replicationDestination->setLocation(strval($destination->Location)); + } + if (isset($destination->TransferType)){ + $replicationDestination->setTransferType(strval($destination->TransferType)); + } + $replicationRule->addDestination($replicationDestination); + } + + } + + /** + * @param $progress \SimpleXMLElement + * @param $replicationRule ReplicationRule + */ + private function parseProgress($progress,&$replicationRule) + { + if(isset($progress)){ + $replicationRuleProgress = new ReplicationRuleProgress(); + if (isset($progress->HistoricalObject)){ + $replicationRuleProgress->setHistoricalObject(strval($progress->HistoricalObject)); + } + if (isset($progress->NewObject)){ + $replicationRuleProgress->setNewObject(strval($progress->NewObject)); + } + $replicationRule->addProgress($replicationRuleProgress); + } + } + + /** + * Serialize the object to xml + * + * @return string + */ + public function serializeToXml() + { + + $xml = new \SimpleXMLElement(''); + $xmlRule = $xml->addChild('Rule'); + $this->rule->appendToXml($xmlRule); + return $xml->asXML(); + } + + /** + * Serialize the object into xml string. + * + * @return string + */ + public function __toString() + { + return $this->serializeToXml(); + } +} + + diff --git a/src/OSS/Model/ReplicationRule.php b/src/OSS/Model/ReplicationRule.php new file mode 100644 index 00000000..38eefe32 --- /dev/null +++ b/src/OSS/Model/ReplicationRule.php @@ -0,0 +1,263 @@ +id = $id; + } + + /** + * @return string|null + */ + public function getId(){ + return $this->id; + } + + /** + * @param string $status enabled|disabled + */ + public function setRTC($status){ + $this->rtc = $status; + } + + /** + * @return string enabled|disabled + */ + public function getRTC(){ + return $this->rtc; + } + + /** + * @param $prefix string + */ + public function setPrefixSet($prefix){ + $this->prefix[] = $prefix; + } + + /** + * @return array|null + */ + public function getPrefixSet(){ + return $this->prefix; + } + + + /** + * @param $action string ALL|PUT + */ + public function setAction($action){ + $this->action= $action; + } + + /** + * @return string|null + */ + public function getAction(){ + return $this->action; + } + + + /** + * @param $status string starting|doing|closing + */ + public function setStatus($status){ + $this->status= $status; + } + + + /** + * @return string |null + */ + public function getStatus(){ + return $this->status; + } + /** + * @param $destination ReplicationDestination + */ + public function addDestination($destination){ + $this->destination= $destination; + } + + /** + * @return ReplicationDestination + */ + public function getDestination(){ + return $this->destination; + } + + /** + * @param $transferType string + */ + public function setTransferType($transferType){ + $this->transferType= $transferType; + } + + /** + * @return string + */ + public function getTransferType(){ + return $this->transferType; + } + + /** + * @param $status string enabled | disabled + */ + public function setHistoricalObjectReplication($status){ + $this->historicalObjectReplication= $status; + } + + /** + * @return string enabled | disabled + */ + public function getHistoricalObjectReplication(){ + return $this->historicalObjectReplication; + } + + /** + * @param $syncRole string + */ + public function setSyncRole($syncRole){ + $this->syncRole= $syncRole; + } + + /** + * @return string |null + */ + public function getSyncRole(){ + return $this->syncRole; + } + + /** + * @param $sourceSelectionCriteria ReplicationSourceSelectionCriteria + */ + public function addSourceSelectionCriteria($sourceSelectionCriteria){ + $this->sourceSelectionCriteria= $sourceSelectionCriteria; + } + + /** + * @return ReplicationSourceSelectionCriteria + */ + public function getSourceSelectionCriteria(){ + return $this->sourceSelectionCriteria; + } + + /** + * @param $replicationEncryptionConfiguration ReplicationEncryptionConfiguration + */ + public function addEncryptionConfiguration($replicationEncryptionConfiguration){ + $this->encryptionConfiguration= $replicationEncryptionConfiguration; + } + + /** + * @return ReplicationEncryptionConfiguration + */ + public function getEncryptionConfiguration(){ + return $this->encryptionConfiguration; + } + + /** + * @param $replicationRuleProgress ReplicationRuleProgress + */ + public function addProgress($replicationRuleProgress){ + $this->progress= $replicationRuleProgress; + } + + /** + * @return ReplicationRuleProgress + */ + public function getProgress(){ + return $this->progress; + } + + /** + * @param \SimpleXMLElement $xmlRule + */ + public function appendToXml(&$xmlRule) + { + if (isset($this->id)){ + $xmlRule->addChild('ID', $this->id); + } + if (isset($this->prefix)){ + $xmlPrefixSet = $xmlRule->addChild('PrefixSet'); + foreach ($this->prefix as $prefix){ + $xmlPrefixSet->addChild('Prefix',$prefix); + } + } + if (isset($this->action)){ + $xmlRule->addChild('Action', $this->action); + } + if (isset($this->destination)){ + $this->destination->appendToXml($xmlRule); + } + if (isset($this->status)){ + $xmlRule->addChild('Status', $this->status); + } + if (isset($this->historicalObjectReplication)){ + $xmlRule->addChild('HistoricalObjectReplication', $this->historicalObjectReplication); + } + if (isset($this->sourceSelectionCriteria)){ + $status = $this->sourceSelectionCriteria->getStatus(); + if (isset($status)){ + $xmlSourceSelectionCriteria = $xmlRule->addChild('SourceSelectionCriteria'); + $xmlSseKmsEncryptedObjects = $xmlSourceSelectionCriteria->addChild('SseKmsEncryptedObjects'); + $xmlSseKmsEncryptedObjects->addChild('Status', $status); + } + } + if (isset($this->syncRole)){ + $xmlRule->addChild('SyncRole', $this->syncRole); + } + if (isset($this->encryptionConfiguration)){ + $kmsId = $this->encryptionConfiguration->getReplicaKmsKeyID(); + if (isset($kmsId)){ + $xmlEncryptionConfiguration = $xmlRule->addChild('EncryptionConfiguration'); + $xmlEncryptionConfiguration->addChild('ReplicaKmsKeyID', $kmsId); + } + } + if (isset($this->rtc)){ + $xmlRtc = $xmlRule->addChild('RTC'); + $xmlRtc->addChild('Status',$this->rtc); + } + if (isset($this->progress)){ + $this->progress->appendToXml($xmlRule); + } + } + +} \ No newline at end of file diff --git a/src/OSS/Model/ReplicationRuleProgress.php b/src/OSS/Model/ReplicationRuleProgress.php new file mode 100644 index 00000000..5b47cc07 --- /dev/null +++ b/src/OSS/Model/ReplicationRuleProgress.php @@ -0,0 +1,62 @@ +historicalObject = $historicalObject; + } + + /** + * @return string |null + */ + public function getHistoricalObject(){ + return $this->historicalObject; + } + + /** + * @param $newObject string + */ + public function setNewObject($newObject) + { + $this->newObject = $newObject; + } + + /** + * @return string |null + */ + public function getNewObject(){ + return $this->newObject; + } + /** + * @param \SimpleXMLElement $xmlRule + */ + public function appendToXml(&$xmlRule) + { + if (isset($this->historicalObject) || isset($this->newObject)){ + $xmlProgress= $xmlRule->addChild('Progress'); + } + if (isset($this->historicalObject)){ + $xmlProgress->addChild('HistoricalObject', $this->historicalObject); + } + if (isset($this->newObject)){ + $xmlProgress->addChild('NewObject', $this->newObject); + } + } +} + + diff --git a/src/OSS/Model/ReplicationSourceSelectionCriteria.php b/src/OSS/Model/ReplicationSourceSelectionCriteria.php new file mode 100644 index 00000000..b2aeda5d --- /dev/null +++ b/src/OSS/Model/ReplicationSourceSelectionCriteria.php @@ -0,0 +1,30 @@ +status = $status; + } + + /** + * @return string Enabled|Disabled + */ + public function getStatus(){ + return $this->status; + } +} + + diff --git a/src/OSS/OssClient.php b/src/OSS/OssClient.php index 0922a0b7..c4653d22 100644 --- a/src/OSS/OssClient.php +++ b/src/OSS/OssClient.php @@ -6,15 +6,22 @@ use OSS\Http\RequestCore; use OSS\Http\RequestCore_Exception; use OSS\Http\ResponseCore; +use OSS\Model\BucketReplicationLocation; +use OSS\Model\ReplicationProgress; 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\ReplicationLocation; 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; @@ -2579,6 +2586,146 @@ 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 Key-Value array + * @return null + * @throws OssException|RequestCore_Exception + */ + public function putBucketReplication($bucket,$replicationConfig,$options=null) + { + $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(); + } + + /** + * Turn the bucket replication on or off + * + * @param string $bucket bucket name + * @param string $ruleId the replication rule id + * @param string $status enabled|disabled + * @param null|array $options Key-Value array + * @return null + * @throws OssException + * @throws RequestCore_Exception + */ + public function putBucketRtc($bucket,$ruleId,$status,$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] = 'rtc'; + $options[self::OSS_CONTENT_TYPE] = 'application/xml'; + $options[self::OSS_CONTENT] = ''.$status.''.$ruleId.''; + $response = $this->auth($options); + $result = new HeaderResult($response); + return $result->getData(); + } + + /** + * Get the bucket Replication rule + * + * @param string $bucket bucket name + * @param array $options Key-Value array + * @return ReplicationConfig|null + * @throws OssException|RequestCore_Exception + */ + public function getBucketReplication($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] = 'replication'; + $response = $this->auth($options); + $result = new GetBucketReplicationResult($response); + return $result->getData(); + } + + + /** + * Get the bucket Replication location + * + * @param string $bucket bucket name + * @param array $options Key-Value array + * @return ReplicationLocation + * @throws OssException|RequestCore_Exception + */ + public function getBucketReplicationLocation($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] = 'replicationLocation'; + $response = $this->auth($options); + $result = new GetBucketReplicationLocationResult($response); + return $result->getData(); + } + + /** + * Get the bucket Replication Progress + * + * @param string $bucket bucket name + * @param string $ruleId rule id + * @param array $options Key-Value array + * @return ReplicationProgress|null + * @throws OssException + * @throws RequestCore_Exception + */ + public function getBucketReplicationProgress($bucket,$ruleId,$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] = '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 + * @param array $options Key-Value array + * @return null + * @throws OssException|RequestCore_Exception + */ + public function deleteBucketReplication($bucket,$replicationId,$options=null) + { + $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..73885906 --- /dev/null +++ b/src/OSS/Result/GetBucketReplicationLocationResult.php @@ -0,0 +1,32 @@ +rawResponse->body; + if (empty($content)) { + throw new OssException("body is null"); + } + $replicationLocation= new ReplicationLocation(); + $replicationLocation->parseFromXml($content); + return $replicationLocation; + + } +} \ 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..f5632875 --- /dev/null +++ b/src/OSS/Result/GetBucketReplicationProgressResult.php @@ -0,0 +1,33 @@ +rawResponse->body; + if (empty($content)) { + throw new OssException("body is null"); + } + + $replicationConfig = new ReplicationProgress(); + $replicationConfig->parseFromXml($content); + return $replicationConfig; + } +} \ 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..24c80d05 --- /dev/null +++ b/tests/OSS/Tests/GetBucketReplicationLocationResultTest.php @@ -0,0 +1,77 @@ + + + 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']); + $this->assertEquals("oss_acc", $info->getLocationTransferTypes()[0]['type']); + $this->assertEquals("oss_acc", $info->getLocationTransferTypes()[1]['type']); + } + + } + + 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..0732b418 --- /dev/null +++ b/tests/OSS/Tests/GetBucketReplicationProgressResultTest.php @@ -0,0 +1,88 @@ + + + + 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); + $rs = $result->getData(); + $info = $rs->getRule(); + + $this->assertEquals("test_replication_1", $info->getId()); + + + if ($info->getPrefixSet()) { + $this->assertEquals("source_image", $info->getPrefixSet()[0]); + $this->assertEquals("video", $info->getPrefixSet()[1]); + } + + $this->assertEquals("PUT", $info->getAction()); + $this->assertEquals("doing", $info->getStatus()); + $this->assertEquals("enabled", $info->getHistoricalObjectReplication()); + + $destination = $info->getDestination(); + $this->assertEquals("target-bucket", $destination->getBucket()); + $this->assertEquals("oss-cn-beijing", $destination->getLocation()); + $this->assertEquals("oss_acc", $destination->getTransferType()); + + + $progress = $info->getProgress(); + $this->assertEquals("0.85", $progress->getHistoricalObject()); + $this->assertEquals("2015-09-24T15:28:14.000Z", $progress->getNewObject()); + } + + public function testInvalidXml() + { + $response = new ResponseCore(array(), $this->invalidXml, 200); + $result = new GetBucketReplicationProgressResult($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/GetBucketReplicationResultTest.php b/tests/OSS/Tests/GetBucketReplicationResultTest.php new file mode 100644 index 00000000..2939a453 --- /dev/null +++ b/tests/OSS/Tests/GetBucketReplicationResultTest.php @@ -0,0 +1,109 @@ + + + + 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 + enabled + + +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->getRules()[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->getRules()[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()); + $this->assertEquals("enabled", $ruleOne->getRTC()); + } + + public function testInvalidXml() + { + $response = new ResponseCore(array(), $this->invalidXml, 200); + $result = new GetBucketReplicationResult($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/OssClientBucketReplicationTest.php b/tests/OSS/Tests/OssClientBucketReplicationTest.php new file mode 100644 index 00000000..e5f67c79 --- /dev/null +++ b/tests/OSS/Tests/OssClientBucketReplicationTest.php @@ -0,0 +1,121 @@ +descBucket = $this->bucket . 'desc'; + $this->ossClient->createBucket($this->descBucket); + } + public function testBucketReplication(){ + + + $replicationConfig = new ReplicationConfig(); + + $rule = new ReplicationRule(); + $rule->setPrefixSet('prefix_5'); + $rule->setPrefixSet('prefix_6'); + $rule->setAction(ReplicationRule::ACTION_PUT); + $rule->setHistoricalObjectReplication('enabled'); + $rule->setSyncRole('aliyunramrole'); + $destination = new ReplicationDestination(); + $destination->setBucket($this->descBucket); + $destination->setLocation('oss-cn-hangzhou'); + $destination->setTransferType('internal'); + $rule->addDestination($destination); + $criteria = new ReplicationSourceSelectionCriteria(); + $criteria->setStatus("Enabled"); + $rule->addSourceSelectionCriteria($criteria); + $encryptionConfiguration = new ReplicationEncryptionConfiguration(); + $encryptionConfiguration->setReplicaKmsKeyID("c4d49f85-ee30-426b-a5ed-95e9139d"); + $rule->addEncryptionConfiguration($encryptionConfiguration); + $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); + $rules = $replicationConfig2->getRules(); + $rule = $rules[0]; + $id = $rule->getId(); + } catch (OssException $e) { + $this->assertTrue(false); + } + + try { + $this->ossClient->putBucketRtc($this->bucket, $id,'disabled'); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + + try { + $result = $this->ossClient->getBucketReplicationLocation($this->bucket); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + + + try { + $result = $this->ossClient->getBucketReplicationProgress($this->bucket,$id); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + + + try { + Common::waitMetaSync(); + $result = $this->ossClient->deleteBucketReplication($this->bucket,$id); + } catch (OssException $e) { + $this->assertTrue(false); + } + + } + + protected function tearDown(): void + { + if (!$this->ossClient->doesBucketExist($this->descBucket)) { + return; + } + + $objects = $this->ossClient->listObjects( + $this->bucket . 'desc', array('max-keys' => 1000, 'delimiter' => ''))->getObjectList(); + $keys = array(); + foreach ($objects as $obj) { + $keys[] = $obj->getKey(); + } + if (count($keys) > 0) { + $this->ossClient->deleteObjects($this->descBucket, $keys); + } + $uploads = $this->ossClient->listMultipartUploads($this->descBucket)->getUploads(); + foreach ($uploads as $up) { + $this->ossClient->abortMultipartUpload($this->descBucket, $up->getKey(), $up->getUploadId()); + } + + $this->ossClient->deleteBucket($this->descBucket); + parent::tearDown(); + } + +} diff --git a/tests/OSS/Tests/ReplicationConfigTest.php b/tests/OSS/Tests/ReplicationConfigTest.php new file mode 100644 index 00000000..e23123c8 --- /dev/null +++ b/tests/OSS/Tests/ReplicationConfigTest.php @@ -0,0 +1,137 @@ + + + + +prefix_1 +prefix_2 + +ALL + +Target Bucket Name +oss-cn-hangzhou +oss_acc + +enabled + + +BBBB; + private $validXmlOne = << + + +159f80c1-a51e-4264-b832-******* + +prefix_5 +prefix_6 + +PUT + +test-bucket +oss-cn-hongkong +oss_acc + +doing +enabled + + +Disabled + + +aliyunramrole + + +8d66bf62-099f-48e9-9b03-******* + +prefix_5 +prefix_6 + +PUT + +test-bucket-1 +oss-cn-hangzhou + +doing +enabled + + +Enabled + + +aliyunramrole + +c4d49f85-ee30-426b-a5ed-95e9139d**** + + + +BBBB; + private $invalidXml = << + +BBBB; + + public function testValidXmlDemo() + { + $replicationConfig = new ReplicationConfig(); + $replicationConfig->parseFromXml($this->cleanXml($this->validXmlOne)); + $this->assertEquals($this->cleanXml($replicationConfig->serializeToXml()), $this->cleanXml($this->validXmlOne)); + } + public function testValidXml() + { + $replicationConfig = new ReplicationConfig(); + $replicationRule = new ReplicationRule(); + $replicationRule->setPrefixSet('prefix_1'); + $replicationRule->setPrefixSet('prefix_2'); + $replicationRule->setAction('ALL'); + $replicationOssBucketDestination = new ReplicationDestination(); + $replicationOssBucketDestination->setBucket('Target Bucket Name'); + $replicationOssBucketDestination->setLocation('oss-cn-hangzhou'); + $replicationOssBucketDestination->setTransferType('oss_acc'); + $replicationRule->addDestination($replicationOssBucketDestination); + $replicationRule->setHistoricalObjectReplication('enabled'); + $replicationConfig->addRule($replicationRule); + $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 testValidXmlOne() + { + $replicationConfig = new ReplicationConfig(); + + $replicationConfig->parseFromXml($this->cleanXml($this->validXmlOne)); + + $this->assertEquals($this->cleanXml($replicationConfig->serializeToXml()), $this->cleanXml($this->validXmlOne)); + } + + 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)); + } +} diff --git a/tests/OSS/Tests/ReplicationLocationTest.php b/tests/OSS/Tests/ReplicationLocationTest.php new file mode 100644 index 00000000..6bac8fb2 --- /dev/null +++ b/tests/OSS/Tests/ReplicationLocationTest.php @@ -0,0 +1,60 @@ + + +oss-cn-beijing +oss-cn-qingdao +oss-cn-shenzhen +oss-cn-hongkong +oss-us-west-1 + + +oss-cn-hongkong + +oss_acc + + + +oss-us-west-1 + +oss_acc + + + + +BBBB; + private $invalidXml = << + +BBBB; + + public function testValidXmlDemo() + { + $replicationLocation = new ReplicationLocation(); + $replicationLocation->parseFromXml($this->cleanXml($this->validXml)); + $this->assertEquals($this->cleanXml($replicationLocation->serializeToXml()), $this->cleanXml($this->validXml)); + } + + public function testInvalidXml() + { + $replicationLocation = new ReplicationLocation(); + $replicationLocation->parseFromXml($this->cleanXml($this->invalidXml)); + $this->assertEquals($this->cleanXml($this->invalidXml), $this->cleanXml($replicationLocation->serializeToXml())); + } + + private function cleanXml($xml) + { + return str_replace("\n", "", str_replace("\r", "", $xml)); + } +} diff --git a/tests/OSS/Tests/ReplicationProcessTest.php b/tests/OSS/Tests/ReplicationProcessTest.php new file mode 100644 index 00000000..b76aa8eb --- /dev/null +++ b/tests/OSS/Tests/ReplicationProcessTest.php @@ -0,0 +1,58 @@ + + + +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 testValidXmlDemo() + { + $replicationProgress = new ReplicationProgress(); + $replicationProgress->parseFromXml($this->cleanXml($this->validXml)); + $this->assertEquals($this->cleanXml($replicationProgress->serializeToXml()), $this->cleanXml($this->validXml)); + } + + public function testInvalidXml() + { + $replicationProgress = new ReplicationProgress(); + $replicationProgress->parseFromXml($this->cleanXml($this->invalidXml)); + $this->assertEquals($this->cleanXml($this->invalidXml), $this->cleanXml($replicationProgress->serializeToXml())); + } + + private function cleanXml($xml) + { + return str_replace("\n", "", str_replace("\r", "", $xml)); + } +}