Explorar o código

update lifecycle of bucket: add trasition & abort multipart upload rules

hangzws %!s(int64=7) %!d(string=hai) anos
pai
achega
cfa5bb61a8
Modificáronse 3 ficheiros con 104 adicións e 15 borrados
  1. 2 2
      oss/client.go
  2. 12 0
      oss/const.go
  3. 90 13
      oss/type.go

+ 2 - 2
oss/client.go

@@ -260,8 +260,8 @@ 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 {
-	lxml := lifecycleXML{Rules: convLifecycleRule(rules)}
-	bs, err := xml.Marshal(lxml)
+	lifecycleCfg := LifecycleConfiguration{Rules: rules}
+	bs, err := xml.Marshal(lifecycleCfg)
 	if err != nil {
 		return err
 	}

+ 12 - 0
oss/const.go

@@ -72,6 +72,18 @@ 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"

+ 90 - 13
oss/type.go

@@ -2,6 +2,7 @@ package oss
 
 import (
 	"encoding/xml"
+	"fmt"
 	"net/url"
 	"time"
 )
@@ -42,20 +43,43 @@ type LifecycleConfiguration struct {
 
 // LifecycleRule defines Lifecycle rules
 type LifecycleRule struct {
-	XMLName    xml.Name            `xml:"Rule"`
-	ID         string              `xml:"ID"`         // 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"` // The expiration property
+	XMLName              xml.Name                       `xml:"Rule"`
+	ID                   string                         `xml:"ID"`                           // 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
+	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.
+	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
+}
+
+// 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
+	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
 }
 
+/*
 type lifecycleXML struct {
 	XMLName xml.Name        `xml:"LifecycleConfiguration"`
 	Rules   []lifecycleRule `xml:"Rule"`
@@ -93,26 +117,79 @@ func convLifecycleRule(rules []LifecycleRule) []lifecycleRule {
 	}
 	return rs
 }
+*/
 
-// BuildLifecycleRuleByDays builds a lifecycle rule with specified expiration days
+const iso8601DateFormat = "2006-01-02T15:04:05.000Z"
+
+// BuildLifecycleRuleByDays builds a lifecycle rule objects will expiration in days after the last modified time
 func BuildLifecycleRuleByDays(id, prefix string, status bool, days int) LifecycleRule {
 	var statusStr = "Enabled"
 	if !status {
 		statusStr = "Disabled"
 	}
 	return LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
-		Expiration: LifecycleExpiration{Days: days}}
+		Expiration: &LifecycleExpiration{Days: days}}
 }
 
-// BuildLifecycleRuleByDate builds a lifecycle rule with specified expiration time.
+// BuildLifecycleRuleByDate builds a lifecycle rule objects will expiration in specified date
 func BuildLifecycleRuleByDate(id, prefix string, status bool, year, month, day int) LifecycleRule {
 	var statusStr = "Enabled"
 	if !status {
 		statusStr = "Disabled"
 	}
-	date := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
+	date := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC).Format(iso8601DateFormat)
 	return LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
-		Expiration: LifecycleExpiration{Date: date}}
+		Expiration: &LifecycleExpiration{Date: date}}
+}
+
+// NewLifecleRuleByDays builds a lifecycle rule objects will expiration in days after the last modified time
+func NewLifecleRuleByDays(id, prefix string, status bool, days int, lrt LifecycleRuleType, sc StorageClassType) (*LifecycleRule, error) {
+	var statusStr = "Enabled"
+	if !status {
+		statusStr = "Disabled"
+	}
+	switch lrt {
+	case LRTExpriration:
+		return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
+			Expiration: &LifecycleExpiration{Days: days}}, nil
+	case LRTTransition:
+		if sc != StorageIA && sc != 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}}, nil
+	case LRTAbortMultiPartUpload:
+		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)
+	}
+}
+
+// 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) {
+	var statusStr = "Enabled"
+	if !status {
+		statusStr = "Disabled"
+	}
+
+	date := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC).Format(iso8601DateFormat)
+	switch lrt {
+	case LRTExpriration:
+		return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
+			Expiration: &LifecycleExpiration{CreatedBeforeDate: date}}, nil
+	case LRTTransition:
+		if sc != StorageIA && sc != 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{CreatedBeforeDate: date, StorageClass: sc}}, nil
+	case LRTAbortMultiPartUpload:
+		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)
+	}
 }
 
 // GetBucketLifecycleResult defines GetBucketLifecycle's result object