24
ci.go
Normal file
24
ci.go
Normal file
@@ -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
error.go
14
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
|
||||
}
|
||||
|
||||
70
example/object/ci_put.go
Normal file
70
example/object/ci_put.go
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user