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.6 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 initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
  12. v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
  13. if err != nil {
  14. panic(err)
  15. }
  16. fmt.Printf("%#v\n", v)
  17. return v
  18. }
  19. func main() {
  20. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  21. b := &cos.BaseURL{BucketURL: u}
  22. c := cos.NewClient(b, &http.Client{
  23. Transport: &cos.AuthorizationTransport{
  24. SecretID: os.Getenv("COS_SECRETID"),
  25. SecretKey: os.Getenv("COS_SECRETKEY"),
  26. Transport: &debug.DebugRequestTransport{
  27. RequestHeader: true,
  28. RequestBody: true,
  29. ResponseHeader: true,
  30. ResponseBody: true,
  31. },
  32. },
  33. })
  34. sourceUrl := "test-1253846586.cos.ap-guangzhou.myqcloud.com/source/copy_multi_upload.go"
  35. name := "test/test_multi_upload.go"
  36. up := initUpload(c, name)
  37. uploadID := up.UploadID
  38. opt := &cos.ObjectCopyPartOptions{}
  39. res, _, err := c.Object.CopyPart(
  40. context.Background(), name, uploadID, 1, sourceUrl, opt)
  41. if err != nil {
  42. panic(err)
  43. }
  44. fmt.Println("ETag:", res.ETag)
  45. completeOpt := &cos.CompleteMultipartUploadOptions{}
  46. completeOpt.Parts = append(completeOpt.Parts, cos.Object{
  47. PartNumber: 1,
  48. ETag: res.ETag,
  49. })
  50. v, resp, err := c.Object.CompleteMultipartUpload(
  51. context.Background(), name, uploadID, completeOpt,
  52. )
  53. if err != nil {
  54. panic(err)
  55. }
  56. fmt.Printf("%s\n", resp.Status)
  57. fmt.Printf("%#v\n", v)
  58. fmt.Printf("%s\n", v.Location)
  59. }