add batch
This commit is contained in:
99
example/batch/create_job.go
Normal file
99
example/batch/create_job.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"fmt"
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func main() {
|
||||
test_batch_bucket := "testcd-1259654469"
|
||||
target_batch_bucket := "targetcd-1259654469"
|
||||
appid := 1259654469
|
||||
uin := "100010805041"
|
||||
region := "ap-chengdu"
|
||||
|
||||
// bucket url:<Bucketname-Appid>.cos.<region>.mycloud.com
|
||||
bucketurl, _ := url.Parse("https://" + test_batch_bucket + ".cos." + region + ".myqcloud.com")
|
||||
// batch url:<uin>.cos-control.<region>.myqcloud.ccom
|
||||
batchurl, _ := url.Parse("https://" + uin + ".cos-control." + region + ".myqcloud.com")
|
||||
|
||||
b := &cos.BaseURL{BucketURL: bucketurl, BatchURL: batchurl}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// 创建需要复制的文件
|
||||
source_name := "test/1.txt"
|
||||
sf := strings.NewReader("batch test content")
|
||||
_, err := c.Object.Put(context.Background(), source_name, sf, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 创建清单文件
|
||||
manifest_name := "test/manifest.csv"
|
||||
f := strings.NewReader(test_batch_bucket + "," + source_name)
|
||||
resp, err := c.Object.Put(context.Background(), manifest_name, f, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
etag := resp.Header.Get("ETag")
|
||||
|
||||
uuid_str := uuid.New().String()
|
||||
opt := &cos.BatchCreateJobOptions{
|
||||
ClientRequestToken: uuid_str,
|
||||
ConfirmationRequired: "true",
|
||||
Description: "test batch",
|
||||
Manifest: &cos.BatchJobManifest{
|
||||
Location: &cos.BatchJobManifestLocation{
|
||||
ETag: etag,
|
||||
ObjectArn: "qcs::cos:" + region + "::" + test_batch_bucket + "/" + manifest_name,
|
||||
},
|
||||
Spec: &cos.BatchJobManifestSpec{
|
||||
Fields: []string{"Bucket", "Key"},
|
||||
Format: "COSBatchOperations_CSV_V1",
|
||||
},
|
||||
},
|
||||
Operation: &cos.BatchJobOperation{
|
||||
PutObjectCopy: &cos.BatchJobOperationCopy{
|
||||
TargetResource: "qcs::cos:" + region + "::" + target_batch_bucket,
|
||||
},
|
||||
},
|
||||
Priority: 1,
|
||||
Report: &cos.BatchJobReport{
|
||||
Bucket: "qcs::cos:" + region + "::" + test_batch_bucket,
|
||||
Enabled: "true",
|
||||
Format: "Report_CSV_V1",
|
||||
Prefix: "job-result",
|
||||
ReportScope: "AllTasks",
|
||||
},
|
||||
RoleArn: "qcs::cam::uin/" + uin + ":roleName/COSBatch_QcsRole",
|
||||
}
|
||||
headers := &cos.BatchRequestHeaders{
|
||||
XCosAppid: appid,
|
||||
}
|
||||
|
||||
res, _, err := c.Batch.CreateJob(context.Background(), opt, headers)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(res)
|
||||
|
||||
}
|
||||
44
example/batch/describe_job.go
Normal file
44
example/batch/describe_job.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"fmt"
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
uin := "100010805041"
|
||||
appid := 1259654469
|
||||
jobid := "795ad997-5557-4869-9a19-b66ec087d460"
|
||||
u, _ := url.Parse("https://" + uin + ".cos-control.ap-chengdu.myqcloud.com")
|
||||
b := &cos.BaseURL{BatchURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
headers := &cos.BatchRequestHeaders{
|
||||
XCosAppid: appid,
|
||||
}
|
||||
|
||||
res, _, err := c.Batch.DescribeJob(context.Background(), jobid, headers)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if res != nil && res.Job != nil {
|
||||
fmt.Printf("%+v", res.Job)
|
||||
}
|
||||
}
|
||||
43
example/batch/list_jobs.go
Normal file
43
example/batch/list_jobs.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"fmt"
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
uin := "100010805041"
|
||||
appid := 1259654469
|
||||
u, _ := url.Parse("https://" + uin + ".cos-control.ap-chengdu.myqcloud.com")
|
||||
b := &cos.BaseURL{BatchURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
headers := &cos.BatchRequestHeaders{
|
||||
XCosAppid: appid,
|
||||
}
|
||||
|
||||
res, _, err := c.Batch.ListJobs(context.Background(), nil, headers)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if res != nil && res.Jobs != nil {
|
||||
fmt.Printf("%+v", res.Jobs)
|
||||
}
|
||||
}
|
||||
48
example/batch/update_priority.go
Normal file
48
example/batch/update_priority.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"fmt"
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
uin := "100010805041"
|
||||
appid := 1259654469
|
||||
jobid := "795ad997-5557-4869-9a19-b66ec087d460"
|
||||
u, _ := url.Parse("https://" + uin + ".cos-control.ap-chengdu.myqcloud.com")
|
||||
b := &cos.BaseURL{BatchURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.BatchUpdatePriorityOptions{
|
||||
JobId: jobid,
|
||||
Priority: 3,
|
||||
}
|
||||
headers := &cos.BatchRequestHeaders{
|
||||
XCosAppid: appid,
|
||||
}
|
||||
|
||||
res, _, err := c.Batch.UpdateJobPriority(context.Background(), opt, headers)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if res != nil {
|
||||
fmt.Printf("%+v", res)
|
||||
}
|
||||
}
|
||||
49
example/batch/update_status.go
Normal file
49
example/batch/update_status.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"fmt"
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
uin := "100010805041"
|
||||
appid := 1259654469
|
||||
jobid := "289b0ea1-5ac5-453d-8a61-7f452dd4a209"
|
||||
u, _ := url.Parse("https://" + uin + ".cos-control.ap-chengdu.myqcloud.com")
|
||||
b := &cos.BaseURL{BatchURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.BatchUpdateStatusOptions{
|
||||
JobId: jobid,
|
||||
RequestedJobStatus: "Ready", // 允许状态转换见 https://cloud.tencent.com/document/product/436/38604
|
||||
StatusUpdateReason: "to test",
|
||||
}
|
||||
headers := &cos.BatchRequestHeaders{
|
||||
XCosAppid: appid,
|
||||
}
|
||||
|
||||
res, _, err := c.Batch.UpdateJobStatus(context.Background(), opt, headers)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if res != nil {
|
||||
fmt.Printf("%+v", res)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user