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.

102 lines
2.0 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "os"
  7. "time"
  8. "bytes"
  9. "io"
  10. "math/rand"
  11. "net/http"
  12. "github.com/tencentyun/cos-go-sdk-v5"
  13. "github.com/tencentyun/cos-go-sdk-v5/debug"
  14. )
  15. func genBigData(blockSize int) []byte {
  16. b := make([]byte, blockSize)
  17. if _, err := rand.Read(b); err != nil {
  18. panic(err)
  19. }
  20. return b
  21. }
  22. func uploadMulti(c *cos.Client) []string {
  23. names := []string{}
  24. data := genBigData(1024 * 1024 * 1)
  25. ctx := context.Background()
  26. var r io.Reader
  27. var name string
  28. n := 3
  29. for n > 0 {
  30. name = fmt.Sprintf("test/test_multi_delete_%s", time.Now().Format(time.RFC3339))
  31. r = bytes.NewReader(data)
  32. c.Object.Put(ctx, name, r, nil)
  33. names = append(names, name)
  34. n--
  35. }
  36. return names
  37. }
  38. func main() {
  39. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  40. b := &cos.BaseURL{BucketURL: u}
  41. c := cos.NewClient(b, &http.Client{
  42. Transport: &cos.AuthorizationTransport{
  43. SecretID: os.Getenv("COS_SECRETID"),
  44. SecretKey: os.Getenv("COS_SECRETKEY"),
  45. Transport: &debug.DebugRequestTransport{
  46. RequestHeader: true,
  47. RequestBody: false,
  48. ResponseHeader: true,
  49. ResponseBody: true,
  50. },
  51. },
  52. })
  53. ctx := context.Background()
  54. names := uploadMulti(c)
  55. names = append(names, []string{"a", "b", "c", "a+bc/xx&?+# "}...)
  56. obs := []cos.Object{}
  57. for _, v := range names {
  58. obs = append(obs, cos.Object{Key: v})
  59. }
  60. //sha1 := ""
  61. opt := &cos.ObjectDeleteMultiOptions{
  62. Objects: obs,
  63. //XCosSha1: sha1,
  64. //Quiet: true,
  65. }
  66. c = cos.NewClient(b, &http.Client{
  67. Transport: &cos.AuthorizationTransport{
  68. SecretID: os.Getenv("COS_SECRETID"),
  69. SecretKey: os.Getenv("COS_SECRETKEY"),
  70. Transport: &debug.DebugRequestTransport{
  71. RequestHeader: true,
  72. RequestBody: true,
  73. ResponseHeader: true,
  74. ResponseBody: true,
  75. },
  76. },
  77. })
  78. v, _, err := c.Object.DeleteMulti(ctx, opt)
  79. if err != nil {
  80. panic(err)
  81. }
  82. for _, x := range v.DeletedObjects {
  83. fmt.Printf("deleted %s\n", x.Key)
  84. }
  85. for _, x := range v.Errors {
  86. fmt.Printf("error %s, %s, %s\n", x.Key, x.Code, x.Message)
  87. }
  88. }