Skip to content

Commit

Permalink
SetBucketLifecycle support overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
taowei.wtw authored and kkuai committed Aug 26, 2021
1 parent abc7094 commit 406a713
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
19 changes: 19 additions & 0 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,25 @@ func (client Client) SetBucketLifecycle(bucketName string, rules []LifecycleRule
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
}

// SetBucketLifecycleXml sets the bucket's lifecycle rule from xml config
func (client Client) SetBucketLifecycleXml(bucketName string, xmlBody string, options ...Option) error {
buffer := new(bytes.Buffer)
buffer.Write([]byte(xmlBody))

contentType := http.DetectContentType(buffer.Bytes())
headers := map[string]string{}
headers[HTTPHeaderContentType] = contentType

params := map[string]interface{}{}
params["lifecycle"] = nil
resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
if err != nil {
return err
}
defer resp.Body.Close()
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
}

// DeleteBucketLifecycle deletes the bucket's lifecycle.
//
//
Expand Down
86 changes: 86 additions & 0 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,92 @@ func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
c.Assert(err, IsNil)
}

// TestSetBucketLifecycleOverLap
func (s *OssClientSuite) TestSetBucketLifecycleOverLap(c *C) {
var bucketNameTest = bucketNamePrefix + RandLowStr(6)

// rule1's prefix and rule2's prefix are overlap
var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
rule1.Prefix = "prefix1"
var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
rule2.Prefix = "prefix1/prefix2"

client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

err = client.CreateBucket(bucketNameTest)
c.Assert(err, IsNil)

// overlap is error
var rules = []LifecycleRule{rule1, rule2}
err = client.SetBucketLifecycle(bucketNameTest, rules)
c.Assert(err, NotNil)

//enable overlap,success
options := []Option{AllowSameActionOverLap(true)}
err = client.SetBucketLifecycle(bucketNameTest, rules, options...)
c.Assert(err, IsNil)
err = client.DeleteBucket(bucketNameTest)

}

// TestSetBucketLifecycleXml
func (s *OssClientSuite) TestSetBucketLifecycleXml(c *C) {
var bucketNameTest = bucketNamePrefix + RandLowStr(6)

client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

err = client.CreateBucket(bucketNameTest)
c.Assert(err, IsNil)

xmlBody := `<?xml version="1.0" encoding="UTF-8"?>
<LifecycleConfiguration>
<Rule>
<ID>RuleID1</ID>
<Prefix>Prefix</Prefix>
<Status>Enabled</Status>
<Expiration>
<Days>65</Days>
</Expiration>
<Transition>
<Days>45</Days>
<StorageClass>IA</StorageClass>
</Transition>
<AbortMultipartUpload>
<Days>30</Days>
</AbortMultipartUpload>
</Rule>
<Rule>
<ID>RuleID2</ID>
<Prefix>Prefix/SubPrefix</Prefix>
<Status>Enabled</Status>
<Expiration>
<Days>60</Days>
</Expiration>
<Transition>
<Days>40</Days>
<StorageClass>Archive</StorageClass>
</Transition>
<AbortMultipartUpload>
<Days>40</Days>
</AbortMultipartUpload>
</Rule>
</LifecycleConfiguration>`

// overlap is error
err = client.SetBucketLifecycleXml(bucketNameTest, xmlBody)
c.Assert(err, NotNil)

// enable overlap,success
options := []Option{AllowSameActionOverLap(true)}
err = client.SetBucketLifecycleXml(bucketNameTest, xmlBody, options...)
c.Assert(err, IsNil)

err = client.DeleteBucket(bucketNameTest)
c.Assert(err, IsNil)
}

// TestSetBucketLifecycleAboutVersionObject
func (s *OssClientSuite) TestSetBucketLifecycleAboutVersionObject(c *C) {
var bucketNameTest = bucketNamePrefix + RandLowStr(6)
Expand Down
1 change: 1 addition & 0 deletions oss/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ const (
HTTPHeaderOssTaskID = "X-Oss-Task-Id"
HTTPHeaderOssHashCtx = "X-Oss-Hash-Ctx"
HTTPHeaderOssMd5Ctx = "X-Oss-Md5-Ctx"
HTTPHeaderAllowSameActionOverLap = "X-Oss-Allow-Same-Action-Overlap"
)

// HTTP Param
Expand Down
9 changes: 9 additions & 0 deletions oss/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,3 +678,12 @@ func GetDeleteMark(header http.Header) bool {
func GetQosDelayTime(header http.Header) string {
return header.Get("x-oss-qos-delay-time")
}

// ForbidOverWrite is an option to set X-Oss-Forbid-Overwrite
func AllowSameActionOverLap(enabled bool) Option {
if enabled {
return setHeader(HTTPHeaderAllowSameActionOverLap, "true")
} else {
return setHeader(HTTPHeaderAllowSameActionOverLap, "false")
}
}

0 comments on commit 406a713

Please sign in to comment.