From 517ae20fdc06a6e0eb9e5482d1da6716f4542a5b Mon Sep 17 00:00:00 2001 From: jojoliang Date: Tue, 19 May 2020 21:07:37 +0800 Subject: [PATCH 1/2] add GetBucketObjectVersions & update tips when failed --- bucket.go | 58 ++++++++++ bucket_test.go | 105 ++++++++++++++++++ costesting/ci_test.go | 9 ++ example/bucket/getLogging.go | 38 +++++++ example/bucket/getObjectVersion.go | 64 +++++++++++ .../object/{MutiUpload.go => MultiUpload.go} | 23 +++- example/object/abortMultipartUpload.go | 27 ++++- example/object/ci_put.go | 12 +- example/object/completeMultipartUpload.go | 35 ++++-- example/object/copy.go | 36 ++++-- example/object/copyPart.go | 31 ++++-- example/object/delete.go | 24 +++- example/object/deleteMultiple.go | 27 ++++- example/object/get.go | 42 ++++--- example/object/getACL.go | 25 ++++- example/object/getAnonymous.go | 24 +++- example/object/getByPresignedURL.go | 32 ++++-- example/object/head.go | 24 +++- example/object/initiateMultipartUpload.go | 23 +++- example/object/listParts.go | 31 ++++-- example/object/options.go | 26 ++++- example/object/presigned_url_with_token.go | 44 ++++++++ example/object/put.go | 35 ++++-- example/object/putACL.go | 28 ++++- example/object/restore.go | 24 +++- example/object/sse_c.go | 81 ++++++++++++++ example/object/sse_cos.go | 75 +++++++++++++ example/object/upload.go | 23 +++- example/object/uploadFile.go | 29 ++++- example/object/uploadPart.go | 27 ++++- 30 files changed, 944 insertions(+), 138 deletions(-) create mode 100644 example/bucket/getLogging.go create mode 100644 example/bucket/getObjectVersion.go rename example/object/{MutiUpload.go => MultiUpload.go} (64%) create mode 100644 example/object/presigned_url_with_token.go create mode 100644 example/object/sse_c.go create mode 100644 example/object/sse_cos.go diff --git a/bucket.go b/bucket.go index 2e3f92c..67f991b 100644 --- a/bucket.go +++ b/bucket.go @@ -102,3 +102,61 @@ type Bucket struct { Region string `xml:"Location,omitempty"` CreationDate string `xml:",omitempty"` } + +type BucketGetObjectVersionsOptions struct { + Prefix string `url:"prefix,omitempty"` + Delimiter string `url:"delimiter,omitempty"` + EncodingType string `url:"encoding-type,omitempty"` + KeyMarker string `url:"key-marker,omitempty"` + VersionIdMarker string `url:"version-id-marker,omitempty"` + MaxKeys int `url:"max-keys,omitempty"` +} + +type BucketGetObjectVersionsResult struct { + XMLName xml.Name `xml:"ListVersionsResult"` + Name string `xml:"Name,omitempty"` + EncodingType string `xml:"EncodingType,omitempty"` + Prefix string `xml:"Prefix,omitempty"` + KeyMarker string `xml:"KeyMarker,omitempty"` + VersionIdMarker string `xml:"VersionIdMarker,omitempty"` + MaxKeys int `xml:"MaxKeys,omitempty"` + Delimiter string `xml:"Delimiter,omitempty"` + IsTruncated bool `xml:"IsTruncated,omitempty"` + NextKeyMarker string `xml:"NextKeyMarker,omitempty"` + NextVersionIdMarker string `xml:"NextVersionIdMarker,omitempty"` + CommonPrefixes []string `xml:"CommonPrefixes>Prefix,omitempty"` + Version []ListVersionsResultVersion `xml:"Version,omitempty"` + DeleteMarker []ListVersionsResultDeleteMarker `xml:"DeleteMarker,omitempty"` +} + +type ListVersionsResultVersion struct { + Key string `xml:"Key,omitempty"` + VersionId string `xml:"VersionId,omitempty"` + IsLatest bool `xml:"IsLatest,omitempty"` + LastModified string `xml:"LastModified,omitempty"` + ETag string `xml:"ETag,omitempty"` + Size int `xml:"Size,omitempty"` + StorageClass string `xml:"StorageClass,omitempty"` + Owner *Owner `xml:"Owner,omitempty"` +} + +type ListVersionsResultDeleteMarker struct { + Key string `xml:"Key,omitempty"` + VersionId string `xml:"VersionId,omitempty"` + IsLatest bool `xml:"IsLatest,omitempty"` + LastModified string `xml:"LastModified,omitempty"` + Owner *Owner `xml:"Owner,omitempty"` +} + +func (s *BucketService) GetObjectVersions(ctx context.Context, opt *BucketGetObjectVersionsOptions) (*BucketGetObjectVersionsResult, *Response, error) { + var res BucketGetObjectVersionsResult + sendOpt := sendOptions{ + baseURL: s.client.BaseURL.BucketURL, + uri: "/?versions", + method: http.MethodGet, + optQuery: opt, + result: &res, + } + resp, err := s.client.send(ctx, &sendOpt) + return &res, resp, err +} diff --git a/bucket_test.go b/bucket_test.go index 4a5812e..16c55a2 100644 --- a/bucket_test.go +++ b/bucket_test.go @@ -150,3 +150,108 @@ func TestBucketService_Head(t *testing.T) { t.Fatalf("Bucket.Head returned error: %v", err) } } + +func TestBucketService_GetObjectVersions(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + w.WriteHeader(http.StatusOK) + vs := values{ + "versions": "", + "delimiter": "/", + } + testFormValues(t, r, vs) + + fmt.Fprint(w, ` + + examplebucket-1250000000 + + + + 1000 + false + / + + example-folder-1/ + + + example-folder-2/ + + + example-object-1.jpg + MTg0NDUxNzgxMjEzNTU3NTk1Mjg + true + 2019-08-16T10:45:53.000Z + "5d1143df07a17b23320d0da161e2819e" + 30 + STANDARD + + 1250000000 + 1250000000 + + + + example-object-1.jpg + MTg0NDUxNzgxMjEzNjE1OTcxMzM + false + 2019-08-16T10:45:47.000Z + + 1250000000 + 1250000000 + + +`) + }) + + want := &BucketGetObjectVersionsResult { + XMLName: xml.Name { Local: "ListVersionsResult" }, + Name: "examplebucket-1250000000", + MaxKeys: 1000, + IsTruncated: false, + Delimiter: "/", + CommonPrefixes: []string { + "example-folder-1/", + "example-folder-2/", + }, + Version: []ListVersionsResultVersion { + { + Key: "example-object-1.jpg", + VersionId: "MTg0NDUxNzgxMjEzNTU3NTk1Mjg", + IsLatest: true, + LastModified: "2019-08-16T10:45:53.000Z", + ETag: "\"5d1143df07a17b23320d0da161e2819e\"", + Size: 30, + StorageClass: "STANDARD", + Owner: &Owner { + ID: "1250000000", + DisplayName: "1250000000", + }, + }, + }, + DeleteMarker: []ListVersionsResultDeleteMarker { + { + Key: "example-object-1.jpg", + VersionId: "MTg0NDUxNzgxMjEzNjE1OTcxMzM", + IsLatest: false, + LastModified: "2019-08-16T10:45:47.000Z", + Owner: &Owner { + ID: "1250000000", + DisplayName: "1250000000", + }, + }, + }, + } + opt := &BucketGetObjectVersionsOptions { + Delimiter: "/", + } + res, _, err := client.Bucket.GetObjectVersions(context.Background(), opt) + if err != nil { + t.Fatalf("Bucket.GetObjectVersions returned error: %v", err) + } + if !reflect.DeepEqual(res, want) { + t.Errorf("Bucket.GetObjectVersions returned\n%+v\nwant\n%+v", res, want) + } + +} diff --git a/costesting/ci_test.go b/costesting/ci_test.go index 38a8ffa..491bce8 100644 --- a/costesting/ci_test.go +++ b/costesting/ci_test.go @@ -184,6 +184,15 @@ func (s *CosTestSuite) TestGetBucket() { assert.Nil(s.T(), err, "GetBucket Failed") } +func (s *CosTestSuite) TestGetObjectVersions() { + opt := &cos.BucketGetObjectVersionsOptions { + Prefix: "中文", + MaxKeys: 3, + } + _, _, err := s.Client.Bucket.GetObjectVersions(context.Background(), opt) + assert.Nil(s.T(), err, "GetObjectVersions Failed") +} + func (s *CosTestSuite) TestGetBucketLocation() { v, _, err := s.Client.Bucket.GetLocation(context.Background()) assert.Nil(s.T(), err, "GetLocation Failed") diff --git a/example/bucket/getLogging.go b/example/bucket/getLogging.go new file mode 100644 index 0000000..6d68f7e --- /dev/null +++ b/example/bucket/getLogging.go @@ -0,0 +1,38 @@ +package main + +import ( + "context" + "fmt" + "net/url" + "os" + + "net/http" + + "github.com/tencentyun/cos-go-sdk-v5" + "github.com/tencentyun/cos-go-sdk-v5/debug" +) + +func main() { + u, _ := url.Parse("https://bj-1259654469.cos.ap-beijing.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, + }, + }, + }) + + v, _, err := c.Bucket.GetLogging(context.Background()) + if err != nil { + panic(err) + } + fmt.Printf("%+v\n", v.LoggingEnabled) +} diff --git a/example/bucket/getObjectVersion.go b/example/bucket/getObjectVersion.go new file mode 100644 index 0000000..a9aca0a --- /dev/null +++ b/example/bucket/getObjectVersion.go @@ -0,0 +1,64 @@ +package main + +import ( + "context" + "fmt" + "os" + + "net/url" + + "net/http" + + "github.com/tencentyun/cos-go-sdk-v5" + "github.com/tencentyun/cos-go-sdk-v5/debug" +) + +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + +func main() { + u, _ := url.Parse("https://test-1259654469.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.BucketGetObjectVersionsOptions { + Delimiter: "/", + MaxKeys: 1, + } + v, _, err := c.Bucket.GetObjectVersions(context.Background(), opt) + log_status(err) + + for _, c := range v.Version { + fmt.Printf("%v, %v, %v\n", c.Key, c.Size, c.IsLatest) + } + +} diff --git a/example/object/MutiUpload.go b/example/object/MultiUpload.go similarity index 64% rename from example/object/MutiUpload.go rename to example/object/MultiUpload.go index 282f7af..1439c75 100644 --- a/example/object/MutiUpload.go +++ b/example/object/MultiUpload.go @@ -12,6 +12,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("http://alanbj-1251668577.cos.ap-beijing.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -37,8 +56,6 @@ func main() { v, _, err := c.Object.MultiUpload( context.Background(), "test/gomulput1G", "./test1G", opt, ) - if err != nil { - panic(err) - } + log_status(err) fmt.Println(v) } diff --git a/example/object/abortMultipartUpload.go b/example/object/abortMultipartUpload.go index 1544169..3ada616 100644 --- a/example/object/abortMultipartUpload.go +++ b/example/object/abortMultipartUpload.go @@ -12,6 +12,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -30,14 +49,10 @@ func main() { name := "test_multipart.txt" v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%s\n", v.UploadID) resp, err := c.Object.AbortMultipartUpload(context.Background(), name, v.UploadID) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%s\n", resp.Status) } diff --git a/example/object/ci_put.go b/example/object/ci_put.go index de36848..5417431 100644 --- a/example/object/ci_put.go +++ b/example/object/ci_put.go @@ -17,15 +17,15 @@ func log_status(err error) { } if cos.IsNotFoundError(err) { // WARN - fmt.Println("Resource is not existed") + fmt.Println("WARN: Resource is not existed") } else if e, ok := cos.IsCOSError(err); ok { - fmt.Printf("Code: %v\n", e.Code) - fmt.Printf("Message: %v\n", e.Message) - fmt.Printf("Resource: %v\n", e.Resource) - fmt.Printf("RequestId: %v\n", e.RequestID) + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) // ERROR } else { - fmt.Println(err) + fmt.Printf("ERROR: %v\n", err) // ERROR } } diff --git a/example/object/completeMultipartUpload.go b/example/object/completeMultipartUpload.go index 9558158..a97c9d2 100644 --- a/example/object/completeMultipartUpload.go +++ b/example/object/completeMultipartUpload.go @@ -14,11 +14,28 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult { v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%#v\n", v) return v } @@ -27,7 +44,7 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s b := make([]byte, blockSize) if _, err := rand.Read(b); err != nil { - panic(err) + log_status(err) } s := fmt.Sprintf("%X", b) f := strings.NewReader(s) @@ -35,15 +52,13 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s resp, err := c.Object.UploadPart( context.Background(), name, uploadID, n, f, nil, ) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%s\n", resp.Status) return resp.Header.Get("Etag") } func main() { - u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} c := cos.NewClient(b, &http.Client{ Transport: &cos.AuthorizationTransport{ @@ -86,9 +101,7 @@ func main() { v, resp, err := c.Object.CompleteMultipartUpload( context.Background(), name, uploadID, opt, ) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%s\n", resp.Status) fmt.Printf("%#v\n", v) fmt.Printf("%s\n", v.Location) diff --git a/example/object/copy.go b/example/object/copy.go index 2a81cb3..199be64 100644 --- a/example/object/copy.go +++ b/example/object/copy.go @@ -16,8 +16,27 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { - u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} c := cos.NewClient(b, &http.Client{ Transport: &cos.AuthorizationTransport{ @@ -37,24 +56,19 @@ func main() { f := strings.NewReader(expected) _, err := c.Object.Put(context.Background(), source, f, nil) - if err != nil { - panic(err) - } + log_status(err) soruceURL := fmt.Sprintf("%s/%s", u.Host, source) dest := fmt.Sprintf("test/objectMove_%d.go", time.Now().Nanosecond()) //opt := &cos.ObjectCopyOptions{} res, _, err := c.Object.Copy(context.Background(), dest, soruceURL, nil) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%+v\n\n", res) resp, err := c.Object.Get(context.Background(), dest, nil) - if err != nil { - panic(err) - } - bs, _ := ioutil.ReadAll(resp.Body) + log_status(err) + + bs, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() result := string(bs) if result != expected { diff --git a/example/object/copyPart.go b/example/object/copyPart.go index 22ce620..5b96356 100644 --- a/example/object/copyPart.go +++ b/example/object/copyPart.go @@ -13,11 +13,28 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult { v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%#v\n", v) return v } @@ -46,9 +63,7 @@ func main() { opt := &cos.ObjectCopyPartOptions{} res, _, err := c.Object.CopyPart( context.Background(), name, uploadID, 1, sourceUrl, opt) - if err != nil { - panic(err) - } + log_status(err) fmt.Println("ETag:", res.ETag) completeOpt := &cos.CompleteMultipartUploadOptions{} @@ -59,9 +74,7 @@ func main() { v, resp, err := c.Object.CompleteMultipartUpload( context.Background(), name, uploadID, completeOpt, ) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%s\n", resp.Status) fmt.Printf("%#v\n", v) fmt.Printf("%s\n", v.Location) diff --git a/example/object/delete.go b/example/object/delete.go index ed227c9..bb8ffaf 100644 --- a/example/object/delete.go +++ b/example/object/delete.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "net/url" "os" @@ -11,6 +12,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -30,7 +50,5 @@ func main() { name := "test/objectPut.go" _, err := c.Object.Delete(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) } diff --git a/example/object/deleteMultiple.go b/example/object/deleteMultiple.go index 018e4a2..4faa846 100644 --- a/example/object/deleteMultiple.go +++ b/example/object/deleteMultiple.go @@ -18,10 +18,29 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func genBigData(blockSize int) []byte { b := make([]byte, blockSize) if _, err := rand.Read(b); err != nil { - panic(err) + log_status(err) } return b } @@ -46,7 +65,7 @@ func uploadMulti(c *cos.Client) []string { } func main() { - u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} c := cos.NewClient(b, &http.Client{ Transport: &cos.AuthorizationTransport{ @@ -89,9 +108,7 @@ func main() { }) v, _, err := c.Object.DeleteMulti(ctx, opt) - if err != nil { - panic(err) - } + log_status(err) for _, x := range v.DeletedObjects { fmt.Printf("deleted %s\n", x.Key) diff --git a/example/object/get.go b/example/object/get.go index b2c1bcb..2650030 100644 --- a/example/object/get.go +++ b/example/object/get.go @@ -13,6 +13,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -32,31 +51,26 @@ func main() { // Case1 Download object into ReadCloser(). the body needs to be closed name := "test/hello.txt" resp, err := c.Object.Get(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) + bs, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() fmt.Printf("%s\n", string(bs)) // Case2 Download object to local file. the body needs to be closed fd, err := os.OpenFile("hello.txt", os.O_WRONLY|os.O_CREATE, 0660) - if err != nil { - panic(err) - } + log_status(err) + defer fd.Close() resp, err = c.Object.Get(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) + io.Copy(fd, resp.Body) resp.Body.Close() // Case3 Download object to local file path _, err = c.Object.GetToFile(context.Background(), name, "hello_1.txt", nil) - if err != nil { - panic(err) - } + log_status(err) // Case4 Download object with range header, can used to concurrent download opt := &cos.ObjectGetOptions{ @@ -64,9 +78,7 @@ func main() { Range: "bytes=0-3", } resp, err = c.Object.Get(context.Background(), name, opt) - if err != nil { - panic(err) - } + log_status(err) bs, _ = ioutil.ReadAll(resp.Body) resp.Body.Close() fmt.Printf("%s\n", string(bs)) diff --git a/example/object/getACL.go b/example/object/getACL.go index f098fe2..0beb495 100644 --- a/example/object/getACL.go +++ b/example/object/getACL.go @@ -12,8 +12,27 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { - u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} c := cos.NewClient(b, &http.Client{ Transport: &cos.AuthorizationTransport{ @@ -30,9 +49,7 @@ func main() { name := "test/hello.txt" v, _, err := c.Object.GetACL(context.Background(), name) - if err != nil { - panic(err) - } + log_status(err) for _, a := range v.AccessControlList { fmt.Printf("%s, %s, %s\n", a.Grantee.Type, a.Grantee.ID, a.Permission) } diff --git a/example/object/getAnonymous.go b/example/object/getAnonymous.go index be1d46e..90ceb30 100644 --- a/example/object/getAnonymous.go +++ b/example/object/getAnonymous.go @@ -11,6 +11,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func upload(c *cos.Client, name string) { f := strings.NewReader("test") f = strings.NewReader("test xxx") @@ -35,10 +54,7 @@ func main() { upload(c, name) resp, err := c.Object.Get(context.Background(), name, nil) - if err != nil { - panic(err) - return - } + log_status(err) bs, _ := ioutil.ReadAll(resp.Body) defer resp.Body.Close() fmt.Printf("%s\n", string(bs)) diff --git a/example/object/getByPresignedURL.go b/example/object/getByPresignedURL.go index 61cf05b..d8bae12 100644 --- a/example/object/getByPresignedURL.go +++ b/example/object/getByPresignedURL.go @@ -14,6 +14,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { ak := os.Getenv("COS_SECRETID") sk := os.Getenv("COS_SECRETKEY") @@ -38,22 +57,17 @@ func main() { // Normal header way to get object resp, err := c.Object.Get(ctx, name, nil) - if err != nil { - panic(err) - } + log_status(err) bs, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() // Get presigned presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, ak, sk, time.Hour, nil) - if err != nil { - panic(err) - } + log_status(err) + // Get object by presinged url resp2, err := http.Get(presignedURL.String()) - if err != nil { - panic(err) - } + log_status(err) bs2, _ := ioutil.ReadAll(resp2.Body) resp2.Body.Close() fmt.Printf("result2 is : %s\n", string(bs2)) diff --git a/example/object/head.go b/example/object/head.go index 87a37a8..e96b54d 100644 --- a/example/object/head.go +++ b/example/object/head.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "net/url" "os" @@ -11,6 +12,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -29,7 +49,5 @@ func main() { name := "test/hello.txt" _, err := c.Object.Head(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) } diff --git a/example/object/initiateMultipartUpload.go b/example/object/initiateMultipartUpload.go index a77501b..0eec7c6 100644 --- a/example/object/initiateMultipartUpload.go +++ b/example/object/initiateMultipartUpload.go @@ -13,6 +13,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -31,8 +50,6 @@ func main() { name := "test_multipart" + time.Now().Format(time.RFC3339) v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%s\n", v.UploadID) } diff --git a/example/object/listParts.go b/example/object/listParts.go index ecf8674..43edd59 100644 --- a/example/object/listParts.go +++ b/example/object/listParts.go @@ -14,11 +14,28 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult { v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%#v\n", v) return v } @@ -27,7 +44,7 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s b := make([]byte, blockSize) if _, err := rand.Read(b); err != nil { - panic(err) + log_status(err) } s := fmt.Sprintf("%X", b) f := strings.NewReader(s) @@ -35,9 +52,7 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s resp, err := c.Object.UploadPart( context.Background(), name, uploadID, n, f, nil, ) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%s\n", resp.Status) return resp.Header.Get("Etag") } @@ -73,7 +88,7 @@ func main() { // } v, _, err := c.Object.ListParts(ctx, name, uploadID, nil) if err != nil { - panic(err) + log_status(err) return } for _, p := range v.Parts { diff --git a/example/object/options.go b/example/object/options.go index 1e770de..7df27a7 100644 --- a/example/object/options.go +++ b/example/object/options.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "net/url" "os" @@ -11,6 +12,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -29,11 +49,9 @@ func main() { name := "test/hello.txt" opt := &cos.ObjectOptionsOptions{ - Origin: "http://www.qq.com", + Origin: "http://www.qq.com", AccessControlRequestMethod: "PUT", } _, err := c.Object.Options(context.Background(), name, opt) - if err != nil { - panic(err) - } + log_status(err) } diff --git a/example/object/presigned_url_with_token.go b/example/object/presigned_url_with_token.go new file mode 100644 index 0000000..200a815 --- /dev/null +++ b/example/object/presigned_url_with_token.go @@ -0,0 +1,44 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "net/url" + "os" + "time" + + "github.com/tencentyun/cos-go-sdk-v5" +) + +type URLToken struct { + SessionToken string `url:"x-cos-security-token,omitempty" header:"-"` +} + +func main() { + // 替换成您的临时密钥 + tak := os.Getenv("COS_SECRETID") + tsk := os.Getenv("COS_SECRETKEY") + token := &URLToken{ + SessionToken: "", + } + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") + b := &cos.BaseURL{BucketURL: u} + c := cos.NewClient(b, &http.Client{}) + + name := "exampleobject" + ctx := context.Background() + + // Get presigned + presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token) + if err != nil { + fmt.Printf("Error: %v\n", err) + return + } + // Get object by presinged url + _, err = http.Get(presignedURL.String()) + if err != nil { + fmt.Printf("Error: %v\n", err) + } + fmt.Println(presignedURL.String()) +} diff --git a/example/object/put.go b/example/object/put.go index 8fd5839..a9f97f3 100644 --- a/example/object/put.go +++ b/example/object/put.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "net/url" "os" "strings" @@ -12,8 +13,27 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { - u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} c := cos.NewClient(b, &http.Client{ Transport: &cos.AuthorizationTransport{ @@ -34,9 +54,7 @@ func main() { f := strings.NewReader("test") _, err := c.Object.Put(context.Background(), name, f, nil) - if err != nil { - panic(err) - } + log_status(err) // Case2 put object with the options name = "test/put_option.go" @@ -51,14 +69,9 @@ func main() { }, } _, err = c.Object.Put(context.Background(), name, f, opt) - if err != nil { - panic(err) - } + log_status(err) // Case3 put object by local file path _, err = c.Object.PutFromFile(context.Background(), name, "./test", nil) - if err != nil { - panic(err) - } - + log_status(err) } diff --git a/example/object/putACL.go b/example/object/putACL.go index e30d4ab..82c7ef4 100644 --- a/example/object/putACL.go +++ b/example/object/putACL.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "net/url" "os" @@ -11,6 +12,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -34,9 +54,7 @@ func main() { } name := "test/hello.txt" _, err := c.Object.PutACL(context.Background(), name, opt) - if err != nil { - panic(err) - } + log_status(err) // with body opt = &cos.ObjectPutACLOptions{ @@ -58,7 +76,5 @@ func main() { } _, err = c.Object.PutACL(context.Background(), name, opt) - if err != nil { - panic(err) - } + log_status(err) } diff --git a/example/object/restore.go b/example/object/restore.go index 984513c..0c58467 100644 --- a/example/object/restore.go +++ b/example/object/restore.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "net/http" "net/url" "os" @@ -10,6 +11,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -35,7 +55,5 @@ func main() { } name := "archivetest" _, err := c.Object.PostRestore(context.Background(), name, opt) - if err != nil { - panic(err) - } + log_status(err) } diff --git a/example/object/sse_c.go b/example/object/sse_c.go new file mode 100644 index 0000000..95499db --- /dev/null +++ b/example/object/sse_c.go @@ -0,0 +1,81 @@ +package main + +import ( + "context" + "errors" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "os" + "strings" + + "github.com/tencentyun/cos-go-sdk-v5" + "github.com/tencentyun/cos-go-sdk-v5/debug" +) + +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + +func main() { + u, _ := url.Parse("https://testcd-1259654469.cos.ap-chengdu.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, + // Notice when put a large file and set need the request body, might happend out of memory error. + RequestBody: false, + ResponseHeader: true, + ResponseBody: true, + }, + }, + }) + opt := &cos.ObjectPutOptions{ + ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{ + ContentType: "text/html", + XCosSSECustomerAglo: "AES256", + XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=", + XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==", + }, + ACLHeaderOptions: &cos.ACLHeaderOptions{}, + } + name := "PutFromGoWithSSE-C" + content := "Put Object From Go With SSE-C" + f := strings.NewReader(content) + _, err := c.Object.Put(context.Background(), name, f, opt) + log_status(err) + + getopt := &cos.ObjectGetOptions{ + XCosSSECustomerAglo: "AES256", + XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=", + XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==", + } + var resp *cos.Response + resp, err = c.Object.Get(context.Background(), name, getopt) + log_status(err) + + bodyBytes, _ := ioutil.ReadAll(resp.Body) + bodyContent := string(bodyBytes) + if bodyContent != content { + log_status(errors.New("Content inconsistency")) + } +} diff --git a/example/object/sse_cos.go b/example/object/sse_cos.go new file mode 100644 index 0000000..e558d68 --- /dev/null +++ b/example/object/sse_cos.go @@ -0,0 +1,75 @@ +package main + +import ( + "context" + "errors" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "os" + "strings" + + "github.com/tencentyun/cos-go-sdk-v5" + "github.com/tencentyun/cos-go-sdk-v5/debug" +) + +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + +func main() { + u, _ := url.Parse("https://test-1259654469.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, + // Notice when put a large file and set need the request body, might happend out of memory error. + RequestBody: false, + ResponseHeader: true, + ResponseBody: true, + }, + }, + }) + opt := &cos.ObjectPutOptions{ + ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{ + ContentType: "text/html", + XCosServerSideEncryption: "AES256", + }, + ACLHeaderOptions: &cos.ACLHeaderOptions{}, + } + name := "PutFromGoWithSSE-COS" + content := "Put Object From Go With SSE-COS" + f := strings.NewReader(content) + _, err := c.Object.Put(context.Background(), name, f, opt) + log_status(err) + + getopt := &cos.ObjectGetOptions{} + var resp *cos.Response + resp, err = c.Object.Get(context.Background(), name, getopt) + log_status(err) + + bodyBytes, _ := ioutil.ReadAll(resp.Body) + bodyContent := string(bodyBytes) + if bodyContent != content { + log_status(errors.New("Content inconsistency")) + } +} diff --git a/example/object/upload.go b/example/object/upload.go index 4f055d2..7894fda 100644 --- a/example/object/upload.go +++ b/example/object/upload.go @@ -12,6 +12,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -33,8 +52,6 @@ func main() { v, _, err := c.Object.Upload( context.Background(), "gomulput1G", "./test1G", nil, ) - if err != nil { - panic(err) - } + log_status(err) fmt.Println(v) } diff --git a/example/object/uploadFile.go b/example/object/uploadFile.go index ec4a73a..c2d3a12 100644 --- a/example/object/uploadFile.go +++ b/example/object/uploadFile.go @@ -13,6 +13,25 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func main() { u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} @@ -32,11 +51,13 @@ func main() { name := "test/uploadFile.go" f, err := os.Open(os.Args[0]) if err != nil { - panic(err) + log_status(err) + return } s, err := f.Stat() if err != nil { - panic(err) + log_status(err) + return } fmt.Println(s.Size()) opt := &cos.ObjectPutOptions{ @@ -47,7 +68,5 @@ func main() { //opt.ContentLength = int(s.Size()) _, err = c.Object.Put(context.Background(), name, f, opt) - if err != nil { - panic(err) - } + log_status(err) } diff --git a/example/object/uploadPart.go b/example/object/uploadPart.go index f4a837f..945bfe5 100644 --- a/example/object/uploadPart.go +++ b/example/object/uploadPart.go @@ -14,11 +14,28 @@ import ( "github.com/tencentyun/cos-go-sdk-v5/debug" ) +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("WARN: Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("ERROR: Code: %v\n", e.Code) + fmt.Printf("ERROR: Message: %v\n", e.Message) + fmt.Printf("ERROR: Resource: %v\n", e.Resource) + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Printf("ERROR: %v\n", err) + // ERROR + } +} + func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult { v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil) - if err != nil { - panic(err) - } + log_status(err) fmt.Printf("%#v\n", v) return v } @@ -47,7 +64,5 @@ func main() { _, err := c.Object.UploadPart( context.Background(), name, uploadID, 1, f, nil, ) - if err != nil { - panic(err) - } + log_status(err) } From bcc1ed2b8317cea8a32ab193ffad0a6482d63560 Mon Sep 17 00:00:00 2001 From: jojoliang Date: Tue, 19 May 2020 21:08:06 +0800 Subject: [PATCH 2/2] update version --- cos.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cos.go b/cos.go index d9a67b8..b20ee46 100644 --- a/cos.go +++ b/cos.go @@ -21,7 +21,7 @@ import ( const ( // Version current go sdk version - Version = "0.7.5" + Version = "0.7.6" userAgent = "cos-go-sdk-v5/" + Version contentTypeXML = "application/xml" defaultServiceBaseURL = "http://service.cos.myqcloud.com"