From 01ab468a2d3f2fe5d52b1d76f328c46ad84d3f1e Mon Sep 17 00:00:00 2001 From: toranger Date: Wed, 27 Feb 2019 19:35:16 +0800 Subject: [PATCH] Add PostObjectRestore interface --- costesting/ci_test.go | 27 +++++++++++++++++++++++++++ example/object/restore.go | 43 +++++++++++++++++++++++++++++++++++++++++++ object.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 example/object/restore.go diff --git a/costesting/ci_test.go b/costesting/ci_test.go index a85fe29..2db4e04 100644 --- a/costesting/ci_test.go +++ b/costesting/ci_test.go @@ -334,6 +334,33 @@ func (s *CosTestSuite) TestPutGetObjectACL() { assert.Nil(s.T(), err, "DeleteObject Failed") } +func (s *CosTestSuite) TestPutObjectRestore() { + name := "archivetest" + putOpt := &cos.ObjectPutOptions{ + ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{ + XCosStorageClass: "ARCHIVE", + }, + } + f := strings.NewReader("test") + _, err := s.Client.Object.Put(context.Background(), name, f, putOpt) + assert.Nil(s.T(), err, "PutObject Archive faild") + opt := &cos.ObjectRestoreOptions{ + Days: 2, + Tier: &cos.CASJobParameters{ + // Standard, Exepdited and Bulk + Tier: "Expedited", + }, + } + resp, _ := s.Client.Object.PutRestore(context.Background(), name, opt) + retCode := resp.StatusCode + if retCode != 200 && retCode != 202 && retCode != 409 { + right := false + fmt.Println("PutObjectRestore get code is:", retCode) + assert.Equal(s.T(), true, right, "PutObjectRestore Failed") + } + +} + func (s *CosTestSuite) TestCopyObject() { u := "http://gosdkcopytest-" + s.Appid + ".cos.ap-beijing-1.myqcloud.com" iu, _ := url.Parse(u) diff --git a/example/object/restore.go b/example/object/restore.go new file mode 100644 index 0000000..dccf270 --- /dev/null +++ b/example/object/restore.go @@ -0,0 +1,43 @@ +package main + +import ( + "context" + "os" + + "net/url" + + "net/http" + + "github.com/tencentyun/cos-go-sdk-v5" + "github.com/tencentyun/cos-go-sdk-v5/debug" +) + +func main() { + u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") + b := &cos.BaseURL{BucketURL: 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.ObjectRestoreOptions{ + Days: 2, + Tier: &cos.CASJobParameters{ + // Standard, Exepdited and Bulk + Tier: "Expedited", + }, + } + name := "archivetest" + _, err := c.Object.PutRestore(context.Background(), name, opt) + if err != nil { + panic(err) + } +} diff --git a/object.go b/object.go index 791960f..711e418 100644 --- a/object.go +++ b/object.go @@ -232,6 +232,34 @@ func (s *ObjectService) Options(ctx context.Context, name string, opt *ObjectOpt return resp, err } +// CASJobParameters support three way: Standard(in 35 hours), Expedited(quick way, in 15 mins), Bulk(in 5-12 hours_ +type CASJobParameters struct { + Tier string `xml:"Tier"` +} + +// ObjectRestoreOptions is the option of object restore +type ObjectRestoreOptions struct { + XMLName xml.Name `xml:"RestoreRequest"` + Days int `xml:"Days"` + Tier *CASJobParameters `xml:"CASJobParameters"` +} + +// PutRestore API can recover an object of type archived by COS archive. +// +// https://cloud.tencent.com/document/product/436/12633 +func (s *ObjectService) PutRestore(ctx context.Context, name string, opt *ObjectRestoreOptions) (*Response, error) { + u := fmt.Sprintf("/%s?restore", encodeURIComponent(name)) + sendOpt := sendOptions{ + baseURL: s.client.BaseURL.BucketURL, + uri: u, + method: http.MethodPost, + body: opt, + } + resp, err := s.client.send(ctx, &sendOpt) + + return resp, err +} + // TODO Append 接口在优化未开放使用 // // Append请求可以将一个文件(Object)以分块追加的方式上传至 Bucket 中。使用Append Upload的文件必须事前被设定为Appendable。