|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/tencentyun/cos-go-sdk-v5" |
| 11 | + "github.com/tencentyun/cos-go-sdk-v5/debug" |
| 12 | +) |
| 13 | + |
| 14 | +func log_status(err error) { |
| 15 | + if err == nil { |
| 16 | + return |
| 17 | + } |
| 18 | + if cos.IsNotFoundError(err) { |
| 19 | + // WARN |
| 20 | + fmt.Println("Resource is not existed") |
| 21 | + } else if e, ok := cos.IsCOSError(err); ok { |
| 22 | + fmt.Printf("Code: %v\n", e.Code) |
| 23 | + fmt.Printf("Message: %v\n", e.Message) |
| 24 | + fmt.Printf("Resource: %v\n", e.Resource) |
| 25 | + fmt.Printf("RequestId: %v\n", e.RequestID) |
| 26 | + // ERROR |
| 27 | + } else { |
| 28 | + fmt.Println(err) |
| 29 | + // ERROR |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func main() { |
| 34 | + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") |
| 35 | + b := &cos.BaseURL{BucketURL: u} |
| 36 | + c := cos.NewClient(b, &http.Client{ |
| 37 | + Transport: &cos.AuthorizationTransport{ |
| 38 | + SecretID: os.Getenv("COS_SECRETID"), |
| 39 | + SecretKey: os.Getenv("COS_SECRETKEY"), |
| 40 | + Transport: &debug.DebugRequestTransport{ |
| 41 | + RequestHeader: true, |
| 42 | + // Notice when put a large file and set need the request body, might happend out of memory error. |
| 43 | + RequestBody: false, |
| 44 | + ResponseHeader: true, |
| 45 | + ResponseBody: true, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }) |
| 49 | + |
| 50 | + opt := &cos.ObjectPutOptions{ |
| 51 | + nil, |
| 52 | + &cos.ObjectPutHeaderOptions{ |
| 53 | + XOptionHeader: &http.Header{}, |
| 54 | + }, |
| 55 | + } |
| 56 | + pic := &cos.PicOperations{ |
| 57 | + IsPicInfo: 1, |
| 58 | + Rules: []cos.PicOperationsRules{ |
| 59 | + { |
| 60 | + FileId: "format.jpg", |
| 61 | + Rule: "imageView2/format/png", |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic)) |
| 66 | + name := "test.jpg" |
| 67 | + local_filename := "./test.jpg" |
| 68 | + _, err := c.Object.PutFromFile(context.Background(), name, local_filename, opt) |
| 69 | + log_status(err) |
| 70 | +} |
0 commit comments