github.com/aliyun/aliyun-oss-go-sdk@v3.0.2+incompatible/sample/bucket_replication.go (about)

     1  package sample
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  	"github.com/aliyun/aliyun-oss-go-sdk/oss"
     7  )
     8  
     9  // BucketReplicationSample  how to set, get or delete the bucket replication.
    10  func BucketReplicationSample() {
    11  	// New client
    12  	client, err := oss.New(endpoint, accessID, accessKey)
    13  	if err != nil {
    14  		HandleError(err)
    15  	}
    16  
    17  	// Create a bucket with default parameters
    18  	err = client.CreateBucket(bucketName)
    19  	if err != nil {
    20  		HandleError(err)
    21  	}
    22  
    23  	// Case 1:Put Bucket Replication
    24  	// Case 1-1:Put Bucket Replication in xml format
    25  	xmlData := `<?xml version="1.0" encoding="UTF-8"?>
    26  <ReplicationConfiguration>
    27   <Rule>
    28      <PrefixSet>
    29         <Prefix>source1</Prefix>
    30         <Prefix>video</Prefix>
    31      </PrefixSet>
    32      <Action>PUT</Action>
    33      <Destination>
    34         <Bucket>destBucketName</Bucket>
    35         <Location>oss-cn-hangzhou</Location>
    36         <TransferType>oss_acc</TransferType>
    37      </Destination>
    38      <HistoricalObjectReplication>enabled</HistoricalObjectReplication>
    39       <SyncRole>aliyunramrole</SyncRole>
    40       <SourceSelectionCriteria>
    41          <SseKmsEncryptedObjects>
    42            <Status>Enabled</Status>
    43          </SseKmsEncryptedObjects>
    44       </SourceSelectionCriteria>
    45       <EncryptionConfiguration>
    46            <ReplicaKmsKeyID>c4d49f85-ee30-426b-a5ed-95e9139d****</ReplicaKmsKeyID>
    47       </EncryptionConfiguration>
    48   </Rule>
    49  </ReplicationConfiguration>`
    50  
    51  	err = client.PutBucketReplication(bucketName, xmlData)
    52  	if err != nil {
    53  		HandleError(err)
    54  	}
    55  	fmt.Println("Put Bucket Replication in xml format Success!")
    56  	// Case 1-2:Put Bucket Replication in Struct
    57  	destBucketName := "yp-re"
    58  	prefix1 := "prefix_1"
    59  	prefix2 := "prefix_2"
    60  	keyId := "c4d49f85-ee30-426b-a5ed-95e9139d******"
    61  	source := "Enabled"
    62  	prefixSet := oss.ReplicationRulePrefix{Prefix: []*string{&prefix1, &prefix2}}
    63  	reqReplication := oss.PutBucketReplication{
    64  		Rule: []oss.ReplicationRule{
    65  			{
    66  				PrefixSet: &prefixSet,
    67  				Action:    "ALL",
    68  				Destination: &oss.ReplicationRuleDestination{
    69  					Bucket:       destBucketName,
    70  					Location:     "oss-cn-hangzhou",
    71  					TransferType: "oss_acc",
    72  				},
    73  				HistoricalObjectReplication: "disabled",
    74  				SyncRole:                    "aliyunramrole",
    75  				EncryptionConfiguration:     &keyId,
    76  				SourceSelectionCriteria:     &source,
    77  			},
    78  		},
    79  	}
    80  	xmlBody, err := xml.Marshal(reqReplication)
    81  	if err != nil {
    82  		HandleError(err)
    83  	}
    84  	err = client.PutBucketReplication(bucketName, string(xmlBody))
    85  
    86  	if err != nil {
    87  		HandleError(err)
    88  	}
    89  	fmt.Println("Put Bucket Replication Success!")
    90  
    91  	// Case 2:Get Bucket Replication
    92  	stringData, err := client.GetBucketReplication(bucketName)
    93  	if err != nil {
    94  		HandleError(err)
    95  	}
    96  
    97  	var repResult oss.GetBucketReplicationResult
    98  	err = xml.Unmarshal([]byte(stringData),&repResult)
    99  	if err != nil {
   100  		HandleError(err)
   101  	}
   102  	for _, rule := range repResult.Rule {
   103  		fmt.Printf("Rule Id:%s\n", rule.ID)
   104  		if rule.RTC != nil {
   105  			fmt.Printf("Rule RTC:%s\n", *rule.RTC)
   106  		}
   107  		if rule.PrefixSet != nil {
   108  			for _, prefix := range rule.PrefixSet.Prefix {
   109  				fmt.Printf("Rule Prefix:%s\n", *prefix)
   110  			}
   111  		}
   112  		fmt.Printf("Rule Action:%s\n", rule.Action)
   113  		fmt.Printf("Rule Destination Bucket:%s\n", rule.Destination.Bucket)
   114  		fmt.Printf("Rule Destination Location:%s\n", rule.Destination.Location)
   115  		fmt.Printf("Rule Destination TransferType:%s\n", rule.Destination.TransferType)
   116  		fmt.Printf("Rule Status:%s\n", rule.Status)
   117  		fmt.Printf("Rule Historical Object Replication:%s\n", rule.HistoricalObjectReplication)
   118  		if rule.SyncRole != "" {
   119  			fmt.Printf("Rule SyncRole:%s\n", rule.SyncRole)
   120  		}
   121  	}
   122  
   123  	// Case 3:Put Bucket RTC
   124  	enabled := "enabled"
   125  	ruleId := "564df6de-7372-46dc-b4eb-10f******"
   126  	rtc := oss.PutBucketRTC{
   127  		RTC: &enabled,
   128  		ID:  ruleId,
   129  	}
   130  	err = client.PutBucketRTC(bucketName, rtc)
   131  	if err != nil {
   132  		HandleError(err)
   133  	}
   134  
   135  	fmt.Println("Put Bucket RTC Success!")
   136  	// Case 4:Get Bucket Replication Location
   137  	stringData, err = client.GetBucketReplicationLocation(bucketName)
   138  	if err != nil {
   139  		HandleError(err)
   140  	}
   141  
   142  	var repLocation oss.GetBucketReplicationLocationResult
   143  	err = xml.Unmarshal([]byte(stringData),&repLocation)
   144  	if err != nil {
   145  		HandleError(err)
   146  	}
   147  
   148  	for _, location := range repLocation.Location {
   149  		fmt.Printf("Bucket Replication Location: %s\n", location)
   150  	}
   151  
   152  	for _, transferType := range repLocation.LocationTransferType {
   153  		fmt.Printf("Bucket Replication Location Transfer Type Location: %s\n", transferType.Location)
   154  		fmt.Printf("Bucket Replication Location Transfer Type Type: %s\n", transferType.TransferTypes)
   155  	}
   156  
   157  	for _, rtcLocation := range repLocation.RTCLocation {
   158  		fmt.Printf("Bucket Replication Location RTC Location: %s\n", rtcLocation)
   159  	}
   160  	fmt.Println("Get Bucket Replication Location Success!")
   161  	// Case 5:Get Bucket Replication Progress
   162  	stringData, err = client.GetBucketReplicationProgress(bucketName, ruleId)
   163  	if err != nil {
   164  		HandleError(err)
   165  	}
   166  	var repProgress oss.GetBucketReplicationProgressResult
   167  	err = xml.Unmarshal([]byte(stringData),&repProgress)
   168  	if err != nil {
   169  		HandleError(err)
   170  	}
   171  	for _, repProgressRule := range repProgress.Rule {
   172  		fmt.Printf("Rule Id:%s\n", repProgressRule.ID)
   173  		if repProgressRule.PrefixSet != nil {
   174  			for _, prefix := range repProgressRule.PrefixSet.Prefix {
   175  				fmt.Printf("Rule Prefix:%s\n", *prefix)
   176  			}
   177  		}
   178  		fmt.Printf("Replication Progress Rule Action:%s\n", repProgressRule.Action)
   179  		fmt.Printf("Replication Progress Rule Destination Bucket:%s\n", repProgressRule.Destination.Bucket)
   180  		fmt.Printf("Replication Progress Rule Destination Location:%s\n", repProgressRule.Destination.Location)
   181  		fmt.Printf("Replication Progress Rule Destination TransferType:%s\n", repProgressRule.Destination.TransferType)
   182  		fmt.Printf("Replication Progress Rule Status:%s\n", repProgressRule.Status)
   183  		fmt.Printf("Replication Progress Rule Historical Object Replication:%s\n", repProgressRule.HistoricalObjectReplication)
   184  		if (*repProgressRule.Progress).HistoricalObject != "" {
   185  			fmt.Printf("Replication Progress Rule Progress Historical Object:%s\n", (*repProgressRule.Progress).HistoricalObject)
   186  		}
   187  		fmt.Printf("Replication Progress Rule Progress NewObject:%s\n", (*repProgressRule.Progress).NewObject)
   188  	}
   189  	fmt.Println("Get Bucket Replication Progress Success!")
   190  	// Case 6:Delete Bucket Replication
   191  	err = client.DeleteBucketReplication(bucketName, ruleId)
   192  	if err != nil {
   193  		HandleError(err)
   194  	}
   195  	fmt.Println("Delete Bucket Replication Success!")
   196  
   197  	fmt.Println("BucketReplicationSample completed")
   198  
   199  }