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.

65 lines
1.6 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "time"
  10. "github.com/tencentyun/cos-go-sdk-v5"
  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. // base on http.DefaultTransport
  38. Transport: &http.Transport{
  39. Proxy: http.ProxyFromEnvironment,
  40. DialContext: (&net.Dialer{
  41. Timeout: 30 * time.Second,
  42. KeepAlive: 30 * time.Second,
  43. DualStack: true,
  44. }).DialContext,
  45. MaxIdleConns: 100,
  46. IdleConnTimeout: 90 * time.Second,
  47. TLSHandshakeTimeout: 10 * time.Second,
  48. ExpectContinueTimeout: 1 * time.Second,
  49. // ResponseHeaderTimeout: 1 * time.Second,
  50. // MaxIdleConnsPerHost: 100,
  51. // MaxIdleConns: 100,
  52. },
  53. },
  54. })
  55. // Case1 上传对象
  56. name := "test/example"
  57. // Case3 通过本地文件上传对象
  58. _, err := c.Object.PutFromFile(context.Background(), name, "./test", nil) // 请求的超时时间为 min{context超时时间, HTTP超时时间}
  59. log_status(err)
  60. }