update bucket lifecycle,presignedURL,error format
This commit is contained in:
@@ -6,9 +6,16 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type BucketLifecycleAndOperator struct {
|
||||
Prefix string `xml:"Prefix,omitempty"`
|
||||
Tag []BucketTaggingTag `xml:"Tag,omitempty"`
|
||||
}
|
||||
|
||||
// BucketLifecycleFilter is the param of BucketLifecycleRule
|
||||
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
|
||||
|
||||
@@ -23,7 +23,13 @@ func TestBucketService_GetLifecycle(t *testing.T) {
|
||||
<Rule>
|
||||
<ID>1234</ID>
|
||||
<Filter>
|
||||
<Prefix>test</Prefix>
|
||||
<And>
|
||||
<Prefix>test</Prefix>
|
||||
<Tag>
|
||||
<Key>key</Key>
|
||||
<Value>value</Value>
|
||||
</Tag>
|
||||
</And>
|
||||
</Filter>
|
||||
<Status>Enabled</Status>
|
||||
<Transition>
|
||||
@@ -53,8 +59,15 @@ func TestBucketService_GetLifecycle(t *testing.T) {
|
||||
XMLName: xml.Name{Local: "LifecycleConfiguration"},
|
||||
Rules: []BucketLifecycleRule{
|
||||
{
|
||||
ID: "1234",
|
||||
Filter: &BucketLifecycleFilter{Prefix: "test"},
|
||||
ID: "1234",
|
||||
Filter: &BucketLifecycleFilter{
|
||||
And: &BucketLifecycleAndOperator{
|
||||
Prefix: "test",
|
||||
Tag: []BucketTaggingTag{
|
||||
{Key: "key", Value: "value"},
|
||||
},
|
||||
},
|
||||
},
|
||||
Status: "Enabled",
|
||||
Transition: &BucketLifecycleTransition{Days: 10, StorageClass: "Standard"},
|
||||
},
|
||||
|
||||
6
error.go
6
error.go
@@ -30,8 +30,12 @@ func (r *ErrorResponse) Error() string {
|
||||
if TraceID == "" {
|
||||
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)",
|
||||
r.Response.Request.Method, r.Response.Request.URL,
|
||||
r.Response.Request.Method, decodeURL,
|
||||
r.Response.StatusCode, r.Code, r.Message, RequestID, TraceID)
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ func main() {
|
||||
name := "exampleobject"
|
||||
ctx := context.Background()
|
||||
|
||||
// 方法1 通过 tag 设置 x-cos-security-token
|
||||
// Get presigned
|
||||
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token)
|
||||
if err != nil {
|
||||
@@ -36,9 +37,32 @@ func main() {
|
||||
return
|
||||
}
|
||||
// Get object by presinged url
|
||||
_, err = http.Get(presignedURL.String())
|
||||
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)
|
||||
|
||||
// 方法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
|
||||
}
|
||||
|
||||
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
|
||||
func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt interface{}) (*url.URL, error) {
|
||||
sendOpt := sendOptions{
|
||||
@@ -115,6 +120,12 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
|
||||
optQuery: 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user