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.

81 lines
2.0 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "net/url"
  7. "net/http"
  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("WARN: Resource is not existed")
  18. } else if e, ok := cos.IsCOSError(err); ok {
  19. fmt.Printf("ERROR: Code: %v\n", e.Code)
  20. fmt.Printf("ERROR: Message: %v\n", e.Message)
  21. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  22. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  23. // ERROR
  24. } else {
  25. fmt.Printf("ERROR: %v\n", err)
  26. // ERROR
  27. }
  28. }
  29. func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
  30. v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
  31. log_status(err)
  32. fmt.Printf("%#v\n", v)
  33. return v
  34. }
  35. func main() {
  36. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  37. b := &cos.BaseURL{BucketURL: u}
  38. c := cos.NewClient(b, &http.Client{
  39. Transport: &cos.AuthorizationTransport{
  40. SecretID: os.Getenv("COS_SECRETID"),
  41. SecretKey: os.Getenv("COS_SECRETKEY"),
  42. Transport: &debug.DebugRequestTransport{
  43. RequestHeader: true,
  44. RequestBody: true,
  45. ResponseHeader: true,
  46. ResponseBody: true,
  47. },
  48. },
  49. })
  50. sourceUrl := "test-1253846586.cos.ap-guangzhou.myqcloud.com/source/copy_multi_upload.go"
  51. name := "test/test_multi_upload.go"
  52. up := initUpload(c, name)
  53. uploadID := up.UploadID
  54. opt := &cos.ObjectCopyPartOptions{}
  55. res, _, err := c.Object.CopyPart(
  56. context.Background(), name, uploadID, 1, sourceUrl, opt)
  57. log_status(err)
  58. fmt.Println("ETag:", res.ETag)
  59. completeOpt := &cos.CompleteMultipartUploadOptions{}
  60. completeOpt.Parts = append(completeOpt.Parts, cos.Object{
  61. PartNumber: 1,
  62. ETag: res.ETag,
  63. })
  64. v, resp, err := c.Object.CompleteMultipartUpload(
  65. context.Background(), name, uploadID, completeOpt,
  66. )
  67. log_status(err)
  68. fmt.Printf("%s\n", resp.Status)
  69. fmt.Printf("%#v\n", v)
  70. fmt.Printf("%s\n", v.Location)
  71. }