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.

76 lines
1.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "os"
  7. "time"
  8. "fmt"
  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. //设置超时时间
  35. Timeout: 100 * time.Second,
  36. Transport: &cos.AuthorizationTransport{
  37. SecretID: os.Getenv("COS_SECRETID"),
  38. SecretKey: os.Getenv("COS_SECRETKEY"),
  39. Transport: &debug.DebugRequestTransport{
  40. RequestHeader: false,
  41. RequestBody: false,
  42. ResponseHeader: false,
  43. ResponseBody: false,
  44. },
  45. },
  46. })
  47. // Case1 多线程上传对象
  48. opt := &cos.MultiUploadOptions{
  49. EnableVerification: true,
  50. ThreadPoolSize: 5,
  51. }
  52. v, _, err := c.Object.Upload(
  53. context.Background(), "gomulput1G", "./test1G", opt,
  54. )
  55. log_status(err)
  56. fmt.Printf("Case1 done, %v\n", v)
  57. // Case2 多线程上传对象,查看上传进度
  58. opt.OptIni = &cos.InitiateMultipartUploadOptions{
  59. nil,
  60. &cos.ObjectPutHeaderOptions{
  61. Listener: &cos.DefaultProgressListener{},
  62. },
  63. }
  64. v, _, err = c.Object.Upload(
  65. context.Background(), "gomulput1G", "./test1G", opt,
  66. )
  67. log_status(err)
  68. fmt.Printf("Case2 done, %v\n", v)
  69. }