You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.2 KiB

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