Skip to content

Commit

Permalink
Merge pull request #73 from agin719/fixup
Browse files Browse the repository at this point in the history
update ci and error
  • Loading branch information
agin719 authored Apr 2, 2020
2 parents 5eb2ce1 + e7237f4 commit 6bc90a9
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
24 changes: 24 additions & 0 deletions ci.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 11 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -48,16 +48,24 @@ func checkResponse(r *http.Response) error {
return errorResponse
}

func IsNoSuchKeyError(e error) bool {
func IsNotFoundError(e error) bool {
if e == nil {
return false
}
err, ok := e.(*ErrorResponse)
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
}
70 changes: 70 additions & 0 deletions example/object/ci_put.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 6bc90a9

Please sign in to comment.