Skip to content

Commit

Permalink
add ci post
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Sep 29, 2020
1 parent e6f823c commit dd7c41c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
50 changes: 50 additions & 0 deletions ci.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cos

import (
"context"
"encoding/json"
"encoding/xml"
"net/http"
)

type PicOperations struct {
Expand All @@ -22,3 +25,50 @@ func EncodePicOperations(pic *PicOperations) string {
}
return string(bs)
}

type CloudImageReuslt struct {
XMLName xml.Name `xml:"UploadResult"`
OriginalInfo *PicOriginalInfo `xml:"OriginalInfo,omitempty"`
ProcessObject *PicProcessObject `xml:"ProcessResults>Object,omitempty"`
}

type PicOriginalInfo struct {
Key string `xml:"Key,omitempty"`
Location string `xml:"Location,omitempty"`
ImageInfo *PicImageInfo `xml:"ImageInfo,omitempty"`
}

type PicImageInfo struct {
Format string `xml:"Format,omitempty"`
Width int `xml:"Width,omitempty"`
Height int `xml:"Height,omitempty"`
Size int `xml:"Size,omitempty"`
Quality int `xml:"Quality,omitempty"`
}

type PicProcessObject struct {
Key string `xml:"Key,omitempty"`
Location string `xml:"Location,omitempty"`
Format string `xml:"Format,omitempty"`
Width int `xml:"Width,omitempty"`
Height int `xml:"Height,omitempty"`
Size int `xml:"Size,omitempty"`
Quality int `xml:"Quality,omitempty"`
}

type CloudImageOptions struct {
PicOperations string `header:"Pic-Operations" xml:"-" url:"-"`
}

func (s *ObjectService) PostCI(ctx context.Context, name string, opt *CloudImageOptions) (*CloudImageReuslt, *Response, error) {
var res CloudImageReuslt
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/" + encodeURIComponent(name) + "?image_process",
method: http.MethodPost,
optHeader: opt,
result: &res,
}
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}
68 changes: 68 additions & 0 deletions example/object/ci_post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"context"
"encoding/xml"
"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,
},
},
})

pic := &cos.PicOperations{
IsPicInfo: 1,
Rules: []cos.PicOperationsRules{
{
FileId: "format.jpg",
Rule: "imageView2/format/png",
},
},
}
opt := &cos.CloudImageOptions{
PicOperations: cos.EncodePicOperations(pic),
}
name := "test.jpg"
res, _, err := c.Object.PostCI(context.Background(), name, opt)
data, _ := xml.Marshal(res)
fmt.Printf("%+v\n", string(data))
log_status(err)
}

0 comments on commit dd7c41c

Please sign in to comment.