Skip to content

Commit 70039d9

Browse files
author
jojoliang
committed
update lifecycle,policy,intelligent params, 增加重试标识,增加upload时自定义参数传递
1 parent bb60e1e commit 70039d9

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

bucket_lifecycle.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import (
77
)
88

99
type BucketLifecycleAndOperator struct {
10-
Prefix string `xml:"Prefix,omitempty" header:"-"`
11-
Tag []BucketTaggingTag `xml:"Tag,omitempty" header:"-"`
10+
Prefix string `xml:"Prefix,omitempty" header:"-"`
11+
Tag []BucketTaggingTag `xml:"Tag,omitempty" header:"-"`
12+
PrefixNotEquals string `xml:"PrefixNotEquals,omitempty" header:"-"`
13+
ObjectSizeGreaterThan int64 `xml:"ObjectSizeGreaterThan,omitempty" header:"-"`
14+
ObjectSizeLessThan int64 `xml:"ObjectSizeLessThan,omitempty" header:"-"`
1215
}
1316

1417
// BucketLifecycleFilter is the param of BucketLifecycleRule
@@ -25,16 +28,23 @@ type BucketLifecycleExpiration struct {
2528
ExpiredObjectDeleteMarker bool `xml:"ExpiredObjectDeleteMarker,omitempty" header:"-"`
2629
}
2730

31+
type BucketAccessFrequency struct {
32+
AccessCountLessThan int `xml:"AccessCountLessThan,omitempty" header:"-"`
33+
RecentDays int `xml:"RecentDays,omitempty" header:"-"`
34+
}
35+
2836
// BucketLifecycleTransition is the param of BucketLifecycleRule
2937
type BucketLifecycleTransition struct {
30-
Date string `xml:"Date,omitempty" header:"-"`
31-
Days int `xml:"Days,omitempty" header:"-"`
32-
StorageClass string `xml:"StorageClass,omitempty" header:"-"`
38+
Date string `xml:"Date,omitempty" header:"-"`
39+
Days int `xml:"Days,omitempty" header:"-"`
40+
StorageClass string `xml:"StorageClass,omitempty" header:"-"`
41+
AccessFrequency *BucketAccessFrequency `xml:"AccessFrequency,omitempty" header:"-"`
3342
}
3443

3544
type BucketLifecycleNoncurrentVersion struct {
36-
NoncurrentDays int `xml:"NoncurrentDays,omitempty" header:"-"`
37-
StorageClass string `xml:"StorageClass,omitempty" header:"-"`
45+
NoncurrentDays int `xml:"NoncurrentDays,omitempty" header:"-"`
46+
StorageClass string `xml:"StorageClass,omitempty" header:"-"`
47+
AccessFrequency *BucketAccessFrequency `xml:"AccessFrequency,omitempty" header:"-"`
3848
}
3949

4050
// BucketLifecycleAbortIncompleteMultipartUpload is the param of BucketLifecycleRule

bucket_policy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type BucketStatement struct {
1414
Effect string `json:"effect,omitempty"`
1515
Resource []string `json:"resource,omitempty"`
1616
Condition map[string]map[string]interface{} `json:"condition,omitempty"`
17+
Sid string `json:"sid,omitempty"`
1718
}
1819

1920
type BucketPutPolicyOptions struct {

cos.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
const (
2727
// Version current go sdk version
28-
Version = "0.7.50"
28+
Version = "0.7.52"
2929
UserAgent = "cos-go-sdk-v5/" + Version
3030
contentTypeXML = "application/xml"
3131
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
@@ -40,10 +40,10 @@ var (
4040
)
4141

4242
// {<http://>|<https://>}{bucketname-appid}.{cos|cos-internal|cos-website|ci}.{region}.{myqcloud.com/tencentcos.cn}{/}
43-
hostSuffix = regexp.MustCompile(`^.*((cos|cos-internal|cos-website|ci)\.[a-z-1]+|file)\.(myqcloud\.com|tencentcos\.cn).*$`)
44-
hostPrefix = regexp.MustCompile(`^(http://|https://){0,1}([a-z0-9-]+-[0-9]+\.){0,1}((cos|cos-internal|cos-website|ci)\.[a-z-1]+|file)\.(myqcloud\.com|tencentcos\.cn).*$`)
43+
hostSuffix = regexp.MustCompile(`^.*((cos|cos-internal|cos-website|ci)\.[a-z-1]+|file)\.(myqcloud\.com|tencentcos\.cn).*$`)
44+
hostPrefix = regexp.MustCompile(`^(http://|https://){0,1}([a-z0-9-]+-[0-9]+\.){0,1}((cos|cos-internal|cos-website|ci)\.[a-z-1]+|file)\.(myqcloud\.com|tencentcos\.cn).*$`)
4545
metaInsightHostPrefix = regexp.MustCompile(`^(http://|https://){0,1}([0-9]+\.){1}((cos|cos-internal|cos-website|ci)\.[a-z-1]+|file)\.(myqcloud\.com|tencentcos\.cn).*$`)
46-
invalidBucketErr = fmt.Errorf("invalid bucket format, please check your cos.BaseURL")
46+
invalidBucketErr = fmt.Errorf("invalid bucket format, please check your cos.BaseURL")
4747

4848
switchHost = regexp.MustCompile(`([a-z0-9-]+-[0-9]+\.)(cos\.[a-z-1]+)\.(myqcloud\.com)(:[0-9]+){0,1}$`)
4949
accelerateDomainSuffix = "accelerate.myqcloud.com"
@@ -121,11 +121,11 @@ type Client struct {
121121

122122
common service
123123

124-
Service *ServiceService
125-
Bucket *BucketService
126-
Object *ObjectService
127-
Batch *BatchService
128-
CI *CIService
124+
Service *ServiceService
125+
Bucket *BucketService
126+
Object *ObjectService
127+
Batch *BatchService
128+
CI *CIService
129129
MetaInsight *MetaInsightService
130130

131131
Conf *Config
@@ -217,7 +217,7 @@ func (c *Client) GetCredential() *Credential {
217217
return nil
218218
}
219219

220-
func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method string, body interface{}, optQuery interface{}, optHeader interface{}) (req *http.Request, err error) {
220+
func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method string, body interface{}, optQuery interface{}, optHeader interface{}, isRetry bool) (req *http.Request, err error) {
221221
if !checkURL(baseURL) {
222222
host := baseURL.String()
223223
if c.BaseURL.MetaInsightURL != baseURL || !metaInsightHostPrefix.MatchString(host) {
@@ -273,6 +273,9 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
273273
if req.Header.Get("Content-Type") == "" && contentType != "" {
274274
req.Header.Set("Content-Type", contentType)
275275
}
276+
if isRetry {
277+
req.Header.Set("X-Cos-Sdk-Retry", "true")
278+
}
276279
if c.Host != "" {
277280
req.Host = c.Host
278281
}
@@ -370,6 +373,8 @@ type sendOptions struct {
370373
// 是否禁用自动调用 resp.Body.Close()
371374
// 自动调用 Close() 是为了能够重用连接
372375
disableCloseBody bool
376+
// 是否重试
377+
isRetry bool
373378
}
374379

375380
func toSwitchHost(oldURL *url.URL) *url.URL {
@@ -428,6 +433,7 @@ func (c *Client) doRetry(ctx context.Context, opt *sendOptions) (resp *Response,
428433
if err != nil {
429434
retryErr.Add(err)
430435
}
436+
opt.isRetry = nr > 0
431437
resp, err = c.send(ctx, opt)
432438
opt.baseURL, retrieable = c.CheckRetrieable(opt.baseURL, resp, err, nr >= count-2)
433439
if retrieable {
@@ -449,7 +455,7 @@ func (c *Client) doRetry(ctx context.Context, opt *sendOptions) (resp *Response,
449455
}
450456

451457
func (c *Client) send(ctx context.Context, opt *sendOptions) (resp *Response, err error) {
452-
req, err := c.newRequest(ctx, opt.baseURL, opt.uri, opt.method, opt.body, opt.optQuery, opt.optHeader)
458+
req, err := c.newRequest(ctx, opt.baseURL, opt.uri, opt.method, opt.body, opt.optQuery, opt.optHeader, opt.isRetry)
453459
if err != nil {
454460
return
455461
}

object.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
161161
}
162162
}
163163
}
164-
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader)
164+
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
165165
if err != nil {
166166
return nil, err
167167
}
@@ -242,7 +242,7 @@ func (s *ObjectService) GetPresignedURL2(ctx context.Context, httpMethod, name s
242242
sendOpt.uri = fmt.Sprintf("%s%s%s", sendOpt.uri, mark, url.Values{"x-cos-security-token": []string{cred.SessionToken}}.Encode())
243243
}
244244

245-
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader)
245+
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
246246
if err != nil {
247247
return nil, err
248248
}
@@ -318,7 +318,7 @@ func (s *ObjectService) GetPresignedURL3(ctx context.Context, httpMethod, name s
318318
sendOpt.uri = fmt.Sprintf("%s%s%s", sendOpt.uri, mark, url.Values{"x-cos-security-token": []string{cred.SessionToken}}.Encode())
319319
}
320320

321-
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader)
321+
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
322322
if err != nil {
323323
return nil, err
324324
}
@@ -371,7 +371,7 @@ func (s *ObjectService) GetSignature(ctx context.Context, httpMethod, name, ak,
371371
sendOpt.uri = fmt.Sprintf("%s?%s", sendOpt.uri, qs)
372372
}
373373
}
374-
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader)
374+
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
375375
if err != nil {
376376
return ""
377377
}
@@ -771,7 +771,7 @@ type CASJobParameters struct {
771771
// ObjectRestoreOptions is the option of object restore
772772
type ObjectRestoreOptions struct {
773773
XMLName xml.Name `xml:"RestoreRequest" header:"-" url:"-"`
774-
Days int `xml:"Days" header:"-" url:"-"`
774+
Days int `xml:"Days,omitempty" header:"-" url:"-"`
775775
Tier *CASJobParameters `xml:"CASJobParameters" header:"-" url:"-"`
776776
XOptionHeader *http.Header `xml:"-" header:",omitempty" url:"-"`
777777
}
@@ -1368,6 +1368,7 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
13681368
partOpt.XCosSSECustomerKey = optini.XCosSSECustomerKey
13691369
partOpt.XCosSSECustomerKeyMD5 = optini.XCosSSECustomerKeyMD5
13701370
partOpt.XCosTrafficLimit = optini.XCosTrafficLimit
1371+
partOpt.XOptionHeader = optini.XOptionHeader
13711372
}
13721373
job := &Jobs{
13731374
Name: name,

0 commit comments

Comments
 (0)