Skip to content

Commit

Permalink
Replication related APIs, demos, and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzong18 committed Nov 24, 2021
1 parent 4ccead6 commit 7edd7ff
Show file tree
Hide file tree
Showing 16 changed files with 1,636 additions and 2 deletions.
323 changes: 323 additions & 0 deletions samples/BucketReplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,323 @@
<?php
require_once __DIR__ . '/Common.php';

use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Model\ReplicationConfig;
use OSS\Model\ReplicationOssBucketDestination;
use OSS\Model\ReplicationSourceSelectionCriteria;

$bucket = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);

//******************************* Simple Usage ***************************************************************

// Set Bucket Replication rule

$replicationConfig = new ReplicationConfig();
// Set the prefix of the object to be copied
$replicationConfig->addPrefixSet('prefix_3');
$replicationConfig->addPrefixSet('prefix_4');
// Specify the operations that can be copied to the target bucket (ALL or PUT)
$replicationConfig->addAction('ALL');
// Specify whether to copy historical data
$replicationConfig->addHistoricalObjectReplication('enabled');
// Which role is authorized for OSS to use for data replication
$replicationConfig->addSyncRole('aliyunramrole');
$replicationOssBucketDestination = new ReplicationOssBucketDestination();
// Specify the target bucket to which the data will be copied。
$replicationOssBucketDestination->addBucket('test-demo4');
// The region where the target bucket is located。
$replicationOssBucketDestination->addLocation('oss-cn-shanghai');
// Specify the data transmission link used when data is copied. internal (default) oss_acc: transmission acceleration link
$replicationOssBucketDestination->addTransferType('internal');
$replicationConfig->addDestination($replicationOssBucketDestination);

$replicationSourceSelectionCriteria = new ReplicationSourceSelectionCriteria();
// Specify whether OSS copies objects created through SSE-KMS encryption
$replicationSourceSelectionCriteria->addStatus("Enabled");
// Specify SSE-KMS key ID. If you specify Status as Enabled, you must specify this element
$replicationSourceSelectionCriteria->addReplicaKmsKeyID("c4d49f85-ee30-426b-a5ed-95e9139d");
$replicationConfig->addSourceSelectionCriteria($replicationSourceSelectionCriteria);
$ossClient->putBucketReplication($bucket,$replicationConfig);
Common::println("Bucket replication has created");


// Get bucket Replication rule
$replicationResult = $ossClient->getBucketReplication($bucket);
Common::println("===Replication Rule start ===");
foreach ($replicationResult as $info) {
Common::println("Replication Id:". $info->getId().PHP_EOL);
if ($info->getPrefixSet()['Prefix']){
foreach ($info->getPrefixSet()['Prefix'] as $prefix){
Common::println("Replication Prefix: ".$prefix.PHP_EOL);
}
}
Common::println("Replication Action:". $info->getAction().PHP_EOL);
Common::println("Replication Target Bucket: ". $info->getDestination()['Bucket'].PHP_EOL);
Common::println("Replication Target Bucket Location: ". $info->getDestination()['Location'].PHP_EOL);
if(isset($info->getDestination()['TransferType'])){
Common::println("Replication Target Bucket TransferType: ". $info->getDestination()['TransferType'].PHP_EOL);
}

Common::println("Replication HistoricalObjectReplication:". $info->getHistoricalObjectReplication().PHP_EOL);
Common::println("Replication SyncRole: ". $info->getSyncRole().PHP_EOL);
Common::println("Replication Status: ". $info->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


$replicationProcessResult = $ossClient->getBucketReplicationProgress($bucket,'test-replication-id');

if($replicationProcessResult){
Common::println("=====================Bucket replication process start=================================".PHP_EOL);
Common::println("Bucket replication process id is ".$replicationProcessResult->getId().PHP_EOL);
if($replicationProcessResult->getPrefixSet()){
foreach ($replicationProcessResult->getPrefixSet()['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()){
Common::println("Bucket replication process bucket name is: ".$replicationProcessResult->getDestination()['Bucket'].PHP_EOL);
Common::println("Bucket replication process bucket location is: ".$replicationProcessResult->getDestination()['Location'].PHP_EOL);
Common::println("Bucket replication process Prefix transfer type is: ".$replicationProcessResult->getDestination()['TransferType'].PHP_EOL);
}
Common::println("Bucket replication process status is ".$replicationProcessResult->getStatus().PHP_EOL);
Common::println("Bucket replication process historicalObjectReplication is: ".$replicationProcessResult->getHistoricalObjectReplication().PHP_EOL);

if($replicationProcessResult->getProgress()){
Common::println("Bucket replication process HistoricalObject is: ".$replicationProcessResult->getProgress()['HistoricalObject'].PHP_EOL);
Common::println("Bucket replication process NewObject is: ".$replicationProcessResult->getProgress()['NewObject'].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);
getBucketReplication($ossClient, $bucket);
deleteReplication($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();
// Set the prefix of the object to be copied
$replicationConfig->addPrefixSet('prefix_3');
$replicationConfig->addPrefixSet('prefix_4');
// Specify the operations that can be copied to the target bucket (ALL or PUT)
$replicationConfig->addAction('ALL');
// Specify whether to copy historical data
$replicationConfig->addHistoricalObjectReplication('enabled');
// Which role is authorized for OSS to use for data replication
$replicationConfig->addSyncRole('aliyunramrole');
$replicationOssBucketDestination = new ReplicationOssBucketDestination();
// Specify the target bucket to which the data will be copied。
$replicationOssBucketDestination->addBucket('test-demo4');
// The region where the target bucket is located。
$replicationOssBucketDestination->addLocation('oss-cn-shanghai');
// Specify the data transmission link used when data is copied. internal (default) oss_acc: transmission acceleration link
$replicationOssBucketDestination->addTransferType('internal');
$replicationConfig->addDestination($replicationOssBucketDestination);

$replicationSourceSelectionCriteria = new ReplicationSourceSelectionCriteria();
// Specify whether OSS copies objects created through SSE-KMS encryption
$replicationSourceSelectionCriteria->addStatus("Enabled");
// Specify SSE-KMS key ID. If you specify Status as Enabled, you must specify this element
$replicationSourceSelectionCriteria->addReplicaKmsKeyID("c4d49f85-ee30-426b-a5ed-95e9139d");
$replicationConfig->addSourceSelectionCriteria($replicationSourceSelectionCriteria);
$ossClient->putBucketReplication($bucket,$replicationConfig);
}catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");

printf("Bucket replication has created");
}


/**
* 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);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
printf("===Replication Rule start ===");
foreach ($replicationResult as $info) {
printf("Replication Id:". $info->getId().PHP_EOL);
if ($info->getPrefixSet()['Prefix']){
foreach ($info->getPrefixSet()['Prefix'] as $prefix){
printf("Replication Prefix: ".$prefix.PHP_EOL);
}
}
printf("Replication Action:". $info->getAction().PHP_EOL);
printf("Replication Target Bucket: ". $info->getDestination()['Bucket'].PHP_EOL);
printf("Replication Target Bucket Location: ". $info->getDestination()['Location'].PHP_EOL);
if(isset($info->getDestination()['TransferType'])){
printf("Replication Target Bucket TransferType: ". $info->getDestination()['TransferType'].PHP_EOL);
}

printf("Replication HistoricalObjectReplication:". $info->getHistoricalObjectReplication().PHP_EOL);
printf("Replication SyncRole: ". $info->getSyncRole());
if($info->getSourceSelectionCriteria()){
printf("Replication SourceSelectionCriteria Status: ". $info->getSourceSelectionCriteria()['SseKmsEncryptedObjects']['Status'].PHP_EOL);
if($info->getSourceSelectionCriteria()['SseKmsEncryptedObjects']['Status'] == "Enabled"){
printf("Replication EncryptionConfiguration ReplicaKmsKeyID: ".$info->getEncryptionConfiguration().PHP_EOL);
}
}
}
printf("===Replication Rule End ===");
}


/**
* 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);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
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);
}


/**
* Get bucket replication location
*
* @param OssClient $ossClient OssClient instance
* @param string $bucket bucket name
* @return null
*/
function getBucketReplicationProgress($ossClient, $bucket)
{
try {
$replicationProcessResult = $ossClient->getBucketReplicationProgress($bucket,"test-replication-id");
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
printf("=====================Bucket replication progress start=================================".PHP_EOL);
printf("Bucket replication process id is ".$replicationProcessResult->getId().PHP_EOL);
if($replicationProcessResult->getPrefixSet()){
foreach ($replicationProcessResult->getPrefixSet()['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()){
printf("Bucket replication process bucket name is: ".$replicationProcessResult->getDestination()['Bucket'].PHP_EOL);
printf("Bucket replication process bucket location is: ".$replicationProcessResult->getDestination()['Location'].PHP_EOL);
printf("Bucket replication process Prefix transfer type is: ".$replicationProcessResult->getDestination()['TransferType'].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()){
printf("Bucket replication process HistoricalObject is: ".$replicationProcessResult->getProgress()['HistoricalObject'].PHP_EOL);
printf("Bucket replication process NewObject is: ".$replicationProcessResult->getProgress()['NewObject'].PHP_EOL);
}
printf("========================Bucket replication progress end ============================".PHP_EOL);
}

/**
* 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);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
printf("$bucket replication rule has deleted");
}

Loading

0 comments on commit 7edd7ff

Please sign in to comment.