update bucket lifecycle,presignedURL,error format
This commit is contained in:
@@ -6,9 +6,16 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type BucketLifecycleAndOperator struct {
|
||||||
|
Prefix string `xml:"Prefix,omitempty"`
|
||||||
|
Tag []BucketTaggingTag `xml:"Tag,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// BucketLifecycleFilter is the param of BucketLifecycleRule
|
// BucketLifecycleFilter is the param of BucketLifecycleRule
|
||||||
type BucketLifecycleFilter struct {
|
type BucketLifecycleFilter struct {
|
||||||
Prefix string `xml:"Prefix,omitempty"`
|
Prefix string `xml:"Prefix,omitempty"`
|
||||||
|
Tag *BucketTaggingTag `xml:"Tag,omitempty"`
|
||||||
|
And *BucketLifecycleAndOperator `xml:"And,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// BucketLifecycleExpiration is the param of BucketLifecycleRule
|
// BucketLifecycleExpiration is the param of BucketLifecycleRule
|
||||||
|
|||||||
@@ -23,7 +23,13 @@ func TestBucketService_GetLifecycle(t *testing.T) {
|
|||||||
<Rule>
|
<Rule>
|
||||||
<ID>1234</ID>
|
<ID>1234</ID>
|
||||||
<Filter>
|
<Filter>
|
||||||
<Prefix>test</Prefix>
|
<And>
|
||||||
|
<Prefix>test</Prefix>
|
||||||
|
<Tag>
|
||||||
|
<Key>key</Key>
|
||||||
|
<Value>value</Value>
|
||||||
|
</Tag>
|
||||||
|
</And>
|
||||||
</Filter>
|
</Filter>
|
||||||
<Status>Enabled</Status>
|
<Status>Enabled</Status>
|
||||||
<Transition>
|
<Transition>
|
||||||
@@ -53,8 +59,15 @@ func TestBucketService_GetLifecycle(t *testing.T) {
|
|||||||
XMLName: xml.Name{Local: "LifecycleConfiguration"},
|
XMLName: xml.Name{Local: "LifecycleConfiguration"},
|
||||||
Rules: []BucketLifecycleRule{
|
Rules: []BucketLifecycleRule{
|
||||||
{
|
{
|
||||||
ID: "1234",
|
ID: "1234",
|
||||||
Filter: &BucketLifecycleFilter{Prefix: "test"},
|
Filter: &BucketLifecycleFilter{
|
||||||
|
And: &BucketLifecycleAndOperator{
|
||||||
|
Prefix: "test",
|
||||||
|
Tag: []BucketTaggingTag{
|
||||||
|
{Key: "key", Value: "value"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
Status: "Enabled",
|
Status: "Enabled",
|
||||||
Transition: &BucketLifecycleTransition{Days: 10, StorageClass: "Standard"},
|
Transition: &BucketLifecycleTransition{Days: 10, StorageClass: "Standard"},
|
||||||
},
|
},
|
||||||
|
|||||||
6
error.go
6
error.go
@@ -30,8 +30,12 @@ func (r *ErrorResponse) Error() string {
|
|||||||
if TraceID == "" {
|
if TraceID == "" {
|
||||||
TraceID = r.Response.Header.Get("X-Cos-Trace-Id")
|
TraceID = r.Response.Header.Get("X-Cos-Trace-Id")
|
||||||
}
|
}
|
||||||
|
decodeURL, err := decodeURIComponent(r.Response.Request.URL.String())
|
||||||
|
if err != nil {
|
||||||
|
decodeURL = r.Response.Request.URL.String()
|
||||||
|
}
|
||||||
return fmt.Sprintf("%v %v: %d %v(Message: %v, RequestId: %v, TraceId: %v)",
|
return fmt.Sprintf("%v %v: %d %v(Message: %v, RequestId: %v, TraceId: %v)",
|
||||||
r.Response.Request.Method, r.Response.Request.URL,
|
r.Response.Request.Method, decodeURL,
|
||||||
r.Response.StatusCode, r.Code, r.Message, RequestID, TraceID)
|
r.Response.StatusCode, r.Code, r.Message, RequestID, TraceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ func main() {
|
|||||||
name := "exampleobject"
|
name := "exampleobject"
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 方法1 通过 tag 设置 x-cos-security-token
|
||||||
// Get presigned
|
// Get presigned
|
||||||
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token)
|
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -36,9 +37,32 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Get object by presinged url
|
// Get object by presinged url
|
||||||
_, err = http.Get(presignedURL.String())
|
resp, err := http.Get(presignedURL.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %v\n", err)
|
fmt.Printf("Error: %v\n", err)
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
fmt.Println(presignedURL.String())
|
fmt.Println(presignedURL.String())
|
||||||
|
fmt.Printf("resp:%v\n", resp)
|
||||||
|
|
||||||
|
// 方法2 通过 PresignedURLOptions 设置 x-cos-security-token
|
||||||
|
opt := &cos.PresignedURLOptions{
|
||||||
|
Query: &url.Values{},
|
||||||
|
Header: &http.Header{},
|
||||||
|
}
|
||||||
|
opt.Query.Add("x-cos-security-token", "<token>")
|
||||||
|
// Get presigned
|
||||||
|
presignedURL, err = c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, opt)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Get object by presinged url
|
||||||
|
resp, err = http.Get(presignedURL.String())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %v\n", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
fmt.Println(presignedURL.String())
|
||||||
|
fmt.Printf("resp:%v\n", resp)
|
||||||
}
|
}
|
||||||
|
|||||||
11
object.go
11
object.go
@@ -106,6 +106,11 @@ func (s *ObjectService) GetToFile(ctx context.Context, name, localpath string, o
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PresignedURLOptions struct {
|
||||||
|
Query *url.Values `xml:"-" url:"-" header:"-"`
|
||||||
|
Header *http.Header `header:"-,omitempty" url:"-" xml:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
// GetPresignedURL get the object presigned to down or upload file by url
|
// GetPresignedURL get the object presigned to down or upload file by url
|
||||||
func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt interface{}) (*url.URL, error) {
|
func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt interface{}) (*url.URL, error) {
|
||||||
sendOpt := sendOptions{
|
sendOpt := sendOptions{
|
||||||
@@ -115,6 +120,12 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
|
|||||||
optQuery: opt,
|
optQuery: opt,
|
||||||
optHeader: opt,
|
optHeader: opt,
|
||||||
}
|
}
|
||||||
|
if popt, ok := opt.(*PresignedURLOptions); ok {
|
||||||
|
qs := popt.Query.Encode()
|
||||||
|
if qs != "" {
|
||||||
|
sendOpt.uri = fmt.Sprintf("%s?%s", sendOpt.uri, qs)
|
||||||
|
}
|
||||||
|
}
|
||||||
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader)
|
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user