diff --git a/oss/client_test.go b/oss/client_test.go index eb448498..c13ba6bf 100644 --- a/oss/client_test.go +++ b/oss/client_test.go @@ -655,15 +655,9 @@ func (s *OssClientSuite) TestSetBucketLifecycle(c *C) { c.Assert(err, IsNil) } -// TestSetBucketLifecycle +// TestSetBucketLifecycleNew func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) { var bucketNameTest = bucketNamePrefix + randLowStr(6) - rule1, err := NewLifecycleRuleByCreateBeforeDate("rule1", "one", true, 2015, 11, 11, LRTExpriration) - c.Assert(err, IsNil) - rule2, err := NewLifecycleRuleByDays("rule2", "two", true, 3, LRTAbortMultiPartUpload) - c.Assert(err, IsNil) - rule3, err := NewLifecycleRuleByDays("rule3", "three", true, 3, LRTTransition, StorageIA) - c.Assert(err, IsNil) client, err := New(endpoint, accessID, accessKey) c.Assert(err, IsNil) @@ -671,11 +665,48 @@ func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) { err = client.CreateBucket(bucketNameTest) c.Assert(err, IsNil) - // Set single rule - var rules = []LifecycleRule{*rule1} + expiration := LifecycleExpiration{ + CreatedBeforeDate: randStr(10), + } + rule, err := NewLifecycleRule("rule1", "one", true, &expiration, nil) + c.Assert(err, IsNil) + rules := []LifecycleRule{*rule} + err = client.SetBucketLifecycle(bucketNameTest, rules) + c.Assert(err, NotNil) + + abortMPU := LifecycleAbortMultipartUpload{ + Days: -30, + } + rule, err = NewLifecycleRule("rule2", "two", true, nil, &abortMPU) + c.Assert(err, IsNil) + rules = []LifecycleRule{*rule} err = client.SetBucketLifecycle(bucketNameTest, rules) + c.Assert(err, NotNil) + + expiration = LifecycleExpiration{ + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + } + rule1, err := NewLifecycleRule("rule1", "one", true, &expiration, nil) c.Assert(err, IsNil) - // Double set rule + abortMPU = LifecycleAbortMultipartUpload{ + Days: 30, + } + rule2, err := NewLifecycleRule("rule2", "two", true, &expiration, &abortMPU) + c.Assert(err, IsNil) + + transition1 := LifecycleTransition{ + Days: 3, + StorageClass: StorageIA, + } + transition2 := LifecycleTransition{ + Days: 30, + StorageClass: StorageArchive, + } + rule3, err := NewLifecycleRule("rule3", "three", true, nil, &abortMPU, &transition1, &transition2) + c.Assert(err, IsNil) + + // Set single rule + rules = []LifecycleRule{*rule1} err = client.SetBucketLifecycle(bucketNameTest, rules) c.Assert(err, IsNil) @@ -684,27 +715,35 @@ func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) { c.Assert(len(res.Rules), Equals, 1) c.Assert(res.Rules[0].ID, Equals, "rule1") c.Assert(res.Rules[0].Expiration, NotNil) + c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z") err = client.DeleteBucketLifecycle(bucketNameTest) c.Assert(err, IsNil) - // Set two rules + // Set three rules rules = []LifecycleRule{*rule1, *rule2, *rule3} err = client.SetBucketLifecycle(bucketNameTest, rules) c.Assert(err, IsNil) - // Eliminate effect of cache - time.Sleep(timeoutInOperation) - res, err = client.GetBucketLifecycle(bucketNameTest) c.Assert(err, IsNil) c.Assert(len(res.Rules), Equals, 3) c.Assert(res.Rules[0].ID, Equals, "rule1") c.Assert(res.Rules[0].Expiration, NotNil) + c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z") c.Assert(res.Rules[1].ID, Equals, "rule2") + c.Assert(res.Rules[1].Expiration, NotNil) + c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z") c.Assert(res.Rules[1].AbortMultipartUpload, NotNil) + c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30) c.Assert(res.Rules[2].ID, Equals, "rule3") + c.Assert(res.Rules[2].AbortMultipartUpload, NotNil) + c.Assert(res.Rules[2].AbortMultipartUpload.Days, Equals, 30) c.Assert(res.Rules[2].Transition, NotNil) + c.Assert(res.Rules[2].Transition[0].StorageClass, Equals, StorageIA) + c.Assert(res.Rules[2].Transition[0].Days, Equals, 3) + c.Assert(res.Rules[2].Transition[1].StorageClass, Equals, StorageArchive) + c.Assert(res.Rules[2].Transition[1].Days, Equals, 30) err = client.DeleteBucket(bucketNameTest) c.Assert(err, IsNil) @@ -723,14 +762,14 @@ func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) { err = client.CreateBucket(bucketNameTest) c.Assert(err, IsNil) - time.Sleep(timeoutInOperation) + //time.Sleep(timeoutInOperation) err = client.DeleteBucketLifecycle(bucketNameTest) c.Assert(err, IsNil) err = client.SetBucketLifecycle(bucketNameTest, rules) c.Assert(err, IsNil) - time.Sleep(timeoutInOperation) + //time.Sleep(timeoutInOperation) res, err := client.GetBucketLifecycle(bucketNameTest) c.Assert(err, IsNil) @@ -740,12 +779,12 @@ func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) { err = client.DeleteBucketLifecycle(bucketNameTest) c.Assert(err, IsNil) - time.Sleep(timeoutInOperation) + //time.Sleep(timeoutInOperation) res, err = client.GetBucketLifecycle(bucketNameTest) c.Assert(err, NotNil) // Eliminate effect of cache - time.Sleep(timeoutInOperation) + //time.Sleep(timeoutInOperation) // Delete when not set err = client.DeleteBucketLifecycle(bucketNameTest) diff --git a/oss/const.go b/oss/const.go index 7960d8b0..d1eb4b5f 100644 --- a/oss/const.go +++ b/oss/const.go @@ -72,18 +72,6 @@ const ( HTTPDelete HTTPMethod = "DELETE" ) -// LifecycleRuleType rule type of lifecycle -type LifecycleRuleType string - -const ( - // LRTExpriration Expriation - LRTExpriration LifecycleRuleType = "Expriation" - // LRTTransition Transition - LRTTransition LifecycleRuleType = "Transition" - // LRTAbortMultiPartUpload AbortMultiPartUpload - LRTAbortMultiPartUpload LifecycleRuleType = "AbortMultiPartUpload" -) - // HTTP headers const ( HTTPHeaderAcceptEncoding string = "Accept-Encoding" diff --git a/oss/type.go b/oss/type.go index f8ea2c0f..ef7bd054 100644 --- a/oss/type.go +++ b/oss/type.go @@ -48,35 +48,31 @@ type LifecycleRule struct { Prefix string `xml:"Prefix"` // The object key prefix Status string `xml:"Status"` // The rule status (enabled or not) Expiration *LifecycleExpiration `xml:"Expiration,omitempty"` // The expiration property - Transition *LifecycleTransition `xml:"Transition,omitempty"` // The transition property + Transition []*LifecycleTransition `xml:"Transition,omitempty"` // The transition property AbortMultipartUpload *LifecycleAbortMultipartUpload `xml:"AbortMultipartUpload,omitempty"` // The AbortMultipartUpload property } // LifecycleExpiration defines the rule's expiration property type LifecycleExpiration struct { - XMLName xml.Name `xml:"Expiration"` - Days int `xml:"Days,omitempty"` // Relative expiration time: The expiration time in days after the last modified time - //Date *time.Time `xml:"Date,omitempty"` // Absolute expiration time: The expiration time in date. - //CreatedBeforeDate *time.Time `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired - Date string `xml:"Date,omitempty"` // Absolute expiration time: The expiration time in date. - CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired + XMLName xml.Name `xml:"Expiration"` + Days int `xml:"Days,omitempty"` // Relative expiration time: The expiration time in days after the last modified time + Date string `xml:"Date,omitempty"` // Absolute expiration time: The expiration time in date, not recommended + CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired } // LifecycleTransition defines the rule's transition propery type LifecycleTransition struct { - XMLName xml.Name `xml:"Transition"` - Days int `xml:"Days,omitempty"` // Relative transition time: The transition time in days after the last modified time - //CreatedBeforeDate *time.Time `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be restored + XMLName xml.Name `xml:"Transition"` + Days int `xml:"Days,omitempty"` // Relative transition time: The transition time in days after the last modified time CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired StorageClass StorageClassType `xml:"StorageClass,omitempty"` // Specifies the target storage type } // LifecycleAbortMultipartUpload defines the rule's abort multipart upload propery type LifecycleAbortMultipartUpload struct { - XMLName xml.Name `xml:"AbortMultipartUpload"` - Days int `xml:"Days,omitempty"` // Relative expiration time: The expiration time in days after the last modified time - //CreatedBeforeDate *time.Time `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired - CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired + XMLName xml.Name `xml:"AbortMultipartUpload"` + Days int `xml:"Days,omitempty"` // Relative expiration time: The expiration time in days after the last modified time + CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired } const iso8601DateFormat = "2006-01-02T15:04:05.000Z" @@ -102,80 +98,57 @@ func BuildLifecycleRuleByDate(id, prefix string, status bool, year, month, day i Expiration: &LifecycleExpiration{Date: date}} } -// NewLifecycleRuleByDays builds a lifecycle rule objects will expiration in days after the last modified time -func NewLifecycleRuleByDays(id, prefix string, status bool, days int, lrt LifecycleRuleType, sc ...StorageClassType) (*LifecycleRule, error) { - if len(sc) > 1 { - return nil, fmt.Errorf("invalid count of storage class type, the count should be 0 or 1") - } - - var statusStr = "Enabled" +// NewLifecycleRule build a lifecycle rule +func NewLifecycleRule(id, prefix string, status bool, expiration *LifecycleExpiration, abortMPU *LifecycleAbortMultipartUpload, transitions ...*LifecycleTransition) (*LifecycleRule, error) { + statusStr := "Enabled" if !status { statusStr = "Disabled" } - switch lrt { - case LRTExpriration: - if len(sc) == 1 { - return nil, fmt.Errorf("the count of storage class type should be 0") - } - return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr, - Expiration: &LifecycleExpiration{Days: days}}, nil - case LRTTransition: - if len(sc) == 0 { - return nil, fmt.Errorf("miss storage class of transition lifecycle rule") - } - if sc[0] != StorageIA && sc[0] != StorageArchive { - return nil, fmt.Errorf("invalid storage class of transition lifecycle rule, storage class: %v", sc) - } - return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr, - Transition: &LifecycleTransition{Days: days, StorageClass: sc[0]}}, nil - case LRTAbortMultiPartUpload: - if len(sc) == 1 { - return nil, fmt.Errorf("the count of storage class type should be 0") - } - return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr, - AbortMultipartUpload: &LifecycleAbortMultipartUpload{Days: days}}, nil - default: - return nil, fmt.Errorf("invalid type of lifecycle rule: %v", lrt) + rule := LifecycleRule{ + ID: id, + Prefix: prefix, + Status: statusStr, } -} -// NewLifecycleRuleByCreateBeforeDate builds a lifecycle rule objects created before the date will be expired. -func NewLifecycleRuleByCreateBeforeDate(id, prefix string, status bool, year, month, day int, lrt LifecycleRuleType, sc ...StorageClassType) (*LifecycleRule, error) { - if len(sc) > 1 { - return nil, fmt.Errorf("invalid count of storage class type, the cound should be 0 or 1") + if expiration != nil { + if (expiration.Days != 0 && expiration.CreatedBeforeDate != "") || (expiration.Days == 0 && expiration.CreatedBeforeDate == "") { + return nil, fmt.Errorf("invalid expiration lifecycle, must be set one of CreatedBeforeDate and Days") + } + rule.Expiration = expiration } - var statusStr = "Enabled" - if !status { - statusStr = "Disabled" + if abortMPU != nil { + if (abortMPU.Days != 0 && abortMPU.CreatedBeforeDate != "") || (abortMPU.Days == 0 && abortMPU.CreatedBeforeDate == "") { + return nil, fmt.Errorf("invalid abort multipart upload lifecycle, must be set one of CreatedBeforeDate and Days") + } + rule.AbortMultipartUpload = abortMPU } - date := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC).Format(iso8601DateFormat) - switch lrt { - case LRTExpriration: - if len(sc) == 1 { - return nil, fmt.Errorf("the count of storage class type should be 0") - } - return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr, - Expiration: &LifecycleExpiration{CreatedBeforeDate: date}}, nil - case LRTTransition: - if len(sc) == 0 { - return nil, fmt.Errorf("miss storage class of transition lifecycle rule") - } - if sc[0] != StorageIA && sc[0] != StorageArchive { - return nil, fmt.Errorf("invalid storage class of transition lifecycle rule, storage class: %v", sc) + if len(transitions) > 0 { + if len(transitions) > 2 { + return nil, fmt.Errorf("invalid count of transition lifecycles, the count must than less than 3") } - return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr, - Transition: &LifecycleTransition{CreatedBeforeDate: date, StorageClass: sc[0]}}, nil - case LRTAbortMultiPartUpload: - if len(sc) == 1 { - return nil, fmt.Errorf("the count of storage class type should be 0") + + for _, transition := range transitions { + if transition == nil { + return nil, fmt.Errorf("invalid transitions, there is a transition not be initiated") + } + if (transition.Days != 0 && transition.CreatedBeforeDate != "") || (transition.Days == 0 && transition.CreatedBeforeDate == "") { + return nil, fmt.Errorf("invalid transition lifecycle, must be set one of CreatedBeforeDate and Days") + } + if transition.StorageClass != StorageIA && transition.StorageClass != StorageArchive { + return nil, fmt.Errorf("invalid transition lifecylce, the value of storage class must be IA or Archive") + } } - return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr, - AbortMultipartUpload: &LifecycleAbortMultipartUpload{CreatedBeforeDate: date}}, nil - default: - return nil, fmt.Errorf("invalid type of lifecycle rule: %v", lrt) + + rule.Transition = transitions } + + if rule.Expiration == nil && rule.AbortMultipartUpload == nil && len(rule.Transition) == 0 { + return nil, fmt.Errorf("invalid lifecycle rule, must be set one of Expiration, AbortMultipartUpload and Transition") + } + + return &rule, nil } // GetBucketLifecycleResult defines GetBucketLifecycle's result object diff --git a/oss/type_test.go b/oss/type_test.go index 31f59d97..ef3b905d 100644 --- a/oss/type_test.go +++ b/oss/type_test.go @@ -107,70 +107,143 @@ func (s *OssTypeSuite) TestSortUploadPart(c *C) { c.Assert(parts[4].ETag, Equals, "E5") } -func (s *OssTypeSuite) TestNewLifecleRuleByDays(c *C) { - _, err := NewLifecycleRuleByDays("rule1", "one", true, 30, LRTExpriration) - c.Assert(err, IsNil) +func (s *OssTypeSuite) TestNewLifecleRule(c *C) { + expiration := LifecycleExpiration{ + Days: 30, + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + } + _, err := NewLifecycleRule("ruleID", "prefix", true, &expiration, nil) + c.Assert(err, NotNil) - _, err = NewLifecycleRuleByDays("rule2", "two", true, 30, LRTAbortMultiPartUpload) - c.Assert(err, IsNil) + expiration = LifecycleExpiration{ + Days: 0, + CreatedBeforeDate: "", + } + _, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, nil) + c.Assert(err, NotNil) - _, err = NewLifecycleRuleByDays("rule3", "three", true, 30, LRTTransition, StorageIA) - c.Assert(err, IsNil) + abortMPU := LifecycleAbortMultipartUpload{ + Days: 30, + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + } + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, &abortMPU) + c.Assert(err, NotNil) - _, err = NewLifecycleRuleByDays("rule4", "four", true, 30, LRTTransition, StorageArchive) - c.Assert(err, IsNil) + abortMPU = LifecycleAbortMultipartUpload{ + Days: 0, + CreatedBeforeDate: "", + } + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, &abortMPU) + c.Assert(err, NotNil) - // expiration lifecycle type, set storage class type - _, err = NewLifecycleRuleByDays("rule5", "five", true, 30, LRTExpriration, StorageIA) + transition := LifecycleTransition{ + Days: 30, + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + StorageClass: StorageIA, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition) c.Assert(err, NotNil) - // abort multipart upload lifecycle type, set storage class type - _, err = NewLifecycleRuleByDays("rule6", "six", true, 30, LRTAbortMultiPartUpload, StorageIA) + transition = LifecycleTransition{ + Days: 0, + CreatedBeforeDate: "", + StorageClass: StorageIA, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition) c.Assert(err, NotNil) - // transition lifecycle type, the value of storage class type is StorageStandard - _, err = NewLifecycleRuleByDays("rule7", "seven", true, 30, LRTTransition, StorageStandard) + transition = LifecycleTransition{ + Days: 30, + StorageClass: StorageStandard, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition) c.Assert(err, NotNil) - // transition lifecycle type, do not set storage class type - _, err = NewLifecycleRuleByDays("rule8", "eight", true, 30, LRTTransition) + transition = LifecycleTransition{ + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + StorageClass: StorageStandard, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition) c.Assert(err, NotNil) - // transition lifecycle type,set two storage class type - _, err = NewLifecycleRuleByDays("rule9", "nine", true, 30, LRTTransition, StorageIA, StorageArchive) + transition1 := LifecycleTransition{ + Days: 30, + StorageClass: StorageIA, + } + transition2 := LifecycleTransition{ + Days: 60, + StorageClass: StorageArchive, + } + transition3 := LifecycleTransition{ + Days: 100, + StorageClass: StorageArchive, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition1, &transition2, &transition3) c.Assert(err, NotNil) -} -func (s *OssTypeSuite) TestNewLifecycleRuleByCreateBeforeDate(c *C) { - _, err := NewLifecycleRuleByCreateBeforeDate("rule1", "one", true, 2019, 3, 30, LRTExpriration) - c.Assert(err, IsNil) + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil) + c.Assert(err, NotNil) - _, err = NewLifecycleRuleByCreateBeforeDate("rule2", "two", true, 2019, 3, 30, LRTAbortMultiPartUpload) + expiration = LifecycleExpiration{ + Days: 30, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, nil) c.Assert(err, IsNil) - _, err = NewLifecycleRuleByCreateBeforeDate("rule3", "three", true, 2019, 3, 30, LRTTransition, StorageIA) + expiration = LifecycleExpiration{ + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + } + _, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, nil) c.Assert(err, IsNil) - _, err = NewLifecycleRuleByCreateBeforeDate("rule4", "four", true, 2019, 3, 30, LRTTransition, StorageArchive) + abortMPU = LifecycleAbortMultipartUpload{ + Days: 30, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, &abortMPU) c.Assert(err, IsNil) - // expiration lifecycle type, set storage class type - _, err = NewLifecycleRuleByCreateBeforeDate("rule5", "five", true, 2019, 3, 30, LRTExpriration, StorageIA) - c.Assert(err, NotNil) - - // abort multipart upload lifecycle type, set storage class type - _, err = NewLifecycleRuleByCreateBeforeDate("rule6", "six", true, 2019, 3, 30, LRTAbortMultiPartUpload, StorageIA) - c.Assert(err, NotNil) + abortMPU = LifecycleAbortMultipartUpload{ + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + } + _, err = NewLifecycleRule("ruleID", "prefix", true, nil, &abortMPU) + c.Assert(err, IsNil) - // transition lifecycle type, the value of storage class type is StorageStandard - _, err = NewLifecycleRuleByCreateBeforeDate("rule7", "seven", true, 2019, 3, 30, LRTTransition, StorageStandard) - c.Assert(err, NotNil) + expiration = LifecycleExpiration{ + Days: 30, + } + abortMPU = LifecycleAbortMultipartUpload{ + Days: 30, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, &abortMPU) + c.Assert(err, IsNil) - // transition lifecycle type, do not set storage class type - _, err = NewLifecycleRuleByCreateBeforeDate("rule8", "eight", true, 2019, 3, 30, LRTTransition) - c.Assert(err, NotNil) + expiration = LifecycleExpiration{ + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + } + abortMPU = LifecycleAbortMultipartUpload{ + Days: 30, + } + transition = LifecycleTransition{ + Days: 30, + StorageClass: StorageIA, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, &abortMPU) + c.Assert(err, IsNil) - // transition lifecycle type,set two storage class type - _, err = NewLifecycleRuleByCreateBeforeDate("rule9", "nine", true, 2019, 3, 30, LRTTransition, StorageIA, StorageArchive) - c.Assert(err, NotNil) + expiration = LifecycleExpiration{ + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + } + abortMPU = LifecycleAbortMultipartUpload{ + Days: 30, + } + transition1 = LifecycleTransition{ + Days: 30, + StorageClass: StorageIA, + } + transition2 = LifecycleTransition{ + Days: 60, + StorageClass: StorageArchive, + } + _, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, &abortMPU, &transition1, &transition2) + c.Assert(err, IsNil) } diff --git a/sample/bucket_lifecycle.go b/sample/bucket_lifecycle.go index a58683c8..afabdcfc 100644 --- a/sample/bucket_lifecycle.go +++ b/sample/bucket_lifecycle.go @@ -20,9 +20,11 @@ func BucketLifecycleSample() { HandleError(err) } - // Case 1: Set the lifecycle. The rule ID is id1 and the applied objects' prefix is one and expired time is 11/11/2015 - //var rule1 = oss.BuildLifecycleRuleByDate("id1", "one", true, 2015, 11, 11) - rule1, err := oss.NewLifecycleRuleByCreateBeforeDate("id1", "one", true, 2015, 11, 11, oss.LRTExpriration) + // Case 1: Set the lifecycle. The rule ID is rule1 and the applied objects' prefix is one and expired time is 11/11/2015 + expriation := oss.LifecycleExpiration{ + CreatedBeforeDate: "2015-11-11T00:00:00.000Z", + } + rule1, err := oss.NewLifecycleRule("rule1", "one", true, &expriation, nil) if err != nil { HandleError(err) } @@ -32,9 +34,23 @@ func BucketLifecycleSample() { HandleError(err) } + // Get the bucket's lifecycle + lc, err := client.GetBucketLifecycle(bucketName) + if err != nil { + HandleError(err) + } + fmt.Printf("Bucket Lifecycle:%v, %v\n", lc.Rules, *lc.Rules[0].Expiration) + // Case 2: Set the lifecycle, The rule ID is id2 and the applied objects' prefix is two and the expired time is three days after the object created. - //var rule2 = oss.BuildLifecycleRuleByDays("id2", "two", true, 3) - rule2, err := oss.NewLifecycleRuleByDays("id2", "two", true, 3, oss.LRTTransition, oss.StorageIA) + transitionIA := oss.LifecycleTransition{ + Days: 3, + StorageClass: oss.StorageIA, + } + transitionArch := oss.LifecycleTransition{ + Days: 30, + StorageClass: oss.StorageArchive, + } + rule2, err := oss.NewLifecycleRule("rule2", "two", true, nil, nil, &transitionIA, &transitionArch) if err != nil { HandleError(err) } @@ -45,13 +61,16 @@ func BucketLifecycleSample() { } // Get the bucket's lifecycle - lc, err := client.GetBucketLifecycle(bucketName) + lc, err = client.GetBucketLifecycle(bucketName) if err != nil { HandleError(err) } - fmt.Println("Bucket Lifecycle:", lc.Rules) + fmt.Printf("Bucket Lifecycle:%v, %v, %v\n", lc.Rules, *lc.Rules[0].Transition[0], *lc.Rules[0].Transition[1]) - rule3, err := oss.NewLifecycleRuleByDays("id3", "three", true, 3, oss.LRTAbortMultiPartUpload) + abortMPU := oss.LifecycleAbortMultipartUpload{ + Days: 3, + } + rule3, err := oss.NewLifecycleRule("rule3", "three", true, nil, &abortMPU) if err != nil { HandleError(err) } @@ -66,7 +85,7 @@ func BucketLifecycleSample() { if err != nil { HandleError(err) } - fmt.Println("Bucket Lifecycle:", lc.Rules) + fmt.Printf("Bucket Lifecycle:%v, %v, %v, %v\n", lc.Rules, *lc.Rules[0].Transition[0], *lc.Rules[0].Transition[1], *lc.Rules[1].AbortMultipartUpload) // Delete bucket's Lifecycle err = client.DeleteBucketLifecycle(bucketName)