Data replication asynchronously and automatically replicates objects and their operations, such as creation, overwriting, and deletion, from a source bucket to a destination bucket in near real time. Object Storage Service (OSS) supports cross-region replication (CRR) and same-region replication (SRR).
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
By default, an Alibaba Cloud account has permissions for data replication. If you want to perform data replication operations as a Resource Access Management (RAM) user or using temporary access credentials provided by STS, you must have the required permissions.
To enable data replication, you must have the
oss:PutBucketReplicationpermission.To enable or disable replication time control (RTC), you must have the
oss:PutBucketRtcpermission.To query data replication rules, you must have the
oss:GetBucketReplicationpermission.To query available destination regions, you must have the
oss:GetBucketReplicationLocationpermission.To view the data replication progress, you must have the
oss:GetBucketReplicationProgresspermission.To disable data replication, you must have the
oss:DeleteBucketReplicationpermission.
Examples
Enable data replication
Before you enable data replication, make sure that versioning is either disabled for both the source and destination buckets, or enabled for both buckets.
The following code shows how to enable data replication. In this example, data is replicated from the srcexamplebucket bucket in the China (Hangzhou) region to the destexamplebucket bucket, which can be in the same or a different region.
package main
import (
"encoding/xml"
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the name of the source bucket.
srcbucketName := "srcexamplebucket"
// Specify the destination bucket to which you want to replicate data.
destBucketName := "destexamplebucket"
// Specify the prefixes of the objects that you want to replicate, such as prefix_1 and prefix_2. After you specify prefixes, only objects that match the prefixes are replicated to the destination bucket.
// If you want to replicate all objects from the source bucket to the destination bucket, do not set Prefix.
prefix1 := "prefix_1"
prefix2 := "prefix_2"
// Specify the ID of the server-side encryption (SSE) with Key Management Service (KMS) key. If you set Status to Enabled, you must specify this element.
keyId := "c4d49f85-ee30-426b-a5ed-95e9139d"
// Specify whether to replicate objects that are encrypted using SSE-KMS.
source := "Enabled"
prefixSet := oss.ReplicationRulePrefix{Prefix: []*string{&prefix1, &prefix2}}
// Enable replication time control (RTC) when you configure a cross-region replication rule.
enabled := "enabled"
reqReplication := oss.PutBucketReplication{
Rule: []oss.ReplicationRule{
{
PrefixSet: &prefixSet,
// Replicate object creation and update operations from the source bucket to the destination bucket.
Action: "PUT",
RTC: &enabled,
Destination: &oss.ReplicationRuleDestination{
Bucket: destBucketName,
Location: "oss-cn-hangzhou",
TransferType: "oss_acc",
},
HistoricalObjectReplication: "disabled",
SyncRole: "aliyunramrole",
EncryptionConfiguration: &keyId,
SourceSelectionCriteria: &source,
},
},
}
xmlBody, err := xml.Marshal(reqReplication)
if err != nil {
log.Fatalf("Failed to marshal XML for PutBucketReplication: %v", err)
}
err = client.PutBucketReplication(srcbucketName, string(xmlBody))
if err != nil {
log.Fatalf("Failed to put bucket replication: %v", err)
}
log.Println("Put Bucket Replication Success!")
}
Query data replication rules
The following code shows how to query the data replication rules of the examplebucket bucket.
package main
import (
"encoding/xml"
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the name of the source bucket.
bucketName := "srcexamplebucket"
// Get the data replication configuration.
stringData, err := client.GetBucketReplication(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket replication: %v", err)
}
// Parse the XML response.
var repResult oss.GetBucketReplicationResult
err = xml.Unmarshal([]byte(stringData), &repResult)
if err != nil {
log.Fatalf("Failed to unmarshal XML response: %v", err)
}
// Print the data replication rules.
for _, rule := range repResult.Rule {
log.Printf("Rule Id: %s", rule.ID)
if rule.RTC != nil {
log.Printf("Rule RTC: %s", *rule.RTC)
}
if rule.PrefixSet != nil {
for _, prefix := range rule.PrefixSet.Prefix {
log.Printf("Rule Prefix: %s", *prefix)
}
}
log.Printf("Rule Action: %s", rule.Action)
log.Printf("Rule Destination Bucket: %s", rule.Destination.Bucket)
log.Printf("Rule Destination Location: %s", rule.Destination.Location)
log.Printf("Rule Destination TransferType: %s", rule.Destination.TransferType)
log.Printf("Rule Status: %s", rule.Status)
log.Printf("Rule Historical Object Replication: %s", rule.HistoricalObjectReplication)
if rule.SyncRole != "" {
log.Printf("Rule SyncRole: %s", rule.SyncRole)
}
}
}
Set replication time control (RTC)
The following code shows how to enable or disable replication time control (RTC) for an existing cross-region replication rule.
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the name of the source bucket.
bucketName := "srcexamplebucket"
// Enable RTC.
enabled := "enabled"
// Specify the ID of the cross-region replication rule.
id := "564df6de-7372-46dc-b4eb-10f****"
// Build a PutBucketRTC request.
rtc := oss.PutBucketRTC{
RTC: &enabled,
ID: id,
}
err = client.PutBucketRTC(bucketName, rtc)
if err != nil {
log.Fatalf("Failed to put bucket RTC: %v", err)
}
log.Println("Put Bucket RTC Success!")
}
Query available destination regions
The following code shows how to query the list of destination regions to which data can be replicated from the examplebucket bucket.
package main
import (
"encoding/xml"
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the name of the source bucket.
bucketName := "srcexamplebucket"
// Query the destination regions to which data in the source bucket can be replicated.
stringData, err := client.GetBucketReplicationLocation(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket replication location: %v", err)
}
// Parse the XML response.
var repLocation oss.GetBucketReplicationLocationResult
err = xml.Unmarshal([]byte(stringData), &repLocation)
if err != nil {
log.Fatalf("Failed to unmarshal XML response: %v", err)
}
// Print the destination region information.
for _, location := range repLocation.Location {
log.Printf("Bucket Replication Location: %s", location)
}
// Print the transfer type information.
for _, transferType := range repLocation.LocationTransferType {
log.Printf("Bucket Replication Location Transfer Type Location: %s", transferType.Location)
log.Printf("Bucket Replication Location Transfer Type Type: %s", transferType.TransferTypes)
}
// Print the RTC location information.
for _, rtcLocation := range repLocation.RTCLocation {
log.Printf("Bucket Replication Location RTC Location: %s", rtcLocation)
}
log.Println("Get Bucket Replication Location Success!")
}
Query the progress of a data replication task
You can query the progress of historical data replication tasks and incremental data replication tasks.
The progress of historical data replication tasks 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 incremental data replication tasks is expressed as a point in time. Data that is stored in the source bucket before the point in time is replicated.
The following code shows how to query the progress of a data replication task for a specific rule ID in the examplebucket bucket.
package main
import (
"encoding/xml"
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the name of the source bucket.
bucketName := "srcexamplebucket"
ruleId := "564df6de-7372-46dc-b4eb-10f****"
// Query the data replication progress.
stringData, err := client.GetBucketReplicationProgress(bucketName, ruleId)
if err != nil {
log.Fatalf("Failed to get bucket replication progress: %v", err)
}
// Parse the XML response.
var repProgress oss.GetBucketReplicationProgressResult
err = xml.Unmarshal([]byte(stringData), &repProgress)
if err != nil {
log.Fatalf("Failed to unmarshal XML response: %v", err)
}
// Print the data replication progress information.
for _, repProgressRule := range repProgress.Rule {
log.Printf("Rule Id: %s", repProgressRule.ID)
if repProgressRule.PrefixSet != nil {
for _, prefix := range repProgressRule.PrefixSet.Prefix {
log.Printf("Rule Prefix: %s", *prefix)
}
}
log.Printf("Replication Progress Rule Action: %s", repProgressRule.Action)
log.Printf("Replication Progress Rule Destination Bucket: %s", repProgressRule.Destination.Bucket)
log.Printf("Replication Progress Rule Destination Location: %s", repProgressRule.Destination.Location)
log.Printf("Replication Progress Rule Destination TransferType: %s", repProgressRule.Destination.TransferType)
log.Printf("Replication Progress Rule Status: %s", repProgressRule.Status)
log.Printf("Replication Progress Rule Historical Object Replication: %s", repProgressRule.HistoricalObjectReplication)
if repProgressRule.Progress != nil && repProgressRule.Progress.HistoricalObject != "" {
log.Printf("Replication Progress Rule Progress Historical Object: %s", repProgressRule.Progress.HistoricalObject)
}
log.Printf("Replication Progress Rule Progress NewObject: %s", repProgressRule.Progress.NewObject)
}
log.Println("Get Bucket Replication Progress Success!")
}
Disable data replication
You can disable the replication relationship between the source and destination buckets by deleting the replication rule of the source bucket.
The following code shows how to delete the replication rule for a specific rule ID in the examplebucket bucket.
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the name of the source bucket.
srcbucketName := "yourSourceBucket"
// Specify the ID of the data replication rule that is returned by the GetBucketReplication operation.
ruleID := "e047ce28-6806-4131-b1da-30142116****"
// Disable data replication.
err = client.DeleteBucketReplication(srcbucketName, ruleID)
if err != nil {
log.Fatalf("Failed to delete bucket replication: %v", err)
}
log.Println("Delete Bucket Replication Success!")
}
References
For more information about the API operation for enabling data replication, see PutBucketReplication.
For more information about the API operation for enabling or disabling replication time control (RTC) for an existing cross-region replication rule, see PutBucketRTC.
For more information about the API operation for querying data replication rules, see GetBucketReplication.
For more information about the API operation for querying available destination regions, see GetBucketReplicationLocation.
For more information about the API operation for querying data replication progress, see GetBucketReplicationProgress.
For more information about the API operation for disabling data replication, see DeleteBucketReplication.