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
1.7 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "net/url"
  7. "os"
  8. "strings"
  9. "net/http"
  10. "github.com/tencentyun/cos-go-sdk-v5"
  11. "github.com/tencentyun/cos-go-sdk-v5/debug"
  12. )
  13. func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
  14. v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
  15. if err != nil {
  16. panic(err)
  17. }
  18. fmt.Printf("%#v\n", v)
  19. return v
  20. }
  21. func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) string {
  22. b := make([]byte, blockSize)
  23. if _, err := rand.Read(b); err != nil {
  24. panic(err)
  25. }
  26. s := fmt.Sprintf("%X", b)
  27. f := strings.NewReader(s)
  28. resp, err := c.Object.UploadPart(
  29. context.Background(), name, uploadID, n, f, nil,
  30. )
  31. if err != nil {
  32. panic(err)
  33. }
  34. fmt.Printf("%s\n", resp.Status)
  35. return resp.Header.Get("Etag")
  36. }
  37. func main() {
  38. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  39. b := &cos.BaseURL{BucketURL: u}
  40. c := cos.NewClient(b, &http.Client{
  41. Transport: &cos.AuthorizationTransport{
  42. SecretID: os.Getenv("COS_SECRETID"),
  43. SecretKey: os.Getenv("COS_SECRETKEY"),
  44. Transport: &debug.DebugRequestTransport{
  45. RequestHeader: true,
  46. RequestBody: false,
  47. ResponseHeader: true,
  48. ResponseBody: true,
  49. },
  50. },
  51. })
  52. name := "test/test_list_parts.go"
  53. up := initUpload(c, name)
  54. uploadID := up.UploadID
  55. ctx := context.Background()
  56. blockSize := 1024 * 1024 * 3
  57. for i := 1; i < 5; i++ {
  58. uploadPart(c, name, uploadID, blockSize, i)
  59. }
  60. v, _, err := c.Object.ListParts(ctx, name, uploadID)
  61. if err != nil {
  62. panic(err)
  63. return
  64. }
  65. for _, p := range v.Parts {
  66. fmt.Printf("%d, %s, %d\n", p.PartNumber, p.ETag, p.Size)
  67. }
  68. fmt.Printf("%s\n", v.Initiator.ID)
  69. fmt.Printf("%s\n", v.Owner.ID)
  70. }