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.

75 lines
1.7 KiB

4 years ago
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "os"
  8. "path/filepath"
  9. "sync"
  10. "github.com/tencentyun/cos-go-sdk-v5"
  11. "github.com/tencentyun/cos-go-sdk-v5/debug"
  12. )
  13. func log_status(err error) {
  14. if err == nil {
  15. return
  16. }
  17. if cos.IsNotFoundError(err) {
  18. // WARN
  19. fmt.Println("WARN: Resource is not existed")
  20. } else if e, ok := cos.IsCOSError(err); ok {
  21. fmt.Printf("ERROR: Code: %v\n", e.Code)
  22. fmt.Printf("ERROR: Message: %v\n", e.Message)
  23. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  24. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  25. // ERROR
  26. } else {
  27. fmt.Printf("ERROR: %v\n", err)
  28. // ERROR
  29. }
  30. }
  31. func upload(wg *sync.WaitGroup, c *cos.Client, keysCh <-chan string) {
  32. defer wg.Done()
  33. for key := range keysCh {
  34. // 下载文件到当前目录
  35. _, filename := filepath.Split(key)
  36. _, err := c.Object.GetToFile(context.Background(), key, filename, nil)
  37. if err != nil {
  38. log_status(err)
  39. }
  40. }
  41. }
  42. func main() {
  43. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  44. b := &cos.BaseURL{BucketURL: u}
  45. c := cos.NewClient(b, &http.Client{
  46. Transport: &cos.AuthorizationTransport{
  47. SecretID: os.Getenv("COS_SECRETID"),
  48. SecretKey: os.Getenv("COS_SECRETKEY"),
  49. Transport: &debug.DebugRequestTransport{
  50. RequestHeader: true,
  51. // Notice when put a large file and set need the request body, might happend out of memory error.
  52. RequestBody: false,
  53. ResponseHeader: true,
  54. ResponseBody: false,
  55. },
  56. },
  57. })
  58. keysCh := make(chan string, 2)
  59. keys := []string{"test/test1", "test/test2", "test/test3"}
  60. var wg sync.WaitGroup
  61. threadpool := 2
  62. for i := 0; i < threadpool; i++ {
  63. wg.Add(1)
  64. go upload(&wg, c, keysCh)
  65. }
  66. for _, key := range keys {
  67. keysCh <- key
  68. }
  69. close(keysCh)
  70. wg.Wait()
  71. }