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.

105 lines
2.7 KiB

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