add ci post
This commit is contained in:
50
ci.go
50
ci.go
@@ -1,7 +1,10 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PicOperations struct {
|
||||
@@ -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
example/object/ci_post.go
Normal file
68
example/object/ci_post.go
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user