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.

100 lines
2.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "github.com/google/uuid"
  11. "github.com/tencentyun/cos-go-sdk-v5"
  12. "github.com/tencentyun/cos-go-sdk-v5/debug"
  13. )
  14. func main() {
  15. test_batch_bucket := "testcd-1259654469"
  16. target_batch_bucket := "cosgosdkreptest-1259654469"
  17. appid := 1259654469
  18. uin := "100010805041"
  19. region := "ap-chengdu"
  20. // bucket url:<Bucketname-Appid>.cos.<region>.mycloud.com
  21. bucketurl, _ := url.Parse("https://" + test_batch_bucket + ".cos." + region + ".myqcloud.com")
  22. // batch url:<uin>.cos-control.<region>.myqcloud.ccom
  23. batchurl, _ := url.Parse("https://" + uin + ".cos-control." + region + ".myqcloud.com")
  24. b := &cos.BaseURL{BucketURL: bucketurl, BatchURL: batchurl}
  25. c := cos.NewClient(b, &http.Client{
  26. Transport: &cos.AuthorizationTransport{
  27. SecretID: os.Getenv("COS_SECRETID"),
  28. SecretKey: os.Getenv("COS_SECRETKEY"),
  29. Transport: &debug.DebugRequestTransport{
  30. RequestHeader: true,
  31. RequestBody: true,
  32. ResponseHeader: true,
  33. ResponseBody: true,
  34. },
  35. },
  36. })
  37. // 创建需要复制的文件
  38. source_name := "test/1.txt"
  39. sf := strings.NewReader("batch test content")
  40. _, err := c.Object.Put(context.Background(), source_name, sf, nil)
  41. if err != nil {
  42. panic(err)
  43. }
  44. // 创建清单文件
  45. manifest_name := "test/manifest.csv"
  46. f := strings.NewReader(test_batch_bucket + "," + source_name)
  47. resp, err := c.Object.Put(context.Background(), manifest_name, f, nil)
  48. if err != nil {
  49. panic(err)
  50. }
  51. etag := resp.Header.Get("ETag")
  52. uuid_str := uuid.New().String()
  53. opt := &cos.BatchCreateJobOptions{
  54. ClientRequestToken: uuid_str,
  55. ConfirmationRequired: "true",
  56. Description: "test batch",
  57. Manifest: &cos.BatchJobManifest{
  58. Location: &cos.BatchJobManifestLocation{
  59. ETag: etag,
  60. ObjectArn: "qcs::cos:" + region + "::" + test_batch_bucket + "/" + manifest_name,
  61. },
  62. Spec: &cos.BatchJobManifestSpec{
  63. Fields: []string{"Bucket", "Key"},
  64. Format: "COSBatchOperations_CSV_V1",
  65. },
  66. },
  67. Operation: &cos.BatchJobOperation{
  68. PutObjectCopy: &cos.BatchJobOperationCopy{
  69. TargetResource: "qcs::cos:" + region + ":uid/" + strconv.Itoa(appid) + ":" + target_batch_bucket,
  70. },
  71. },
  72. Priority: 1,
  73. Report: &cos.BatchJobReport{
  74. Bucket: "qcs::cos:" + region + "::" + test_batch_bucket,
  75. Enabled: "true",
  76. Format: "Report_CSV_V1",
  77. Prefix: "job-result",
  78. ReportScope: "AllTasks",
  79. },
  80. RoleArn: "qcs::cam::uin/" + uin + ":roleName/COSBatch_QcsRole",
  81. }
  82. headers := &cos.BatchRequestHeaders{
  83. XCosAppid: appid,
  84. }
  85. res, _, err := c.Batch.CreateJob(context.Background(), opt, headers)
  86. if err != nil {
  87. panic(err)
  88. }
  89. fmt.Println(res)
  90. }