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.4 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "math/rand"
  7. "net/url"
  8. "os"
  9. "time"
  10. "net/http"
  11. "github.com/tencentyun/cos-go-sdk-v5"
  12. "github.com/tencentyun/cos-go-sdk-v5/debug"
  13. )
  14. func genBigData(blockSize int) []byte {
  15. b := make([]byte, blockSize)
  16. if _, err := rand.Read(b); err != nil {
  17. panic(err)
  18. }
  19. return b
  20. }
  21. func main() {
  22. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  23. b := &cos.BaseURL{
  24. BucketURL: u,
  25. }
  26. c := cos.NewClient(b, &http.Client{
  27. Transport: &cos.AuthorizationTransport{
  28. SecretID: os.Getenv("COS_SECRETID"),
  29. SecretKey: os.Getenv("COS_SECRETKEY"),
  30. Transport: &debug.DebugRequestTransport{
  31. RequestHeader: true,
  32. RequestBody: false,
  33. ResponseHeader: true,
  34. ResponseBody: true,
  35. },
  36. },
  37. })
  38. startTime := time.Now()
  39. name := fmt.Sprintf("test/test_object_append_%s", startTime.Format(time.RFC3339))
  40. data := genBigData(1024 * 1024 * 1)
  41. length := len(data)
  42. r := bytes.NewReader(data)
  43. ctx := context.Background()
  44. // 第一次就必须 append
  45. resp, err := c.Object.Append(ctx, name, 0, r, nil)
  46. if err != nil {
  47. panic(err)
  48. return
  49. }
  50. fmt.Printf("%s\n", resp.Status)
  51. // head
  52. if _, err = c.Object.Head(ctx, name, nil); err != nil {
  53. panic(err)
  54. return
  55. }
  56. // 再次 append
  57. data = genBigData(1024 * 1024 * 5)
  58. r = bytes.NewReader(data)
  59. resp, err = c.Object.Append(context.Background(), name, length, r, nil)
  60. if err != nil {
  61. panic(err)
  62. }
  63. fmt.Printf("%s\n", resp.Status)
  64. }