Skip to content

Commit

Permalink
add optional header in lifecycle and object tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Jul 18, 2022
1 parent 7c596d6 commit 8acf953
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 66 deletions.
98 changes: 59 additions & 39 deletions bucket_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,100 +7,120 @@ import (
)

type BucketLifecycleAndOperator struct {
Prefix string `xml:"Prefix,omitempty"`
Tag []BucketTaggingTag `xml:"Tag,omitempty"`
Prefix string `xml:"Prefix,omitempty" header:"-"`
Tag []BucketTaggingTag `xml:"Tag,omitempty" header:"-"`
}

// BucketLifecycleFilter is the param of BucketLifecycleRule
type BucketLifecycleFilter struct {
Prefix string `xml:"Prefix,omitempty"`
Tag *BucketTaggingTag `xml:"Tag,omitempty"`
And *BucketLifecycleAndOperator `xml:"And,omitempty"`
Prefix string `xml:"Prefix,omitempty" header:"-"`
Tag *BucketTaggingTag `xml:"Tag,omitempty" header:"-"`
And *BucketLifecycleAndOperator `xml:"And,omitempty" header:"-"`
}

// BucketLifecycleExpiration is the param of BucketLifecycleRule
type BucketLifecycleExpiration struct {
Date string `xml:"Date,omitempty"`
Days int `xml:"Days,omitempty"`
ExpiredObjectDeleteMarker bool `xml:"ExpiredObjectDeleteMarker,omitempty"`
Date string `xml:"Date,omitempty" header:"-"`
Days int `xml:"Days,omitempty" header:"-"`
ExpiredObjectDeleteMarker bool `xml:"ExpiredObjectDeleteMarker,omitempty" header:"-"`
}

// BucketLifecycleTransition is the param of BucketLifecycleRule
type BucketLifecycleTransition struct {
Date string `xml:"Date,omitempty"`
Days int `xml:"Days,omitempty"`
StorageClass string
Date string `xml:"Date,omitempty" header:"-"`
Days int `xml:"Days,omitempty" header:"-"`
StorageClass string `xml:"StorageClass,omitempty" header:"-"`
}

type BucketLifecycleNoncurrentVersion struct {
NoncurrentDays int `xml:"NoncurrentDays,omitempty"`
StorageClass string `xml:"StorageClass,omitempty"`
NoncurrentDays int `xml:"NoncurrentDays,omitempty" header:"-"`
StorageClass string `xml:"StorageClass,omitempty" header:"-"`
}

// BucketLifecycleAbortIncompleteMultipartUpload is the param of BucketLifecycleRule
type BucketLifecycleAbortIncompleteMultipartUpload struct {
DaysAfterInitiation int `xml:"DaysAfterInitiation,omitempty"`
DaysAfterInitiation int `xml:"DaysAfterInitiation,omitempty" header:"-"`
}

// BucketLifecycleRule is the rule of BucketLifecycle
type BucketLifecycleRule struct {
ID string `xml:"ID,omitempty"`
Status string
Filter *BucketLifecycleFilter `xml:"Filter,omitempty"`
Transition []BucketLifecycleTransition `xml:"Transition,omitempty"`
Expiration *BucketLifecycleExpiration `xml:"Expiration,omitempty"`
AbortIncompleteMultipartUpload *BucketLifecycleAbortIncompleteMultipartUpload `xml:"AbortIncompleteMultipartUpload,omitempty"`
NoncurrentVersionTransition []BucketLifecycleNoncurrentVersion `xml:"NoncurrentVersionTransition,omitempty"`
NoncurrentVersionExpiration *BucketLifecycleNoncurrentVersion `xml:"NoncurrentVersionExpiration,omitempty"`
ID string `xml:"ID,omitempty" header:"-"`
Status string `xml:"Status,omitempty" header:"-"`
Filter *BucketLifecycleFilter `xml:"Filter,omitempty" header:"-"`
Transition []BucketLifecycleTransition `xml:"Transition,omitempty" header:"-"`
Expiration *BucketLifecycleExpiration `xml:"Expiration,omitempty" header:"-"`
AbortIncompleteMultipartUpload *BucketLifecycleAbortIncompleteMultipartUpload `xml:"AbortIncompleteMultipartUpload,omitempty" header:"-"`
NoncurrentVersionTransition []BucketLifecycleNoncurrentVersion `xml:"NoncurrentVersionTransition,omitempty" header:"-"`
NoncurrentVersionExpiration *BucketLifecycleNoncurrentVersion `xml:"NoncurrentVersionExpiration,omitempty" header:"-"`
}

// BucketGetLifecycleResult is the result of BucketGetLifecycle
type BucketGetLifecycleResult struct {
XMLName xml.Name `xml:"LifecycleConfiguration"`
Rules []BucketLifecycleRule `xml:"Rule,omitempty"`
XMLName xml.Name `xml:"LifecycleConfiguration" header:"-"`
Rules []BucketLifecycleRule `xml:"Rule,omitempty" header:"-"`
}

type BucketGetLifecycleOptions struct {
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
}

// GetLifecycle 请求实现读取生命周期管理的配置。当配置不存在时,返回404 Not Found。
// https://www.qcloud.com/document/product/436/8278
func (s *BucketService) GetLifecycle(ctx context.Context) (*BucketGetLifecycleResult, *Response, error) {
func (s *BucketService) GetLifecycle(ctx context.Context, opt ...*BucketGetLifecycleOptions) (*BucketGetLifecycleResult, *Response, error) {
var optHeader *BucketGetLifecycleOptions
if len(opt) > 0 {
optHeader = opt[0]
}
var res BucketGetLifecycleResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?lifecycle",
method: http.MethodGet,
result: &res,
baseURL: s.client.BaseURL.BucketURL,
uri: "/?lifecycle",
method: http.MethodGet,
optHeader: optHeader,
result: &res,
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return &res, resp, err
}

// BucketPutLifecycleOptions is the option of PutBucketLifecycle
type BucketPutLifecycleOptions struct {
XMLName xml.Name `xml:"LifecycleConfiguration"`
Rules []BucketLifecycleRule `xml:"Rule,omitempty"`
XMLName xml.Name `xml:"LifecycleConfiguration" header:"-"`
Rules []BucketLifecycleRule `xml:"Rule,omitempty" header:"-"`
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
}

// PutLifecycle 请求实现设置生命周期管理的功能。您可以通过该请求实现数据的生命周期管理配置和定期删除。
// 此请求为覆盖操作,上传新的配置文件将覆盖之前的配置文件。生命周期管理对文件和文件夹同时生效。
// https://www.qcloud.com/document/product/436/8280
func (s *BucketService) PutLifecycle(ctx context.Context, opt *BucketPutLifecycleOptions) (*Response, error) {
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?lifecycle",
method: http.MethodPut,
body: opt,
baseURL: s.client.BaseURL.BucketURL,
uri: "/?lifecycle",
method: http.MethodPut,
optHeader: opt,
body: opt,
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return resp, err
}

type BucketDeleteLifecycleOptions struct {
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
}

// DeleteLifecycle 请求实现删除生命周期管理。
// https://www.qcloud.com/document/product/436/8284
func (s *BucketService) DeleteLifecycle(ctx context.Context) (*Response, error) {
func (s *BucketService) DeleteLifecycle(ctx context.Context, opt ...*BucketDeleteLifecycleOptions) (*Response, error) {
var optHeader *BucketDeleteLifecycleOptions
if len(opt) > 0 {
optHeader = opt[0]
}
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?lifecycle",
method: http.MethodDelete,
baseURL: s.client.BaseURL.BucketURL,
uri: "/?lifecycle",
optHeader: optHeader,
method: http.MethodDelete,
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return resp, err
Expand Down
72 changes: 45 additions & 27 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -1445,8 +1445,9 @@ func (s *ObjectService) Download(ctx context.Context, name string, filepath stri
}

type ObjectPutTaggingOptions struct {
XMLName xml.Name `xml:"Tagging"`
TagSet []ObjectTaggingTag `xml:"TagSet>Tag,omitempty"`
XMLName xml.Name `xml:"Tagging" header:"-"`
TagSet []ObjectTaggingTag `xml:"TagSet>Tag,omitempty" header:"-"`
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
}
type ObjectTaggingTag BucketTaggingTag
type ObjectGetTaggingResult ObjectPutTaggingOptions
Expand All @@ -1461,53 +1462,70 @@ func (s *ObjectService) PutTagging(ctx context.Context, name string, opt *Object
return nil, errors.New("wrong params")
}
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: u,
method: http.MethodPut,
body: opt,
baseURL: s.client.BaseURL.BucketURL,
uri: u,
method: http.MethodPut,
body: opt,
optHeader: opt,
}
resp, err := s.client.doRetry(ctx, sendOpt)
return resp, err
}

func (s *ObjectService) GetTagging(ctx context.Context, name string, id ...string) (*ObjectGetTaggingResult, *Response, error) {
var u string
if len(id) == 1 {
u = fmt.Sprintf("/%s?tagging&versionId=%s", encodeURIComponent(name), id[0])
} else if len(id) == 0 {
u = fmt.Sprintf("/%s?tagging", encodeURIComponent(name))
} else {
type ObjectGetTaggingOptions struct {
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
}

func (s *ObjectService) GetTagging(ctx context.Context, name string, opt ...interface{}) (*ObjectGetTaggingResult, *Response, error) {
var optHeader *ObjectGetTaggingOptions
u := fmt.Sprintf("/%s?tagging", encodeURIComponent(name))
if len(opt) > 2 {
return nil, nil, errors.New("wrong params")
}
for _, val := range opt {
if v, ok := val.(string); ok {
u = fmt.Sprintf("%s&versionId=%s", u, v)
}
if v, ok := val.(*ObjectGetTaggingOptions); ok {
optHeader = v
}
}

var res ObjectGetTaggingResult
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: u,
method: http.MethodGet,
result: &res,
baseURL: s.client.BaseURL.BucketURL,
uri: u,
method: http.MethodGet,
optHeader: optHeader,
result: &res,
}
resp, err := s.client.doRetry(ctx, sendOpt)
return &res, resp, err
}

func (s *ObjectService) DeleteTagging(ctx context.Context, name string, id ...string) (*Response, error) {
func (s *ObjectService) DeleteTagging(ctx context.Context, name string, opt ...interface{}) (*Response, error) {
if len(name) == 0 || name == "/" {
return nil, errors.New("empty object name")
}
var u string
if len(id) == 1 {
u = fmt.Sprintf("/%s?tagging&versionId=%s", encodeURIComponent(name), id[0])
} else if len(id) == 0 {
u = fmt.Sprintf("/%s?tagging", encodeURIComponent(name))
} else {
var optHeader *ObjectGetTaggingOptions
u := fmt.Sprintf("/%s?tagging", encodeURIComponent(name))
if len(opt) > 2 {
return nil, errors.New("wrong params")
}
for _, val := range opt {
if v, ok := val.(string); ok {
u = fmt.Sprintf("%s&versionId=%s", u, v)
}
if v, ok := val.(*ObjectGetTaggingOptions); ok {
optHeader = v
}
}

sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: u,
method: http.MethodDelete,
baseURL: s.client.BaseURL.BucketURL,
uri: u,
method: http.MethodDelete,
optHeader: optHeader,
}
resp, err := s.client.doRetry(ctx, sendOpt)
return resp, err
Expand Down

0 comments on commit 8acf953

Please sign in to comment.