瀏覽代碼

add verfiyLifecycleRules method

hangzws 6 年之前
父節點
當前提交
167d453a42
共有 6 個文件被更改,包括 258 次插入93 次删除
  1. 4 0
      oss/client.go
  2. 45 18
      oss/client_test.go
  3. 1 1
      oss/multipart_test.go
  4. 32 42
      oss/type.go
  5. 156 18
      oss/type_test.go
  6. 20 14
      sample/bucket_lifecycle.go

+ 4 - 0
oss/client.go

@@ -260,6 +260,10 @@ func (client Client) GetBucketACL(bucketName string) (GetBucketACLResult, error)
 // error    it's nil if no error, otherwise it's an error object.
 //
 func (client Client) SetBucketLifecycle(bucketName string, rules []LifecycleRule) error {
+	err := verifyLifecycleRules(rules)
+	if err != nil {
+		return err
+	}
 	lifecycleCfg := LifecycleConfiguration{Rules: rules}
 	bs, err := xml.Marshal(lifecycleCfg)
 	if err != nil {

+ 45 - 18
oss/client_test.go

@@ -665,34 +665,55 @@ func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
 	err = client.CreateBucket(bucketNameTest)
 	c.Assert(err, IsNil)
 
+	//invalid value of CreatedBeforeDate
 	expiration := LifecycleExpiration{
 		CreatedBeforeDate: randStr(10),
 	}
-	rule, err := NewLifecycleRule("rule1", "one", true, &expiration, nil)
+	rule := LifecycleRule{
+		ID:         "rule1",
+		Prefix:     "one",
+		Status:     "Enabled",
+		Expiration: &expiration,
+	}
 	c.Assert(err, IsNil)
-	rules := []LifecycleRule{*rule}
+	rules := []LifecycleRule{rule}
 	err = client.SetBucketLifecycle(bucketNameTest, rules)
 	c.Assert(err, NotNil)
 
+	//invalid value of Days
 	abortMPU := LifecycleAbortMultipartUpload{
 		Days: -30,
 	}
-	rule, err = NewLifecycleRule("rule2", "two", true, nil, &abortMPU)
-	c.Assert(err, IsNil)
-	rules = []LifecycleRule{*rule}
+	rule = LifecycleRule{
+		ID:                   "rule1",
+		Prefix:               "one",
+		Status:               "Enabled",
+		AbortMultipartUpload: &abortMPU,
+	}
+	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)
+	rule1 := LifecycleRule{
+		ID:         "rule1",
+		Prefix:     "one",
+		Status:     "Enabled",
+		Expiration: &expiration,
+	}
+
 	abortMPU = LifecycleAbortMultipartUpload{
 		Days: 30,
 	}
-	rule2, err := NewLifecycleRule("rule2", "two", true, &expiration, &abortMPU)
-	c.Assert(err, IsNil)
+	rule2 := LifecycleRule{
+		ID:                   "rule2",
+		Prefix:               "two",
+		Status:               "Enabled",
+		Expiration:           &expiration,
+		AbortMultipartUpload: &abortMPU,
+	}
 
 	transition1 := LifecycleTransition{
 		Days:         3,
@@ -702,11 +723,17 @@ func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
 		Days:         30,
 		StorageClass: StorageArchive,
 	}
-	rule3, err := NewLifecycleRule("rule3", "three", true, nil, &abortMPU, &transition1, &transition2)
-	c.Assert(err, IsNil)
+	transitions := []LifecycleTransition{transition1, transition2}
+	rule3 := LifecycleRule{
+		ID:                   "rule3",
+		Prefix:               "three",
+		Status:               "Enabled",
+		AbortMultipartUpload: &abortMPU,
+		Transitions:          transitions,
+	}
 
 	// Set single rule
-	rules = []LifecycleRule{*rule1}
+	rules = []LifecycleRule{rule1}
 	err = client.SetBucketLifecycle(bucketNameTest, rules)
 	c.Assert(err, IsNil)
 
@@ -721,7 +748,7 @@ func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
 	c.Assert(err, IsNil)
 
 	// Set three rules
-	rules = []LifecycleRule{*rule1, *rule2, *rule3}
+	rules = []LifecycleRule{rule1, rule2, rule3}
 	err = client.SetBucketLifecycle(bucketNameTest, rules)
 	c.Assert(err, IsNil)
 
@@ -739,11 +766,11 @@ func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
 	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)
+	c.Assert(len(res.Rules[2].Transitions), Equals, 2)
+	c.Assert(res.Rules[2].Transitions[0].StorageClass, Equals, StorageIA)
+	c.Assert(res.Rules[2].Transitions[0].Days, Equals, 3)
+	c.Assert(res.Rules[2].Transitions[1].StorageClass, Equals, StorageArchive)
+	c.Assert(res.Rules[2].Transitions[1].Days, Equals, 30)
 
 	err = client.DeleteBucket(bucketNameTest)
 	c.Assert(err, IsNil)

+ 1 - 1
oss/multipart_test.go

@@ -603,7 +603,7 @@ func (s *OssBucketMultipartSuite) TestListMultipartUploads(c *C) {
 	c.Assert(err, IsNil)
 	checkNum := 15
 	for _, im := range imurs {
-		if im.Key == objectName+"12" && im.UploadID > "upLoadIDStr" {
+		if im.Key == objectName+"12" && im.UploadID > upLoadIDStr {
 			checkNum = 16
 			break
 		}

+ 32 - 42
oss/type.go

@@ -44,11 +44,11 @@ type LifecycleConfiguration struct {
 // LifecycleRule defines Lifecycle rules
 type LifecycleRule struct {
 	XMLName              xml.Name                       `xml:"Rule"`
-	ID                   string                         `xml:"ID"`                             // The rule ID
+	ID                   string                         `xml:"ID,omitempty"`                   // The rule ID
 	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
+	Transitions          []LifecycleTransition          `xml:"Transition,omitempty"`           // The transition property
 	AbortMultipartUpload *LifecycleAbortMultipartUpload `xml:"AbortMultipartUpload,omitempty"` // The AbortMultipartUpload property
 }
 
@@ -98,57 +98,47 @@ func BuildLifecycleRuleByDate(id, prefix string, status bool, year, month, day i
 		Expiration: &LifecycleExpiration{Date: date}}
 }
 
-// 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"
-	}
-	rule := LifecycleRule{
-		ID:     id,
-		Prefix: prefix,
-		Status: statusStr,
-	}
-
-	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")
+// ValidateLifecycleRule Determine if a lifecycle rule is valid, if it is invalid, it will return an error.
+func verifyLifecycleRules(rules []LifecycleRule) error {
+	for _, rule := range rules {
+		if rule.Status != "Enabled" && rule.Status != "Disabled" {
+			return fmt.Errorf("invalid rule, the value of status must be Enabled or Disabled")
 		}
-		rule.Expiration = expiration
-	}
 
-	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")
+		expiration := rule.Expiration
+		if expiration != nil {
+			if (expiration.Days != 0 && expiration.CreatedBeforeDate != "") || (expiration.Days != 0 && expiration.Date != "") || (expiration.CreatedBeforeDate != "" && expiration.Date != "") || (expiration.Days == 0 && expiration.CreatedBeforeDate == "" && expiration.Date == "") {
+				return fmt.Errorf("invalid expiration lifecycle, must be set one of CreatedBeforeDate, Days and Date")
+			}
 		}
-		rule.AbortMultipartUpload = abortMPU
-	}
 
-	if len(transitions) > 0 {
-		if len(transitions) > 2 {
-			return nil, fmt.Errorf("invalid count of transition lifecycles, the count must than less than 3")
+		abortMPU := rule.AbortMultipartUpload
+		if abortMPU != nil {
+			if (abortMPU.Days != 0 && abortMPU.CreatedBeforeDate != "") || (abortMPU.Days == 0 && abortMPU.CreatedBeforeDate == "") {
+				return fmt.Errorf("invalid abort multipart upload lifecycle, must be set one of CreatedBeforeDate and Days")
+			}
 		}
 
-		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")
+		transitions := rule.Transitions
+		if len(transitions) > 0 {
+			if len(transitions) > 2 {
+				return fmt.Errorf("invalid count of transition lifecycles, the count must than less than 3")
 			}
-			if transition.StorageClass != StorageIA && transition.StorageClass != StorageArchive {
-				return nil, fmt.Errorf("invalid transition lifecylce, the value of storage class must be IA or Archive")
+
+			for _, transition := range transitions {
+				if (transition.Days != 0 && transition.CreatedBeforeDate != "") || (transition.Days == 0 && transition.CreatedBeforeDate == "") {
+					return fmt.Errorf("invalid transition lifecycle, must be set one of CreatedBeforeDate and Days")
+				}
+				if transition.StorageClass != StorageIA && transition.StorageClass != StorageArchive {
+					return fmt.Errorf("invalid transition lifecylce, the value of storage class must be IA or Archive")
+				}
 			}
+		} else if expiration == nil && abortMPU == nil {
+			return fmt.Errorf("invalid rule, must set one of Expiration, AbortMultipartUplaod and Transitions")
 		}
-
-		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
+	return nil
 }
 
 // GetBucketLifecycleResult defines GetBucketLifecycle's result object

+ 156 - 18
oss/type_test.go

@@ -107,33 +107,76 @@ func (s *OssTypeSuite) TestSortUploadPart(c *C) {
 	c.Assert(parts[4].ETag, Equals, "E5")
 }
 
-func (s *OssTypeSuite) TestNewLifecleRule(c *C) {
+func (s *OssTypeSuite) TestValidateLifecleRules(c *C) {
 	expiration := LifecycleExpiration{
 		Days:              30,
 		CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
 	}
-	_, err := NewLifecycleRule("ruleID", "prefix", true, &expiration, nil)
+	rule := LifecycleRule{
+		ID:         "ruleID",
+		Prefix:     "prefix",
+		Status:     "Enabled",
+		Expiration: &expiration,
+	}
+	rules := []LifecycleRule{rule}
+	err := verifyLifecycleRules(rules)
+	c.Assert(err, NotNil)
+
+	expiration = LifecycleExpiration{
+		Date:              "2015-11-11T00:00:00.000Z",
+		CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
+	}
+	rule = LifecycleRule{
+		ID:         "ruleID",
+		Prefix:     "prefix",
+		Status:     "Enabled",
+		Expiration: &expiration,
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
 	expiration = LifecycleExpiration{
 		Days:              0,
 		CreatedBeforeDate: "",
+		Date:              "",
+	}
+	rule = LifecycleRule{
+		ID:         "ruleID",
+		Prefix:     "prefix",
+		Status:     "Enabled",
+		Expiration: &expiration,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, nil)
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
 	abortMPU := LifecycleAbortMultipartUpload{
 		Days:              30,
 		CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, &abortMPU)
+	rule = LifecycleRule{
+		ID:                   "ruleID",
+		Prefix:               "prefix",
+		Status:               "Enabled",
+		AbortMultipartUpload: &abortMPU,
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
 	abortMPU = LifecycleAbortMultipartUpload{
 		Days:              0,
 		CreatedBeforeDate: "",
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, &abortMPU)
+	rule = LifecycleRule{
+		ID:                   "ruleID",
+		Prefix:               "prefix",
+		Status:               "Enabled",
+		AbortMultipartUpload: &abortMPU,
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
 	transition := LifecycleTransition{
@@ -141,7 +184,14 @@ func (s *OssTypeSuite) TestNewLifecleRule(c *C) {
 		CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
 		StorageClass:      StorageIA,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition)
+	rule = LifecycleRule{
+		ID:          "ruleID",
+		Prefix:      "prefix",
+		Status:      "Enabled",
+		Transitions: []LifecycleTransition{transition},
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
 	transition = LifecycleTransition{
@@ -149,21 +199,42 @@ func (s *OssTypeSuite) TestNewLifecleRule(c *C) {
 		CreatedBeforeDate: "",
 		StorageClass:      StorageIA,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition)
+	rule = LifecycleRule{
+		ID:          "ruleID",
+		Prefix:      "prefix",
+		Status:      "Enabled",
+		Transitions: []LifecycleTransition{transition},
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
 	transition = LifecycleTransition{
 		Days:         30,
 		StorageClass: StorageStandard,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition)
+	rule = LifecycleRule{
+		ID:          "ruleID",
+		Prefix:      "prefix",
+		Status:      "Enabled",
+		Transitions: []LifecycleTransition{transition},
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
 	transition = LifecycleTransition{
 		CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
 		StorageClass:      StorageStandard,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition)
+	rule = LifecycleRule{
+		ID:          "ruleID",
+		Prefix:      "prefix",
+		Status:      "Enabled",
+		Transitions: []LifecycleTransition{transition},
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
 	transition1 := LifecycleTransition{
@@ -178,34 +249,75 @@ func (s *OssTypeSuite) TestNewLifecleRule(c *C) {
 		Days:         100,
 		StorageClass: StorageArchive,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil, &transition1, &transition2, &transition3)
+	rule = LifecycleRule{
+		ID:          "ruleID",
+		Prefix:      "prefix",
+		Status:      "Enabled",
+		Transitions: []LifecycleTransition{transition1, transition2, transition3},
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, nil)
+	rule = LifecycleRule{
+		ID:     "ruleID",
+		Prefix: "prefix",
+		Status: "Enabled",
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, NotNil)
 
 	expiration = LifecycleExpiration{
 		Days: 30,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, nil)
+	rule = LifecycleRule{
+		ID:         "ruleID",
+		Prefix:     "prefix",
+		Status:     "Enabled",
+		Expiration: &expiration,
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, IsNil)
 
 	expiration = LifecycleExpiration{
 		CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, nil)
+	rule = LifecycleRule{
+		ID:         "ruleID",
+		Prefix:     "prefix",
+		Status:     "Enabled",
+		Expiration: &expiration,
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, IsNil)
 
 	abortMPU = LifecycleAbortMultipartUpload{
 		Days: 30,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, &abortMPU)
+	rule = LifecycleRule{
+		ID:                   "ruleID",
+		Prefix:               "prefix",
+		Status:               "Enabled",
+		AbortMultipartUpload: &abortMPU,
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, IsNil)
 
 	abortMPU = LifecycleAbortMultipartUpload{
 		CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, nil, &abortMPU)
+	rule = LifecycleRule{
+		ID:                   "ruleID",
+		Prefix:               "prefix",
+		Status:               "Enabled",
+		AbortMultipartUpload: &abortMPU,
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, IsNil)
 
 	expiration = LifecycleExpiration{
@@ -214,7 +326,15 @@ func (s *OssTypeSuite) TestNewLifecleRule(c *C) {
 	abortMPU = LifecycleAbortMultipartUpload{
 		Days: 30,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, &abortMPU)
+	rule = LifecycleRule{
+		ID:                   "ruleID",
+		Prefix:               "prefix",
+		Status:               "Enabled",
+		Expiration:           &expiration,
+		AbortMultipartUpload: &abortMPU,
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, IsNil)
 
 	expiration = LifecycleExpiration{
@@ -227,7 +347,16 @@ func (s *OssTypeSuite) TestNewLifecleRule(c *C) {
 		Days:         30,
 		StorageClass: StorageIA,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, &abortMPU)
+	rule = LifecycleRule{
+		ID:                   "ruleID",
+		Prefix:               "prefix",
+		Status:               "Enabled",
+		Expiration:           &expiration,
+		AbortMultipartUpload: &abortMPU,
+		Transitions:          []LifecycleTransition{transition},
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, IsNil)
 
 	expiration = LifecycleExpiration{
@@ -244,6 +373,15 @@ func (s *OssTypeSuite) TestNewLifecleRule(c *C) {
 		Days:         60,
 		StorageClass: StorageArchive,
 	}
-	_, err = NewLifecycleRule("ruleID", "prefix", true, &expiration, &abortMPU, &transition1, &transition2)
+	rule = LifecycleRule{
+		ID:                   "ruleID",
+		Prefix:               "prefix",
+		Status:               "Enabled",
+		Expiration:           &expiration,
+		AbortMultipartUpload: &abortMPU,
+		Transitions:          []LifecycleTransition{transition1, transition2},
+	}
+	rules = []LifecycleRule{rule}
+	err = verifyLifecycleRules(rules)
 	c.Assert(err, IsNil)
 }

+ 20 - 14
sample/bucket_lifecycle.go

@@ -24,11 +24,13 @@ func BucketLifecycleSample() {
 	expriation := oss.LifecycleExpiration{
 		CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
 	}
-	rule1, err := oss.NewLifecycleRule("rule1", "one", true, &expriation, nil)
-	if err != nil {
-		HandleError(err)
+	rule1 := oss.LifecycleRule{
+		ID:         "rule1",
+		Prefix:     "one",
+		Status:     "Enabled",
+		Expiration: &expriation,
 	}
-	var rules = []oss.LifecycleRule{*rule1}
+	var rules = []oss.LifecycleRule{rule1}
 	err = client.SetBucketLifecycle(bucketName, rules)
 	if err != nil {
 		HandleError(err)
@@ -50,11 +52,13 @@ func BucketLifecycleSample() {
 		Days:         30,
 		StorageClass: oss.StorageArchive,
 	}
-	rule2, err := oss.NewLifecycleRule("rule2", "two", true, nil, nil, &transitionIA, &transitionArch)
-	if err != nil {
-		HandleError(err)
+	rule2 := oss.LifecycleRule{
+		ID:          "rule2",
+		Prefix:      "two",
+		Status:      "Enabled",
+		Transitions: []oss.LifecycleTransition{transitionIA, transitionArch},
 	}
-	rules = []oss.LifecycleRule{*rule2}
+	rules = []oss.LifecycleRule{rule2}
 	err = client.SetBucketLifecycle(bucketName, rules)
 	if err != nil {
 		HandleError(err)
@@ -65,16 +69,18 @@ func BucketLifecycleSample() {
 	if err != nil {
 		HandleError(err)
 	}
-	fmt.Printf("Bucket Lifecycle:%v, %v, %v\n", lc.Rules, *lc.Rules[0].Transition[0], *lc.Rules[0].Transition[1])
+	fmt.Printf("Bucket Lifecycle:%v\n", lc.Rules)
 
 	abortMPU := oss.LifecycleAbortMultipartUpload{
 		Days: 3,
 	}
-	rule3, err := oss.NewLifecycleRule("rule3", "three", true, nil, &abortMPU)
-	if err != nil {
-		HandleError(err)
+	rule3 := oss.LifecycleRule{
+		ID:                   "rule3",
+		Prefix:               "three",
+		Status:               "Enabled",
+		AbortMultipartUpload: &abortMPU,
 	}
-	rules = append(lc.Rules, *rule3)
+	rules = append(lc.Rules, rule3)
 	err = client.SetBucketLifecycle(bucketName, rules)
 	if err != nil {
 		HandleError(err)
@@ -85,7 +91,7 @@ func BucketLifecycleSample() {
 	if err != nil {
 		HandleError(err)
 	}
-	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)
+	fmt.Printf("Bucket Lifecycle:%v, %v\n", lc.Rules, *lc.Rules[1].AbortMultipartUpload)
 
 	// Delete bucket's Lifecycle
 	err = client.DeleteBucketLifecycle(bucketName)