Skip to content

Commit 6bc90a9

Browse files
authored
Merge pull request #73 from agin719/fixup
update ci and error
2 parents 5eb2ce1 + e7237f4 commit 6bc90a9

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

ci.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cos
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
type PicOperations struct {
8+
IsPicInfo int `json:"is_pic_info,omitempty"`
9+
Rules []PicOperationsRules `json:"rules,omitemtpy"`
10+
}
11+
12+
type PicOperationsRules struct {
13+
Bucket string `json:"bucket,omitempty"`
14+
FileId string `json:"fileid"`
15+
Rule string `json:"rule"`
16+
}
17+
18+
func EncodePicOperations(pic *PicOperations) string {
19+
bs, err := json.Marshal(pic)
20+
if err != nil {
21+
return ""
22+
}
23+
return string(bs)
24+
}

error.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ErrorResponse struct {
1616
Code string
1717
Message string
1818
Resource string
19-
RequestID string `header:"x-cos-request-id,omitempty" url:"-" xml:"-"`
19+
RequestID string `header:"x-cos-request-id,omitempty" url:"-" xml:"RequestId,omitempty"`
2020
TraceID string `xml:"TraceId,omitempty"`
2121
}
2222

@@ -48,16 +48,24 @@ func checkResponse(r *http.Response) error {
4848
return errorResponse
4949
}
5050

51-
func IsNoSuchKeyError(e error) bool {
51+
func IsNotFoundError(e error) bool {
5252
if e == nil {
5353
return false
5454
}
5555
err, ok := e.(*ErrorResponse)
5656
if !ok {
5757
return false
5858
}
59-
if err.Response != nil && err.Response.StatusCode == 404 && err.Code == "NoSuchKey" {
59+
if err.Response != nil && err.Response.StatusCode == 404 {
6060
return true
6161
}
6262
return false
6363
}
64+
65+
func IsCOSError(e error) (*ErrorResponse, bool) {
66+
if e == nil {
67+
return nil, false
68+
}
69+
err, ok := e.(*ErrorResponse)
70+
return err, ok
71+
}

example/object/ci_put.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
10+
"github.com/tencentyun/cos-go-sdk-v5"
11+
"github.com/tencentyun/cos-go-sdk-v5/debug"
12+
)
13+
14+
func log_status(err error) {
15+
if err == nil {
16+
return
17+
}
18+
if cos.IsNotFoundError(err) {
19+
// WARN
20+
fmt.Println("Resource is not existed")
21+
} else if e, ok := cos.IsCOSError(err); ok {
22+
fmt.Printf("Code: %v\n", e.Code)
23+
fmt.Printf("Message: %v\n", e.Message)
24+
fmt.Printf("Resource: %v\n", e.Resource)
25+
fmt.Printf("RequestId: %v\n", e.RequestID)
26+
// ERROR
27+
} else {
28+
fmt.Println(err)
29+
// ERROR
30+
}
31+
}
32+
33+
func main() {
34+
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35+
b := &cos.BaseURL{BucketURL: u}
36+
c := cos.NewClient(b, &http.Client{
37+
Transport: &cos.AuthorizationTransport{
38+
SecretID: os.Getenv("COS_SECRETID"),
39+
SecretKey: os.Getenv("COS_SECRETKEY"),
40+
Transport: &debug.DebugRequestTransport{
41+
RequestHeader: true,
42+
// Notice when put a large file and set need the request body, might happend out of memory error.
43+
RequestBody: false,
44+
ResponseHeader: true,
45+
ResponseBody: true,
46+
},
47+
},
48+
})
49+
50+
opt := &cos.ObjectPutOptions{
51+
nil,
52+
&cos.ObjectPutHeaderOptions{
53+
XOptionHeader: &http.Header{},
54+
},
55+
}
56+
pic := &cos.PicOperations{
57+
IsPicInfo: 1,
58+
Rules: []cos.PicOperationsRules{
59+
{
60+
FileId: "format.jpg",
61+
Rule: "imageView2/format/png",
62+
},
63+
},
64+
}
65+
opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic))
66+
name := "test.jpg"
67+
local_filename := "./test.jpg"
68+
_, err := c.Object.PutFromFile(context.Background(), name, local_filename, opt)
69+
log_status(err)
70+
}

0 commit comments

Comments
 (0)