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.

132 lines
3.8 KiB

4 years ago
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  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. cu, _ := url.Parse("https://test-1259654469.ci.ap-guangzhou.myqcloud.com")
  33. b := &cos.BaseURL{BucketURL: u, CIURL: cu}
  34. c := cos.NewClient(b, &http.Client{
  35. Transport: &cos.AuthorizationTransport{
  36. SecretID: os.Getenv("COS_SECRETID"),
  37. SecretKey: os.Getenv("COS_SECRETKEY"),
  38. Transport: &debug.DebugRequestTransport{
  39. RequestHeader: true,
  40. // Notice when put a large file and set need the request body, might happend out of memory error.
  41. RequestBody: true,
  42. ResponseHeader: true,
  43. ResponseBody: true,
  44. },
  45. },
  46. })
  47. // 1、UpdateDocProcessQueue
  48. updateQueueOpt := &cos.UpdateDocProcessQueueOptions{
  49. Name: "queue-doc-process-1",
  50. QueueID: "p111a8dd208104ce3b11c78398f658ca8",
  51. State: "Active",
  52. NotifyConfig: &cos.DocProcessQueueNotifyConfig{
  53. State: "Off",
  54. },
  55. }
  56. updateQueueRes, _, err := c.CI.UpdateDocProcessQueue(context.Background(), updateQueueOpt)
  57. log_status(err)
  58. fmt.Printf("%+v\n", updateQueueRes)
  59. // 2、DescribeDocProcessQueues
  60. DescribeQueueOpt := &cos.DescribeDocProcessQueuesOptions{
  61. QueueIds: "p111a8dd208104ce3b11c78398f658ca8,p4318f85d2aa14c43b1dba6f9b78be9b3,aacb2bb066e9c4478834d4196e76c49d3",
  62. PageNumber: 1,
  63. PageSize: 2,
  64. }
  65. DescribeQueueRes, _, err := c.CI.DescribeDocProcessQueues(context.Background(), DescribeQueueOpt)
  66. log_status(err)
  67. fmt.Printf("%+v\n", DescribeQueueRes)
  68. // 3、DescribeDocProcessBuckets
  69. BucketsOpt := &cos.DescribeDocProcessBucketsOptions{
  70. Regions: "All",
  71. }
  72. BucketsRes, _, err := c.CI.DescribeDocProcessBuckets(context.Background(), BucketsOpt)
  73. log_status(err)
  74. fmt.Printf("%+v\n", BucketsRes)
  75. // 4、CreateDocProcessJobs
  76. createJobOpt := &cos.CreateDocProcessJobsOptions{
  77. Tag: "DocProcess",
  78. Input: &cos.DocProcessJobInput{
  79. Object: "form.pdf",
  80. },
  81. Operation: &cos.DocProcessJobOperation{
  82. Output: &cos.DocProcessJobOutput{
  83. Region: "ap-guangzhou",
  84. Object: "test-doc${Number}",
  85. Bucket: "test-1259654469",
  86. },
  87. DocProcess: &cos.DocProcessJobDocProcess{
  88. TgtType: "png",
  89. StartPage: 1,
  90. EndPage: -1,
  91. ImageParams: "watermark/1/image/aHR0cDovL3Rlc3QwMDUtMTI1MTcwNDcwOC5jb3MuYXAtY2hvbmdxaW5nLm15cWNsb3VkLmNvbS8xLmpwZw==/gravity/southeast",
  92. },
  93. },
  94. QueueId: "p111a8dd208104ce3b11c78398f658ca8",
  95. }
  96. createJobRes, _, err := c.CI.CreateDocProcessJobs(context.Background(), createJobOpt)
  97. log_status(err)
  98. fmt.Printf("%+v\n", createJobRes.JobsDetail)
  99. // 5、DescribeDocProcessJob
  100. DescribeJobRes, _, err := c.CI.DescribeDocProcessJob(context.Background(), createJobRes.JobsDetail.JobId)
  101. log_status(err)
  102. fmt.Printf("%+v\n", DescribeJobRes.JobsDetail)
  103. // 6、DescribeDocProcessJobs
  104. DescribeJobsOpt := &cos.DescribeDocProcessJobsOptions{
  105. QueueId: "p111a8dd208104ce3b11c78398f658ca8",
  106. Tag: "DocProcess",
  107. }
  108. DescribeJobsRes, _, err := c.CI.DescribeDocProcessJobs(context.Background(), DescribeJobsOpt)
  109. log_status(err)
  110. fmt.Printf("%+v\n", DescribeJobsRes)
  111. // 7、doc-preview
  112. opt := &cos.DocPreviewOptions{
  113. Page: 1,
  114. }
  115. resp, err := c.CI.DocPreview(context.Background(), "form.pdf", opt)
  116. log_status(err)
  117. fd, _ := os.OpenFile("form.pdf", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
  118. io.Copy(fd, resp.Body)
  119. fd.Close()
  120. }