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