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.

92 lines
2.0 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("Resource is not existed")
  18. } else if e, ok := cos.IsCOSError(err); ok {
  19. fmt.Printf("Code: %v\n", e.Code)
  20. fmt.Printf("Message: %v\n", e.Message)
  21. fmt.Printf("Resource: %v\n", e.Resource)
  22. fmt.Printf("RequestId: %v\n", e.RequestID)
  23. // ERROR
  24. } else {
  25. fmt.Println(err)
  26. // ERROR
  27. }
  28. }
  29. func main() {
  30. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  31. b := &cos.BaseURL{
  32. BucketURL: u,
  33. }
  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. RequestBody: true,
  41. ResponseHeader: true,
  42. ResponseBody: true,
  43. },
  44. },
  45. })
  46. opt := &cos.BucketPutOriginOptions{
  47. Rule: []cos.BucketOriginRule{
  48. {
  49. OriginType: "Proxy",
  50. OriginCondition: &cos.BucketOriginCondition{
  51. HTTPStatusCode: "404",
  52. Prefix: "",
  53. },
  54. OriginParameter: &cos.BucketOriginParameter{
  55. Protocol: "FOLLOW",
  56. FollowQueryString: true,
  57. HttpHeader: &cos.BucketOriginHttpHeader{
  58. NewHttpHeaders: []cos.OriginHttpHeader{
  59. {
  60. Key: "x-cos-ContentType",
  61. Value: "csv",
  62. },
  63. },
  64. FollowHttpHeaders: []cos.OriginHttpHeader{
  65. {
  66. Key: "Content-Type",
  67. },
  68. },
  69. },
  70. FollowRedirection: true,
  71. },
  72. OriginInfo: &cos.BucketOriginInfo{
  73. HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
  74. },
  75. },
  76. },
  77. }
  78. _, err := c.Bucket.PutOrigin(context.Background(), opt)
  79. log_status(err)
  80. res, _, err := c.Bucket.GetOrigin(context.Background())
  81. log_status(err)
  82. fmt.Printf("%+v\n", res)
  83. fmt.Printf("%+v\n", res.Rule)
  84. _, err = c.Bucket.DeleteOrigin(context.Background())
  85. log_status(err)
  86. }