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.

119 lines
2.5 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 log_status(err error) {
  16. if err == nil {
  17. return
  18. }
  19. if cos.IsNotFoundError(err) {
  20. // WARN
  21. fmt.Println("WARN: Resource is not existed")
  22. } else if e, ok := cos.IsCOSError(err); ok {
  23. fmt.Printf("ERROR: Code: %v\n", e.Code)
  24. fmt.Printf("ERROR: Message: %v\n", e.Message)
  25. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  26. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  27. // ERROR
  28. } else {
  29. fmt.Printf("ERROR: %v\n", err)
  30. // ERROR
  31. }
  32. }
  33. func genBigData(blockSize int) []byte {
  34. b := make([]byte, blockSize)
  35. if _, err := rand.Read(b); err != nil {
  36. log_status(err)
  37. }
  38. return b
  39. }
  40. func uploadMulti(c *cos.Client) []string {
  41. names := []string{}
  42. data := genBigData(1024 * 1024 * 1)
  43. ctx := context.Background()
  44. var r io.Reader
  45. var name string
  46. n := 3
  47. for n > 0 {
  48. name = fmt.Sprintf("test/test_multi_delete_%s", time.Now().Format(time.RFC3339))
  49. r = bytes.NewReader(data)
  50. c.Object.Put(ctx, name, r, nil)
  51. names = append(names, name)
  52. n--
  53. }
  54. return names
  55. }
  56. func main() {
  57. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  58. b := &cos.BaseURL{BucketURL: u}
  59. c := cos.NewClient(b, &http.Client{
  60. Transport: &cos.AuthorizationTransport{
  61. SecretID: os.Getenv("COS_SECRETID"),
  62. SecretKey: os.Getenv("COS_SECRETKEY"),
  63. Transport: &debug.DebugRequestTransport{
  64. RequestHeader: true,
  65. RequestBody: false,
  66. ResponseHeader: true,
  67. ResponseBody: true,
  68. },
  69. },
  70. })
  71. ctx := context.Background()
  72. names := uploadMulti(c)
  73. names = append(names, []string{"a", "b", "c", "a+bc/xx&?+# "}...)
  74. obs := []cos.Object{}
  75. for _, v := range names {
  76. obs = append(obs, cos.Object{Key: v})
  77. }
  78. //sha1 := ""
  79. opt := &cos.ObjectDeleteMultiOptions{
  80. Objects: obs,
  81. //XCosSha1: sha1,
  82. //Quiet: true,
  83. }
  84. c = cos.NewClient(b, &http.Client{
  85. Transport: &cos.AuthorizationTransport{
  86. SecretID: os.Getenv("COS_SECRETID"),
  87. SecretKey: os.Getenv("COS_SECRETKEY"),
  88. Transport: &debug.DebugRequestTransport{
  89. RequestHeader: true,
  90. RequestBody: true,
  91. ResponseHeader: true,
  92. ResponseBody: true,
  93. },
  94. },
  95. })
  96. v, _, err := c.Object.DeleteMulti(ctx, opt)
  97. log_status(err)
  98. for _, x := range v.DeletedObjects {
  99. fmt.Printf("deleted %s\n", x.Key)
  100. }
  101. for _, x := range v.Errors {
  102. fmt.Printf("error %s, %s, %s\n", x.Key, x.Code, x.Message)
  103. }
  104. }