Enable CRR

The following code provides an example on how to enable CRR for a bucket:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

func HandleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}
// Enable CRR for a source bucket. 
func main()  {
    // Set yourEndpoint to the endpoint of the region in which the source bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }
    // Specify the name of the source bucket. 
    srcbucketName := "srcexamplebucket"

    // Specify that only data that is written to the source bucket after the rule is created and operations performed on objects in the bucket are synchronized to the destination bucket. Historical data in the source bucket is not synchronized to the destination bucket. 
    putXml := `<?xml version="1.0" encoding="UTF-8"?>
    <ReplicationConfiguration>
      <Rule>
        <PrefixSet>
            <! -- Specify that objects whose names contain one of the following prefixes are replicated to the destination bucket: prefix_1 and prefix_2. After you configure the Prefix parameter, only objects whose names contain the specified prefix are replicated to the destination bucket. -->
            <! -- To replicate all objects from the source bucket to the destination bucket, do not configure the Prefix parameter. 
            <Prefix>prefix_1</Prefix>
            <Prefix>prefix_2</Prefix>
        </PrefixSet>
        <! -- Specify the operations that can be synchronized to the destination bucket. The default value is ALL, which indicates that all operations performed on objects in the source bucket are replicated to the destination bucket. -->
        <Action>ALL</Action>
        <Destination>
            <! -- Specify the destination bucket to which the data is replicated. -->
            <Bucket>destexamplebucket</Bucket>
            <! -- Specify the region in which the destination bucket is located. -->
            <Location>oss-cn-beijing</Location>
            <! -- Specify the link that is used to transfer data during data replication. In this example, this parameter is set to oss_acc, which indicates that the link used for data transmission is accelerated. -->
            <TransferType>oss_acc</TransferType>
        </Destination>
        <! -- By default, historical data is synchronized. In this example, this parameter is set to disabled, which indicates that historical data is not synchronized. -->
        <HistoricalObjectReplication>disabled</HistoricalObjectReplication>
        <! -- Specify the name of the role that you authorize OSS to use to replicate data. If you want to use SSE-KMS to encrypt the objects that are replicated to the destination bucket, you must configure this parameter. -->
        <SyncRole>aliyunramrole</SyncRole>
        <SourceSelectionCriteria>
          <SseKmsEncryptedObjects>
            <! -- Specify whether to replicate objects that are encrypted by using SSE-KMS. -->
            <Status>Enabled</Status>
          </SseKmsEncryptedObjects>
        </SourceSelectionCriteria>
        <EncryptionConfiguration>
            <! -- Specify the CMK ID used in SSE-KMS. If Status is set to Enabled, you must configure this parameter. -->
           <ReplicaKmsKeyID>c4d49f85-ee30-426b-a5ed-95e9139d****</ReplicaKmsKeyID>
       </EncryptionConfiguration>
     </Rule>
   </ReplicationConfiguration>`

    err = client.PutBucketReplication(srcbucketName,putXml)
    if err != nil {
        HandleError(err)
    }
}            

Query the CRR rules configured for a bucket

The following code provides an example on how to query the CRR rules configured for a bucket:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

func HandleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}

// Query the CRR rules configured for the bucket. 
func main()  {
    // Set yourEndpoint to the endpoint of the region in which the source bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
        os.Exit(-1)
    }

    // Specify the name of the source bucket. 
    srcbucketName := "yourSourceBucket"
    result,err := client.GetBucketReplication(srcbucketName)
    if err != nil {
        HandleError(err)
        os.Exit(-1)
    }
    fmt.Println(result)
}
            

Query the progress of a CRR task that is performed on a bucket

Cross-region replication progress includes the progress of historical and real-time data synchronization.

  • Percent signs (%) indicate the progress of historical data synchronization. This function only works for buckets for which historical data synchronization has been enabled.
  • Time points (the point of latest time the data is written) indicate the progress of real-time data synchronization. If a time point is displayed, the data created before this time point has been synchronized.

The following code provides an example on how to query the progress of a CRR task that is performed on a source bucket:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

func HandleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}

// Query the progress of the CRR task that is performed on the source bucket. 
func main()  {
    // Set yourEndpoint to the endpoint of the region in which the source bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the source bucket. 
    srcbucketName := "yourSourceBucket"
    // Specify the CRR rule ID that is returned by the GetBucketReplication operation. 
    result,err := client.GetBucketReplicationProgress(srcbucketName,"e047ce28-6806-4131-b1da-30142116****")
    if err != nil {
        HandleError(err)
    }

    fmt.Println(result)
}

Query the regions in which CRR can be performed

The following code provides an example on how to query the regions in which CRR can be performed:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

func HandleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}

// Query the regions to which objects in a source bucket can be replicated. 
func main()  {
    // Set yourEndpoint to the endpoint of the region in which the source bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the source bucket. 
    srcbucketName := "yourSourceBucket"    
    result,err := client.GetBucketReplicationLocation(srcbucketName)
    if err != nil {
        HandleError(err)
    }

    fmt.Println(result)
}

            

Disable CRR for a bucket

The following code provides an example on how to disable CRR for a bucket:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

func HandleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}

// Disable CRR for a bucket. 
func main()  {
    // Set yourEndpoint to the endpoint of the region in which the source bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the source bucket. 
    srcbucketName := "yourSourceBucket"
    // Specify the CRR rule ID that is returned by the GetBucketReplication operation. 
    err = client.DeleteBucketReplication(srcbucketName,"e047ce28-6806-4131-b1da-30142116****")
    if err != nil {
        HandleError(err)
    }
}

References

  • For more information about the API operation that you can call to enable CRR, see PutBucketReplication.
  • For more information about the API operation that you can call to query the CRR rules configured for a bucket, see GetBucketReplication.
  • For more information about the API operation that you can call to query the regions in which CRR can be performed, see GetBucketReplicationLocation.
  • For more information about the API operation that you can call to query the progress of a CRR task that is performed on a bucket, see GetBucketReplicationProgress.
  • For more information about the API operation that you can call to disable CRR for a bucket, see DeleteBucketReplication.