From 62fb57e373a9b86dafbd356820c0e96b20da9ba2 Mon Sep 17 00:00:00 2001 From: jojoliang Date: Fri, 5 Mar 2021 20:24:56 +0800 Subject: [PATCH 1/2] update ci put & select --- ci.go | 40 +++++++++++++++++++++++ example/CI/ci_image_process.go | 63 ++++++++++++++++++++++++++++++++++++ example/CI/ci_put.go | 73 ++++++++++++++++++++++++++++++++++++++++++ object_select.go | 2 +- 4 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 example/CI/ci_image_process.go create mode 100644 example/CI/ci_put.go diff --git a/ci.go b/ci.go index f69dd41..75ffdf4 100644 --- a/ci.go +++ b/ci.go @@ -4,7 +4,9 @@ import ( "context" "encoding/json" "encoding/xml" + "io" "net/http" + "os" ) type CIService service @@ -201,3 +203,41 @@ func (s *CIService) GetVideoAuditingJob(ctx context.Context, jobid string) (*Get resp, err := s.client.send(ctx, &sendOpt) return &res, resp, err } + +// ci put https://cloud.tencent.com/document/product/460/18147 +func (s *CIService) Put(ctx context.Context, name string, r io.Reader, opt *ObjectPutOptions) (*ImageProcessResult, *Response, error) { + if err := CheckReaderLen(r); err != nil { + return nil, nil, err + } + if opt != nil && opt.Listener != nil { + totalBytes, err := GetReaderLen(r) + if err != nil { + return nil, nil, err + } + r = TeeReader(r, nil, totalBytes, opt.Listener) + } + + var res ImageProcessResult + sendOpt := sendOptions{ + baseURL: s.client.BaseURL.BucketURL, + uri: "/" + encodeURIComponent(name), + method: http.MethodPut, + body: r, + optHeader: opt, + result: &res, + } + resp, err := s.client.send(ctx, &sendOpt) + + return &res, resp, err +} + +// ci put object from local file +func (s *CIService) PutFromFile(ctx context.Context, name string, filePath string, opt *ObjectPutOptions) (*ImageProcessResult, *Response, error) { + fd, err := os.Open(filePath) + if err != nil { + return nil, nil, err + } + defer fd.Close() + + return s.Put(ctx, name, fd, opt) +} diff --git a/example/CI/ci_image_process.go b/example/CI/ci_image_process.go new file mode 100644 index 0000000..6fc1584 --- /dev/null +++ b/example/CI/ci_image_process.go @@ -0,0 +1,63 @@ +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("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", 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.ImageProcessOptions{ + IsPicInfo: 1, + Rules: []cos.PicOperationsRules{ + { + FileId: "format.jpg", + Rule: "imageView2/format/png", + }, + }, + } + name := "test.jpg" + res, _, err := c.CI.ImageProcess(context.Background(), name, opt) + log_status(err) + fmt.Printf("%+v\n", res) +} diff --git a/example/CI/ci_put.go b/example/CI/ci_put.go new file mode 100644 index 0000000..314f48a --- /dev/null +++ b/example/CI/ci_put.go @@ -0,0 +1,73 @@ +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("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + +func main() { + u, _ := url.Parse("https://jojoliang-batch-1253960454.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" + res, _, err := c.CI.PutFromFile(context.Background(), name, local_filename, opt) + log_status(err) + fmt.Printf("%+v\n", res) + fmt.Printf("%+v\n", res.OriginalInfo) + fmt.Printf("%+v\n", res.ProcessResults) +} diff --git a/object_select.go b/object_select.go index a8d941c..a3c3c6d 100644 --- a/object_select.go +++ b/object_select.go @@ -39,7 +39,7 @@ type JSONOutputSerialization struct { } type CSVOutputSerialization struct { - QuoteFileds string `xml:"QuoteFileds,omitempty"` + QuoteFields string `xml:"QuoteFields,omitempty"` RecordDelimiter string `xml:"RecordDelimiter,omitempty"` FieldDelimiter string `xml:"FieldDelimiter,omitempty"` QuoteCharacter string `xml:"QuoteCharacter,omitempty"` From 685c58af29276c23ff69aee377b03ed31c7eb856 Mon Sep 17 00:00:00 2001 From: jojoliang Date: Fri, 5 Mar 2021 20:29:21 +0800 Subject: [PATCH 2/2] update --- example/CI/ci_put.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/CI/ci_put.go b/example/CI/ci_put.go index 314f48a..52f1d96 100644 --- a/example/CI/ci_put.go +++ b/example/CI/ci_put.go @@ -31,7 +31,7 @@ func log_status(err error) { } func main() { - u, _ := url.Parse("https://jojoliang-batch-1253960454.cos.ap-guangzhou.myqcloud.com") + 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{