From e7237f4b030d117a4868abf2eb8cff3c37dd4aba Mon Sep 17 00:00:00 2001 From: jojoliang Date: Thu, 2 Apr 2020 20:24:59 +0800 Subject: [PATCH] update ci and error --- ci.go | 24 ++++++++++++++ error.go | 14 ++++++-- example/object/ci_put.go | 70 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 ci.go create mode 100644 example/object/ci_put.go diff --git a/ci.go b/ci.go new file mode 100644 index 0000000..2cf1713 --- /dev/null +++ b/ci.go @@ -0,0 +1,24 @@ +package cos + +import ( + "encoding/json" +) + +type PicOperations struct { + IsPicInfo int `json:"is_pic_info,omitempty"` + Rules []PicOperationsRules `json:"rules,omitemtpy"` +} + +type PicOperationsRules struct { + Bucket string `json:"bucket,omitempty"` + FileId string `json:"fileid"` + Rule string `json:"rule"` +} + +func EncodePicOperations(pic *PicOperations) string { + bs, err := json.Marshal(pic) + if err != nil { + return "" + } + return string(bs) +} diff --git a/error.go b/error.go index ad60cc6..23741e7 100644 --- a/error.go +++ b/error.go @@ -16,7 +16,7 @@ type ErrorResponse struct { Code string Message string Resource string - RequestID string `header:"x-cos-request-id,omitempty" url:"-" xml:"-"` + RequestID string `header:"x-cos-request-id,omitempty" url:"-" xml:"RequestId,omitempty"` TraceID string `xml:"TraceId,omitempty"` } @@ -48,7 +48,7 @@ func checkResponse(r *http.Response) error { return errorResponse } -func IsNoSuchKeyError(e error) bool { +func IsNotFoundError(e error) bool { if e == nil { return false } @@ -56,8 +56,16 @@ func IsNoSuchKeyError(e error) bool { if !ok { return false } - if err.Response != nil && err.Response.StatusCode == 404 && err.Code == "NoSuchKey" { + if err.Response != nil && err.Response.StatusCode == 404 { return true } return false } + +func IsCOSError(e error) (*ErrorResponse, bool) { + if e == nil { + return nil, false + } + err, ok := e.(*ErrorResponse) + return err, ok +} diff --git a/example/object/ci_put.go b/example/object/ci_put.go new file mode 100644 index 0000000..de36848 --- /dev/null +++ b/example/object/ci_put.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "net/url" + "os" + + "github.com/tencentyun/cos-go-sdk-v5" + "github.com/tencentyun/cos-go-sdk-v5/debug" +) + +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("Code: %v\n", e.Code) + fmt.Printf("Message: %v\n", e.Message) + fmt.Printf("Resource: %v\n", e.Resource) + fmt.Printf("RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Println(err) + // ERROR + } +} + +func main() { + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") + b := &cos.BaseURL{BucketURL: u} + c := cos.NewClient(b, &http.Client{ + Transport: &cos.AuthorizationTransport{ + SecretID: os.Getenv("COS_SECRETID"), + SecretKey: os.Getenv("COS_SECRETKEY"), + Transport: &debug.DebugRequestTransport{ + RequestHeader: true, + // Notice when put a large file and set need the request body, might happend out of memory error. + RequestBody: false, + ResponseHeader: true, + ResponseBody: true, + }, + }, + }) + + opt := &cos.ObjectPutOptions{ + nil, + &cos.ObjectPutHeaderOptions{ + XOptionHeader: &http.Header{}, + }, + } + pic := &cos.PicOperations{ + IsPicInfo: 1, + Rules: []cos.PicOperationsRules{ + { + FileId: "format.jpg", + Rule: "imageView2/format/png", + }, + }, + } + opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic)) + name := "test.jpg" + local_filename := "./test.jpg" + _, err := c.Object.PutFromFile(context.Background(), name, local_filename, opt) + log_status(err) +}