Data replication (Go SDK V1)
Data replication automatically and asynchronously copies objects and their operations—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).
When to use CRR, SRR, and RTC
| Requirement | CRR | SRR | RTC |
|---|---|---|---|
| Replicate across different regions | Yes | No | — |
| Replicate within the same region | No | Yes | — |
| Guarantee replication within a predictable time window | No | No | Yes |
| Replicate historical objects created before the rule | Yes (optional) | Yes (optional) | — |
| Replicate SSE-KMS encrypted objects | Yes | Yes | — |
Replication time control (RTC) applies to CRR rules only. Enable it when your workload requires a predictable replication time SLA.
Prerequisites
Before you begin, make sure that you have:
The Go SDK installed:
github.com/aliyun/aliyun-oss-go-sdk/ossAccess credentials stored in the
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables. For setup instructions, see Configure access credentialsThe required permissions for the operations you intend to perform
By default, an Alibaba Cloud account has permissions for all data replication operations. If you use a Resource Access Management (RAM) user or temporary access credentials from Security Token Service (STS), grant the permissions listed below.
| Operation | Required permission |
|---|---|
| Enable data replication | oss:PutBucketReplication |
| Enable or disable RTC | oss:PutBucketRtc |
| Query replication rules | oss:GetBucketReplication |
| Query available destination regions | oss:GetBucketReplicationLocation |
| Query replication progress | oss:GetBucketReplicationProgress |
| Disable data replication | oss:DeleteBucketReplication |
Usage notes
The examples in this topic use the public endpoint of the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For region and endpoint details, see Regions and endpoints.
The examples create an OSSClient instance using an OSS endpoint. To create an OSSClient instance with a custom domain name or STS, see Configure OSSClient instances.
Query replication progress
OSS tracks two types of replication progress:
Historical object replication: expressed as a percentage. Available only when
HistoricalObjectReplicationis set toenabledin the replication rule.Incremental data replication: expressed as a point in time. Objects stored in the source bucket before this time have been replicated.
GetBucketReplicationProgress returns both values for a specific rule. Use the rule ID from GetBucketReplication.
package main
import (
"encoding/xml"
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
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)
}
bucketName := "srcexamplebucket"
ruleId := "564df6de-7372-46dc-b4eb-10f****"
stringData, err := client.GetBucketReplicationProgress(bucketName, ruleId)
if err != nil {
log.Fatalf("Failed to get bucket replication progress: %v", err)
}
var repProgress oss.GetBucketReplicationProgressResult
err = xml.Unmarshal([]byte(stringData), &repProgress)
if err != nil {
log.Fatalf("Failed to unmarshal XML response: %v", err)
}
for _, rule := range repProgress.Rule {
log.Printf("Rule ID: %s", rule.ID)
if rule.PrefixSet != nil {
for _, prefix := range rule.PrefixSet.Prefix {
log.Printf("Prefix: %s", *prefix)
}
}
log.Printf("Action: %s", rule.Action)
log.Printf("Destination bucket: %s", rule.Destination.Bucket)
log.Printf("Destination location: %s", rule.Destination.Location)
log.Printf("Transfer type: %s", rule.Destination.TransferType)
log.Printf("Status: %s", rule.Status)
log.Printf("Historical object replication: %s", rule.HistoricalObjectReplication)
if rule.Progress != nil && rule.Progress.HistoricalObject != "" {
// Percentage of historical objects replicated.
log.Printf("Historical object replication progress: %s", rule.Progress.HistoricalObject)
}
// Timestamp up to which incremental objects have been replicated.
log.Printf("Incremental replication progress: %s", rule.Progress.NewObject)
}
log.Println("Get Bucket Replication Progress Success!")
}