Skip to content

Commit f1516b1

Browse files
modify bucket replication api (#366)
Co-authored-by: huiguangjun <40553369+huiguangjun@users.noreply.github.com>
1 parent 4543270 commit f1516b1

File tree

7 files changed

+685
-7
lines changed

7 files changed

+685
-7
lines changed

oss/client.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,43 @@ func (client Client) PutBucketReplication(bucketName string, xmlBody string, opt
19541954
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
19551955
}
19561956

1957+
// PutBucketRTC put bucket replication rtc
1958+
// bucketName the bucket name.
1959+
// rtc the bucket rtc config.
1960+
// error it's nil if no error, otherwise it's an error object.
1961+
//
1962+
func (client Client) PutBucketRTC(bucketName string, rtc PutBucketRTC, options ...Option) error {
1963+
bs, err := xml.Marshal(rtc)
1964+
if err != nil {
1965+
return err
1966+
}
1967+
err = client.PutBucketRTCXml(bucketName, string(bs), options...)
1968+
return err
1969+
}
1970+
1971+
// PutBucketRTCXml put bucket rtc configuration
1972+
// bucketName the bucket name.
1973+
// xmlBody the rtc configuration in xml format.
1974+
// error it's nil if no error, otherwise it's an error object.
1975+
//
1976+
func (client Client) PutBucketRTCXml(bucketName string, xmlBody string, options ...Option) error {
1977+
buffer := new(bytes.Buffer)
1978+
buffer.Write([]byte(xmlBody))
1979+
1980+
contentType := http.DetectContentType(buffer.Bytes())
1981+
headers := map[string]string{}
1982+
headers[HTTPHeaderContentType] = contentType
1983+
1984+
params := map[string]interface{}{}
1985+
params["rtc"] = nil
1986+
resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
1987+
if err != nil {
1988+
return err
1989+
}
1990+
defer resp.Body.Close()
1991+
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
1992+
}
1993+
19571994
// GetBucketReplication get bucket replication configuration
19581995
// bucketName the bucket name.
19591996
// string the replication configuration.

oss/client_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4469,14 +4469,14 @@ func (s *OssClientSuite) TestBucketReplicationDeleteWithEmptyRuleID(c *C) {
44694469
data, err := client1.GetBucketReplication(sourceBucketNameTest)
44704470
c.Assert(err, IsNil)
44714471

4472-
var result GetResult
4472+
var result GetBucketReplicationResult
44734473
err = xml.Unmarshal([]byte(data), &result)
44744474
c.Assert(err, IsNil)
44754475

4476-
c.Assert(result.Rules[0].Status, Equals, "starting")
4477-
c.Assert(result.Rules[0].Destination.Location, Equals, "oss-cn-"+destinationRegion)
4478-
c.Assert(result.Rules[0].Destination.Bucket, Equals, destinationBucketNameTest)
4479-
c.Assert(result.Rules[0].HistoricalObjectReplication, Equals, "enabled")
4476+
c.Assert(result.Rule[0].Status, Equals, "starting")
4477+
c.Assert(result.Rule[0].Destination.Location, Equals, "oss-cn-"+destinationRegion)
4478+
c.Assert(result.Rule[0].Destination.Bucket, Equals, destinationBucketNameTest)
4479+
c.Assert(result.Rule[0].HistoricalObjectReplication, Equals, "enabled")
44804480

44814481
ruleID := ""
44824482

@@ -4517,6 +4517,16 @@ func (s *OssClientSuite) TestBucketReplicationGetLocation(c *C) {
45174517

45184518
c.Assert(strings.Contains(data, "<ReplicationLocation>"), Equals, true)
45194519

4520+
stringData, err := client1.GetBucketReplicationLocation(sourceBucketNameTest)
4521+
var repResult GetBucketReplicationLocationResult
4522+
err = xml.Unmarshal([]byte(stringData), &repResult)
4523+
c.Assert(err, IsNil)
4524+
c.Assert(repResult.Location[0], Equals, "oss-ap-northeast-1")
4525+
c.Assert(repResult.Location[9], Equals, "oss-cn-beijing")
4526+
c.Assert(repResult.LocationTransferType[1].Location, Equals, "oss-eu-central-1")
4527+
c.Assert(repResult.LocationTransferType[1].TransferTypes, Equals, "oss_acc")
4528+
c.Assert(repResult.RTCLocation[2], Equals, "oss-cn-shanghai")
4529+
45204530
err = client1.DeleteBucket(sourceBucketNameTest)
45214531
c.Assert(err, IsNil)
45224532
}
@@ -4619,6 +4629,17 @@ func (s *OssClientSuite) TestBucketReplicationGetProgressWithRuleID(c *C) {
46194629
c.Assert(progressResult.Rules[0].Destination.Bucket, Equals, secondDestinationBucketNameTest)
46204630
c.Assert(progressResult.Rules[0].HistoricalObjectReplication, Equals, "enabled")
46214631

4632+
stringData, err := client1.GetBucketReplicationProgress(sourceBucketNameTest, ruleID)
4633+
4634+
var reqProgress GetBucketReplicationProgressResult
4635+
err = xml.Unmarshal([]byte(stringData), &reqProgress)
4636+
c.Assert(err, IsNil)
4637+
c.Assert(reqProgress.Rule[0].ID, Equals, ruleID)
4638+
c.Assert(reqProgress.Rule[0].Status, Equals, "starting")
4639+
c.Assert(reqProgress.Rule[0].Destination.Location, Equals, "oss-cn-"+secondDestinationRegion)
4640+
c.Assert(reqProgress.Rule[0].Destination.Bucket, Equals, secondDestinationBucketNameTest)
4641+
c.Assert(reqProgress.Rule[0].HistoricalObjectReplication, Equals, "enabled")
4642+
46224643
err = client1.DeleteBucket(sourceBucketNameTest)
46234644
c.Assert(err, IsNil)
46244645

oss/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var signKeyList = []string{"acl", "uploads", "location", "cors",
4949
"x-oss-enable-md5", "x-oss-enable-sha1", "x-oss-enable-sha256",
5050
"x-oss-hash-ctx", "x-oss-md5-ctx", "transferAcceleration",
5151
"regionList", "cloudboxes", "x-oss-ac-source-ip", "x-oss-ac-subnet-mask", "x-oss-ac-vpc-id", "x-oss-ac-forward-allow",
52-
"metaQuery", "resourceGroup",
52+
"metaQuery", "resourceGroup","rtc",
5353
}
5454

5555
// init initializes Conn

oss/type.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,88 @@ type ReplicationXML struct {
13491349
ID string `xml:"ID,omitempty"`
13501350
}
13511351

1352+
// PutBucketReplication define the bucket replication config
1353+
type PutBucketReplication BucketReplicationXml
1354+
1355+
// GetBucketReplicationResult define get bucket's replication config
1356+
type GetBucketReplicationResult BucketReplicationXml
1357+
1358+
// GetBucketReplicationLocationResult define get bucket's replication location
1359+
type GetBucketReplicationLocationResult BucketReplicationLocationXml
1360+
1361+
// GetBucketReplicationProgressResult define get bucket's replication progress
1362+
type GetBucketReplicationProgressResult BucketReplicationProgressXml
1363+
1364+
// PutBucketRTC define the bucket rtc config
1365+
type PutBucketRTC BucketRTCXml
1366+
1367+
// BucketReplicationXml define the xml of bucket replication config
1368+
type BucketReplicationXml struct {
1369+
XMLName xml.Name `xml:"ReplicationConfiguration"`
1370+
Rule []ReplicationRule `xml:"Rule,omitempty"`
1371+
}
1372+
1373+
// BucketReplicationProgressXml define the xml of bucket replication config
1374+
type BucketReplicationProgressXml struct {
1375+
XMLName xml.Name `xml:"ReplicationProgress"`
1376+
Rule []ReplicationRule `xml:"Rule,omitempty"`
1377+
}
1378+
1379+
// BucketRTCXml define the xml of bucket rtc config
1380+
type BucketRTCXml struct {
1381+
XMLName xml.Name `xml:"ReplicationRule"`
1382+
RTC *string `xml:"RTC>Status,omitempty"`
1383+
ID string `xml:"ID,omitempty"`
1384+
}
1385+
1386+
// ReplicationRule define the xml of bucket replication config rule
1387+
type ReplicationRule struct {
1388+
ID string `xml:"ID,omitempty"`
1389+
RTC *string `xml:"RTC>Status,omitempty"`
1390+
PrefixSet *ReplicationRulePrefix `xml:"PrefixSet,omitempty"`
1391+
Action string `xml:"Action,omitempty"`
1392+
Destination *ReplicationRuleDestination `xml:"Destination,omitempty"`
1393+
HistoricalObjectReplication string `xml:"HistoricalObjectReplication,omitempty"`
1394+
Status string `xml:"Status,omitempty"`
1395+
SyncRole string `xml:"SyncRole,omitempty"`
1396+
SourceSelectionCriteria *string `xml:"SourceSelectionCriteria>SseKmsEncryptedObjects>Status,omitempty"`
1397+
EncryptionConfiguration *string `xml:"EncryptionConfiguration>ReplicaKmsKeyID,omitempty"`
1398+
Progress *ReplicationRuleProgress `xml:"Progress,omitempty"`
1399+
HistoricalObject string `xml:"HistoricalObject,omitempty"`
1400+
}
1401+
1402+
type ReplicationRulePrefix struct {
1403+
Prefix []*string `xml:"Prefix,omitempty"`
1404+
}
1405+
1406+
type ReplicationRuleDestination struct {
1407+
Bucket string `xml:"Bucket,omitempty"`
1408+
Location string `xml:"Location,omitempty"`
1409+
TransferType string `xml:"TransferType,omitempty"`
1410+
}
1411+
1412+
// BucketReplicationLocationXml define the xml of bucket replication location info
1413+
type BucketReplicationLocationXml struct {
1414+
XMLName xml.Name `xml:"ReplicationLocation"`
1415+
Location []string `xml:"Location,omitempty"`
1416+
LocationTransferType []ReplicationLocationTransferType `xml:"LocationTransferTypeConstraint>LocationTransferType,omitempty"`
1417+
RTCLocation []string `xml:"LocationRTCConstraint>Location,omitempty"`
1418+
}
1419+
1420+
type ReplicationLocation struct {
1421+
Location string `xml:"Location,omitempty"`
1422+
}
1423+
1424+
type ReplicationLocationTransferType struct {
1425+
Location string `xml:"Location,omitempty"`
1426+
TransferTypes string `xml:"TransferTypes>Type,omitempty"`
1427+
}
1428+
1429+
type ReplicationRuleProgress struct {
1430+
HistoricalObject string `xml:"HistoricalObject,omitempty"`
1431+
NewObject string `xml:"NewObject,omitempty"`
1432+
}
1433+
13521434
// CnameConfigurationXML define cname configuration
13531435
type CnameConfigurationXML struct {
13541436
XMLName xml.Name `xml:"BucketCnameConfiguration"`
@@ -1489,7 +1571,6 @@ type BucketAccessMonitorXml struct {
14891571
Status string `xml:"Status"` // access monitor status
14901572
}
14911573

1492-
14931574
// ListBucketCnameResult define the cname list of the bucket
14941575
type ListBucketCnameResult BucketCnameXml
14951576

0 commit comments

Comments
 (0)