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.

64 lines
1.6 KiB

3 years ago
3 years ago
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/tencentyun/cos-go-sdk-v5"
  6. "github.com/tencentyun/cos-go-sdk-v5/debug"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "time"
  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. bucket := "test-1259654469"
  32. bu, _ := url.Parse("https://" + bucket + ".cos.ap-guangzhou.myqcloud.com")
  33. u, _ := url.Parse("http://ap-guangzhou.migration.myqcloud.com")
  34. b := &cos.BaseURL{BucketURL: bu, FetchURL: u}
  35. c := cos.NewClient(b, &http.Client{
  36. Transport: &cos.AuthorizationTransport{
  37. SecretID: os.Getenv("COS_SECRETID"),
  38. SecretKey: os.Getenv("COS_SECRETKEY"),
  39. Transport: &debug.DebugRequestTransport{
  40. RequestHeader: true,
  41. RequestBody: true,
  42. ResponseHeader: true,
  43. ResponseBody: true,
  44. },
  45. },
  46. })
  47. opt := &cos.PutFetchTaskOptions{
  48. Url: "http://" + bucket + ".cos.ap-guangzhou.myqcloud.com/exampleobject",
  49. Key: "exampleobject",
  50. }
  51. res, _, err := c.Object.PutFetchTask(context.Background(), bucket, opt)
  52. log_status(err)
  53. fmt.Printf("res: %+v\n", res)
  54. time.Sleep(time.Second * 3)
  55. rs, _, err := c.Object.GetFetchTask(context.Background(), bucket, res.Data.TaskId)
  56. log_status(err)
  57. fmt.Printf("res: %+v\n", rs)
  58. }