Data replication automatically replicates objects and object operations, such as creation, overwriting, and deletion, from a source bucket to a destination bucket. Object Storage Service (OSS) supports cross-region replication (CRR) and same-region replication (SRR).
Notes
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.By default, an Alibaba Cloud account has permissions required for data replication. If you want to replicate data as a RAM user or using temporary access credentials provided by Security Token Service (STS), you must have the required permissions.
To enable data replication, you must have the
oss:PutBucketReplicationpermission.To enable or disable the replication time control (RTC) feature, you must have the
oss:PutBucketRtcpermission.To view data replication rules, you must have the
oss:GetBucketReplicationpermission.To view destination regions for replication, you must have the
oss:GetBucketReplicationLocationpermission.To view data replication progress, you must have the
oss:GetBucketReplicationProgresspermission.To disable data replication, you must have the
oss:DeleteBucketReplicationpermission.
Sample code
Enable data replication
Before you enable data replication, make sure that both the source bucket and the destination bucket are in the unversioned or versioning-enabled state.
The following sample code enables data replication and creates a data replication task that replicates data from a source bucket to a destination bucket in the same region or a different region:
<?php
// Include the autoload file to load the SDK and other dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
// Use the Alibaba Cloud OSS V2 namespace.
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line parameters, descriptions, and whether the parameters are required.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
"target-bucket" => ['help' => 'The name of the target bucket', 'required' => True],
"target-location" => ['help' => 'The location of the target bucket', 'required' => True],
];
// Create an array of long options required by getopt. Example: ["region:", "endpoint:", ...]
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Retrieve the command-line arguments passed.
$options = getopt("", $longopts);
// Check whether all required parameters are provided. If one is missing, an error message is displayed and the program exits.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
// Extract the command-line arguments.
$region = $options["region"];
$bucket = $options["bucket"];
$targetBucket = $options["target-bucket"];
$targetLocation = $options["target-location"];
// Use access credentials stored in environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Load the default configuration.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider, region, and optional endpoint.
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Construct a PutBucketReplicationRequest object.
$request = new Oss\Models\PutBucketReplicationRequest(
bucket: $bucket,
replicationConfiguration: new Oss\Models\ReplicationConfiguration(
rules: array(
new Oss\Models\ReplicationRule(
destination: new Oss\Models\ReplicationDestination(
bucket: $targetBucket,
location: $targetLocation,
),
rtc: new Oss\Models\ReplicationTimeControl(
status: 'enabled' // Enable replication time control (RTC).
)
)
)
)
);
// Execute the request and obtain the result.
$result = $client->putBucketReplication($request);
// Display the status code, request ID, and replication rule ID.
printf(
'status code:' . $result->statusCode . PHP_EOL .
'request id:' . $result->requestId . PHP_EOL .
'replication rule id:' . $result->replicationRuleId
);Query data replication rules
The following code queries the data replication rules for a bucket:
<?php
// Include the autoload file to load the SDK and other dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
// Use the Alibaba Cloud OSS V2 namespace and abbreviate it as Oss.
use AlibabaCloud\Oss\V2 as Oss;
// Define and describe command-line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
];
// Construct an array of long options required by getopt.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Retrieve the long options passed from the command line.
$options = getopt("", $longopts);
// Traverse all parameter definitions and check whether all required ones are provided.
foreach ($optsdesc as $key => $value) {
// If a required parameter is missing, display an error message and exit the program.
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
// Extract the region and bucket name from the command-line arguments.
$region = $options["region"];
$bucket = $options["bucket"];
// Load access credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Load the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider, region, and optional endpoint.
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Construct a GetBucketReplicationRequest object.
$request = new Oss\Models\GetBucketReplicationRequest(bucket: $bucket);
// Send the request and obtain the result.
$result = $client->getBucketReplication($request);
// Display the status code, request ID, and replication rules.
printf(
'status code:' . $result->statusCode . PHP_EOL .
'request id:' . $result->requestId . PHP_EOL .
'replication config:' . var_export($result->replicationConfiguration, true)
);Enable or disable the RTC feature
The following sample code enables or disables the RTC feature for a CRR rule:
<?php
// Include the autoload file to load the SDK and dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
// Use the Alibaba Cloud OSS V2 namespace and abbreviate it as Oss.
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line parameters, descriptions, and whether the parameters are required.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
"rule-id" => ['help' => 'The replication rule id of the bucket', 'required' => True],
];
// Construct an array of long options required by getopt. A key with a colon at the end expects a value.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Retrieve the long options passed from the command line.
$options = getopt("", $longopts);
// Check whether all required parameters are provided.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
// Extract the region, bucket, and replication rule ID.
$region = $options["region"];
$bucket = $options["bucket"];
$ruleId = $options["rule-id"];
// Load access credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Load the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider, region, and optional endpoint.
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Construct a PutBucketRtcRequest object.
$request = new Oss\Models\PutBucketRtcRequest(
bucket: $bucket,
rtcConfiguration: new Oss\Models\RtcConfiguration(
rtc: new Oss\Models\ReplicationTimeControl(
status: 'disabled' // Disable RTC.
),
id: $ruleId // The ID of the replication rule to be modified.
)
);
$result = $client->putBucketRtc($request);
// Display the status code and request ID.
printf(
'status code:' . $result->statusCode . PHP_EOL .
'request id:' . $result->requestId
);Query the regions to which data can be replicated
The following sample code queries the regions to which data can be replicated from the specified source bucket:
<?php
// Include the autoload file to load the SDK and other dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
// Use the Alibaba Cloud OSS V2 namespace and abbreviate it as Oss.
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line parameters, descriptions, and whether the parameters are required.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
];
// Construct an array of long options required by getopt.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Retrieve the long options passed from the command line.
$options = getopt("", $longopts);
// Traverse parameter definitions and check whether all required ones are provided.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
// Extract the region and bucket name from the command-line arguments.
$region = $options["region"];
$bucket = $options["bucket"];
// Load access credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Load the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider, region, and optional endpoint.
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Create a GetBucketReplicationLocationRequest object to query regions to which data can be replicated.
$request = new Oss\Models\GetBucketReplicationLocationRequest(bucket: $bucket);
// Send the request and obtain the result.
$result = $client->getBucketReplicationLocation($request);
// Display the status code, request ID, and available destination regions.
printf(
'status code:' . $result->statusCode . PHP_EOL .
'request id:' . $result->requestId . PHP_EOL .
'replication location:' . var_export($result->replicationLocation, true)
);Query the progress of a data replication task
You can query the progress of historical data replication and incremental data replication.
The progress of a historical data replication task is expressed as a percentage. You can query the progress of historical data replication tasks only for buckets for which historical data replication is enabled.
The progress of an incremental data replication task is expressed as a point in time. Data that is stored in the source bucket before the point in time has been replicated.
The following sample code queries the progress of data replication associated with a specific replication rule for a bucket:
<?php
// Include the autoload file to load the SDK and dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
// Use the Alibaba Cloud OSS V2 namespace and abbreviate it as Oss.
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line parameters, descriptions, and whether the parameters are required.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
"rule-id" => ['help' => 'The replication rule id of the bucket', 'required' => True],
];
// Construct an array of long options required by getopt. A key with a colon at the end expects a value.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Retrieve the long options passed from the command line.
$options = getopt("", $longopts);
// Traverse parameter definitions and check whether all required ones are provided.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
// Extract the region, bucket, and replication rule ID.
$region = $options["region"];
$bucket = $options["bucket"];
$ruleId = $options["rule-id"];
// Load access credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Load the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider, region, and optional endpoint.
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Construct a GetBucketReplicationProgressRequest object to query the replication progress for a specific replication rule.
$request = new Oss\Models\GetBucketReplicationProgressRequest(bucket: $bucket, ruleId: $ruleId);
// Send the request and obtain the result.
$result = $client->getBucketReplicationProgress($request);
// Display the status code, request ID, and replication progress details.
printf(
'status code:' . $result->statusCode . PHP_EOL .
'request id:' . $result->requestId . PHP_EOL .
'replication progress:' . var_export($result->replicationProgress, true)
);Disable data replication
You can delete a replication rule that is configured for a source bucket to disable data replication for the bucket.
The following sample code deletes the specified replication rule from the specified bucket:
<?php
// Include the autoload file to load the SDK and other dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
// Use the Alibaba Cloud OSS V2 namespace and abbreviate it as Oss.
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line parameters, descriptions, and whether the parameters are required.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
"rule-id" => ['help' => 'The replication rule id of the bucket', 'required' => True],
];
// Construct an array of long options required by getopt. A key with a colon at the end expects a value.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Retrieve the long options passed from the command line.
$options = getopt("", $longopts);
// Traverse parameter definitions and check whether all required ones are provided.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
// Extract the region, bucket, and replication rule ID.
$region = $options["region"];
$bucket = $options["bucket"];
$ruleId = $options["rule-id"];
// Load access credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Load the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider, region, and optional endpoint.
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Construct a DeleteBucketReplicationRequest object to delete replication rules.
$request = new Oss\Models\DeleteBucketReplicationRequest(
bucket: $bucket,
replicationRules: new Oss\Models\ReplicationRules(
ids: [$ruleId] // The list of IDs of replication rules to be deleted.
)
);
// Send the request to delete the specified replication rules.
$result = $client->deleteBucketReplication($request);
// Display the status code and request ID.
printf(
'status code:' . $result->statusCode . PHP_EOL .
'request id:' . $result->requestId
);