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.

68 lines
1.5 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "net/url"
  7. "strings"
  8. "net/http"
  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 initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
  31. v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
  32. log_status(err)
  33. fmt.Printf("%#v\n", v)
  34. return v
  35. }
  36. func main() {
  37. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  38. b := &cos.BaseURL{BucketURL: u}
  39. c := cos.NewClient(b, &http.Client{
  40. Transport: &cos.AuthorizationTransport{
  41. SecretID: os.Getenv("COS_SECRETID"),
  42. SecretKey: os.Getenv("COS_SECRETKEY"),
  43. Transport: &debug.DebugRequestTransport{
  44. RequestHeader: true,
  45. RequestBody: true,
  46. ResponseHeader: true,
  47. ResponseBody: true,
  48. },
  49. },
  50. })
  51. name := "test/test_multi_upload.go"
  52. up := initUpload(c, name)
  53. uploadID := up.UploadID
  54. f := strings.NewReader("test heoo")
  55. _, err := c.Object.UploadPart(
  56. context.Background(), name, uploadID, 1, f, nil,
  57. )
  58. log_status(err)
  59. }