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.

99 lines
2.5 KiB

  1. package main
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "github.com/tencentyun/cos-go-sdk-v5"
  10. "github.com/tencentyun/cos-go-sdk-v5/debug"
  11. )
  12. func log_status(err error) {
  13. if err == nil {
  14. return
  15. }
  16. if cos.IsNotFoundError(err) {
  17. // WARN
  18. fmt.Println("WARN: Resource is not existed")
  19. } else if e, ok := cos.IsCOSError(err); ok {
  20. fmt.Printf("ERROR: Code: %v\n", e.Code)
  21. fmt.Printf("ERROR: Message: %v\n", e.Message)
  22. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  23. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  24. // ERROR
  25. } else {
  26. fmt.Printf("ERROR: %v\n", err)
  27. // ERROR
  28. }
  29. }
  30. func main() {
  31. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  32. b := &cos.BaseURL{BucketURL: u}
  33. c := cos.NewClient(b, &http.Client{
  34. Transport: &cos.AuthorizationTransport{
  35. SecretID: os.Getenv("COS_SECRETID"),
  36. SecretKey: os.Getenv("COS_SECRETKEY"),
  37. Transport: &debug.DebugRequestTransport{
  38. RequestHeader: true,
  39. // Notice when put a large file and set need the request body, might happend out of memory error.
  40. RequestBody: false,
  41. ResponseHeader: true,
  42. ResponseBody: false,
  43. },
  44. },
  45. })
  46. // 上传时添加盲水印
  47. opt := &cos.ObjectPutOptions{
  48. nil,
  49. &cos.ObjectPutHeaderOptions{
  50. XOptionHeader: &http.Header{},
  51. },
  52. }
  53. pic := &cos.PicOperations{
  54. IsPicInfo: 1,
  55. Rules: []cos.PicOperationsRules{
  56. {
  57. FileId: "format.jpg",
  58. Rule: "watermark/3/type/3/text/" + base64.StdEncoding.EncodeToString([]byte("testwatermark")),
  59. },
  60. },
  61. }
  62. opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic))
  63. name := "test.jpg"
  64. local_filename := "./test.jpg"
  65. res, _, err := c.CI.PutFromFile(context.Background(), name, local_filename, opt)
  66. log_status(err)
  67. fmt.Printf("%+v\n", res)
  68. // 下载时添加盲水印
  69. name = "test.jpg"
  70. filepath := "watermark.jpg"
  71. _, err = c.CI.GetToFile(context.Background(), name, filepath, "watermark/3/type/3/text/"+base64.StdEncoding.EncodeToString([]byte("testwatermark")), nil)
  72. // 提取盲水印
  73. opt = &cos.ObjectPutOptions{
  74. nil,
  75. &cos.ObjectPutHeaderOptions{
  76. XOptionHeader: &http.Header{},
  77. },
  78. }
  79. pic = &cos.PicOperations{
  80. IsPicInfo: 1,
  81. Rules: []cos.PicOperationsRules{
  82. {
  83. FileId: "format2.jpg",
  84. Rule: "watermark/4/type/3/text/" + base64.StdEncoding.EncodeToString([]byte("testwatermark")),
  85. },
  86. },
  87. }
  88. opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic))
  89. name = "test2.jpg"
  90. _, err = c.Object.PutFromFile(context.Background(), name, filepath, opt)
  91. log_status(err)
  92. }