Skip to content

Commit

Permalink
Merge pull request #280 from tencentyun/feature_jojoliang_2e155105
Browse files Browse the repository at this point in the history
Feature jojoliang 2e155105
  • Loading branch information
agin719 authored Sep 12, 2024
2 parents 17ebcc9 + 74f414d commit 2baec15
Show file tree
Hide file tree
Showing 12 changed files with 1,100 additions and 24 deletions.
152 changes: 152 additions & 0 deletions bucket.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cos

import (
"bytes"
"context"
"encoding/xml"
"errors"
"net/http"
)

Expand Down Expand Up @@ -212,3 +214,153 @@ func (s *BucketService) GetObjectVersions(ctx context.Context, opt *BucketGetObj
resp, err := s.client.doRetry(ctx, &sendOpt)
return &res, resp, err
}

type BucketGetMetadataResult struct {
BucketUrl string
BucketName string
Location string
MAZ bool
OFS bool
Encryption *BucketGetEncryptionResult
ACL *BucketGetACLResult
Website *BucketGetWebsiteResult
Logging *BucketGetLoggingResult
CORS *BucketGetCORSResult
Versioning *BucketGetVersionResult
Lifecycle *BucketGetLifecycleResult
IntelligentTiering *ListIntelligentTieringConfigurations
Tagging *BucketGetTaggingResult
ObjectLock *BucketGetObjectLockResult
Replication *BucketGetReplicationResult
}

func (s *BucketService) GetMeta(ctx context.Context, bucket ...string) (*BucketGetMetadataResult, *Response, error) {
if s.client.BaseURL.BucketURL == nil {
return nil, nil, errors.New("BucketURL is empty")
}
var customDomain bool
if !hostPrefix.MatchString(s.client.BaseURL.BucketURL.String()) {
customDomain = true
if len(bucket) == 0 || !bucketChecker.MatchString(bucket[0]) {
return nil, nil, errors.New("you must provide bucket-appid param in using custom domain")
}
}
var err error
var resp *Response
var res BucketGetMetadataResult

resp, err = s.Head(ctx)
if err != nil {
return nil, resp, err
}
// 非自定义域名
if !customDomain {
res.BucketName, res.Location = GetBucketRegionFromUrl(s.client.BaseURL.BucketURL)
} else {
res.BucketName, res.Location = bucket[0], resp.Header.Get("X-Cos-Bucket-Region")
}
w := bytes.NewBuffer(nil)
bucketURLTemplate.Execute(w, struct {
Schema string
BucketName string
Region string
}{"https", res.BucketName, res.Location})
res.BucketUrl = w.String()

if resp.Header.Get("X-Cos-Bucket-Az-Type") == "MAZ" {
res.MAZ = true
}
if resp.Header.Get("X-Cos-Bucket-Arch") == "OFS" {
res.OFS = true
}

res.Encryption, resp, err = s.GetEncryption(ctx)
if err != nil {
if IsNotFoundError(err) {
res.Encryption = nil
} else {
return nil, resp, err
}
}
res.ACL, resp, err = s.GetACL(ctx)
if err != nil {
if IsNotFoundError(err) {
res.ACL = nil
} else {
return nil, resp, err
}
}
res.Website, resp, err = s.GetWebsite(ctx)
if err != nil {
if IsNotFoundError(err) {
res.Website = nil
} else {
return nil, resp, err
}
}
res.Logging, resp, err = s.GetLogging(ctx)
if err != nil {
if IsNotFoundError(err) {
res.Logging = nil
} else {
return nil, resp, err
}
}
res.CORS, resp, err = s.GetCORS(ctx)
if err != nil {
if IsNotFoundError(err) {
res.CORS = nil
} else {
return nil, resp, err
}
}
res.Versioning, resp, err = s.GetVersioning(ctx)
if err != nil {
if IsNotFoundError(err) {
res.Versioning = nil
} else {
return nil, resp, err
}
}
res.Lifecycle, resp, err = s.GetLifecycle(ctx)
if err != nil {
if IsNotFoundError(err) {
res.Lifecycle = nil
} else {
return nil, resp, err
}
}
res.IntelligentTiering, resp, err = s.ListIntelligentTiering(ctx)
if err != nil {
if IsNotFoundError(err) {
res.IntelligentTiering = nil
} else {
return nil, resp, err
}
}
res.Tagging, resp, err = s.GetTagging(ctx)
if err != nil {
if IsNotFoundError(err) {
res.Tagging = nil
} else {
return nil, resp, err
}
}
res.ObjectLock, resp, err = s.GetObjectLockConfiguration(ctx)
if err != nil {
if IsNotFoundError(err) {
res.ObjectLock = nil
} else {
return nil, resp, err
}
}
res.Replication, resp, err = s.GetBucketReplication(ctx)
if err != nil {
if IsNotFoundError(err) {
res.Replication = nil
} else {
return nil, resp, err
}
}
return &res, resp, nil
}
96 changes: 90 additions & 6 deletions bucket_intelligenttiering.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,38 @@ package cos
import (
"context"
"encoding/xml"
"errors"
"net/http"
)

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

type BucketIntelligentTieringFilter struct {
And *BucketIntelligentTieringFilterAnd `xml:"And,omitempty" header:"-"`
Prefix string `xml:"Prefix,omitempty" header:"-"`
Tag []*BucketTaggingTag `xml:"Tag,omitempty" header:"-"`
}

type BucketIntelligentTieringTransition struct {
Days int `xml:"Days,omitempty" header:"-"`
RequestFrequent int `xml:"RequestFrequent,omitempty" header:"-"`
AccessTier string `xml:"AccessTier,omitempty" header:"-"`
Days int `xml:"Days,omitempty" header:"-"`
RequestFrequent int `xml:"RequestFrequent,omitempty" header:"-"`
}

type BucketPutIntelligentTieringOptions struct {
XMLName xml.Name `xml:"IntelligentTieringConfiguration" header:"-"`
Status string `xml:"Status,omitempty" header:"-"`
Transition *BucketIntelligentTieringTransition `xml:"Transition,omitempty" header:"-"`
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
XMLName xml.Name `xml:"IntelligentTieringConfiguration" header:"-"`
Status string `xml:"Status,omitempty" header:"-"`
Transition *BucketIntelligentTieringTransition `xml:"Transition,omitempty" header:"-"`

// V2
Id string `xml:"Id,omitempty" header:"-"`
Tiering []*BucketIntelligentTieringTransition `xml:"Tiering,omitempty" header:"-"`
Filter *BucketIntelligentTieringFilter `xml:"Filter,omitempty" header:"-"`

XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
}

type BucketGetIntelligentTieringResult BucketPutIntelligentTieringOptions
Expand All @@ -24,6 +43,18 @@ type BucketGetIntelligentTieringOptions struct {
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
}

type IntelligentTieringConfiguration struct {
Id string `xml:"Id"`
Status string `xml:"Status"`
Tiering []*BucketIntelligentTieringTransition `xml:"Tiering"`
Filter *BucketIntelligentTieringFilter `xml:"Filter,omitempty"`
}

type ListIntelligentTieringConfigurations struct {
XMLName xml.Name `xml:"ListBucketIntelligentTieringConfigurationsOutput"`
Configurations []*IntelligentTieringConfiguration `xml:"IntelligentTieringConfiguration,omitempty"`
}

func (s *BucketService) PutIntelligentTiering(ctx context.Context, opt *BucketPutIntelligentTieringOptions) (*Response, error) {
if opt != nil && opt.Transition != nil {
opt.Transition.RequestFrequent = 1
Expand Down Expand Up @@ -54,5 +85,58 @@ func (s *BucketService) GetIntelligentTiering(ctx context.Context, opt ...*Bucke
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return &res, resp, err
}

func (s *BucketService) PutIntelligentTieringV2(ctx context.Context, opt *BucketPutIntelligentTieringOptions) (*Response, error) {
if opt == nil || opt.Id == "" {
return nil, errors.New("id is empty")
}
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?intelligent-tiering&id=" + opt.Id,
method: http.MethodPut,
optHeader: opt,
body: opt,
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return resp, err
}

func (s *BucketService) GetIntelligentTieringV2(ctx context.Context, id string, opt ...*BucketGetIntelligentTieringOptions) (*BucketGetIntelligentTieringResult, *Response, error) {
var optHeader *BucketGetIntelligentTieringOptions
if len(opt) > 0 {
optHeader = opt[0]
}
var res BucketGetIntelligentTieringResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?intelligent-tiering&id=" + id,
method: http.MethodGet,
optHeader: optHeader,
result: &res,
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return &res, resp, err
}

func (s *BucketService) ListIntelligentTiering(ctx context.Context) (*ListIntelligentTieringConfigurations, *Response, error) {
var res ListIntelligentTieringConfigurations
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?intelligent-tiering",
method: http.MethodGet,
result: &res,
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return &res, resp, err
}

func (s *BucketService) DeleteIntelligentTiering(ctx context.Context, id string) (*Response, error) {
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?intelligent-tiering&id=" + id,
method: http.MethodDelete,
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return resp, err
}
Loading

0 comments on commit 2baec15

Please sign in to comment.