From 4cc93a9ebd3649100363d59c40934bdf56250eb4 Mon Sep 17 00:00:00 2001 From: jojoliang Date: Wed, 31 Mar 2021 15:20:03 +0800 Subject: [PATCH] add example --- ci.go | 4 +++ example/object/batchGet.go | 75 +++++++++++++++++++++++++++++++++++++++++++ example/object/batchUpload.go | 73 +++++++++++++++++++++++++++++++++++++++++ example/object/directory.go | 62 +++++++++++++++++++++++++++++++++++ example/object/moveObject.go | 68 +++++++++++++++++++++++++++++++++++++++ example/object/uploadFile.go | 13 ++++---- example/object/uploadPart.go | 2 +- object.go | 3 ++ object_part.go | 3 ++ 9 files changed, 295 insertions(+), 8 deletions(-) create mode 100644 example/object/batchGet.go create mode 100644 example/object/batchUpload.go create mode 100644 example/object/directory.go create mode 100644 example/object/moveObject.go diff --git a/ci.go b/ci.go index 9516c8e..545ddce 100644 --- a/ci.go +++ b/ci.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "encoding/xml" + "fmt" "hash/crc64" "io" "net/http" @@ -207,6 +208,9 @@ func (s *CIService) GetVideoAuditingJob(ctx context.Context, jobid string) (*Get // ci put https://cloud.tencent.com/document/product/460/18147 func (s *CIService) Put(ctx context.Context, name string, r io.Reader, uopt *ObjectPutOptions) (*ImageProcessResult, *Response, error) { + if r == nil { + return nil, nil, fmt.Errorf("reader is nil") + } if err := CheckReaderLen(r); err != nil { return nil, nil, err } diff --git a/example/object/batchGet.go b/example/object/batchGet.go new file mode 100644 index 0000000..053bbd4 --- /dev/null +++ b/example/object/batchGet.go @@ -0,0 +1,75 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "net/url" + "os" + "path/filepath" + "sync" + + "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 upload(wg *sync.WaitGroup, c *cos.Client, keysCh <-chan string) { + defer wg.Done() + for key := range keysCh { + // 下载文件到当前目录 + _, filename := filepath.Split(key) + _, err := c.Object.GetToFile(context.Background(), key, filename, nil) + if err != nil { + log_status(err) + } + } +} +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: false, + }, + }, + }) + keysCh := make(chan string, 2) + keys := []string{"test/test1", "test/test2", "test/test3"} + var wg sync.WaitGroup + threadpool := 2 + for i := 0; i < threadpool; i++ { + wg.Add(1) + go upload(&wg, c, keysCh) + } + for _, key := range keys { + keysCh <- key + } + close(keysCh) + wg.Wait() +} diff --git a/example/object/batchUpload.go b/example/object/batchUpload.go new file mode 100644 index 0000000..3a36aa3 --- /dev/null +++ b/example/object/batchUpload.go @@ -0,0 +1,73 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "net/url" + "os" + "sync" + + "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 upload(wg *sync.WaitGroup, c *cos.Client, files <-chan string) { + defer wg.Done() + for file := range files { + name := "test/" + file + _, _, err := c.Object.Upload(context.Background(), name, file, nil) + if err != nil { + log_status(err) + } + } +} +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: false, + }, + }, + }) + filesCh := make(chan string, 2) + filePaths := []string{"test1", "test2", "test3"} + var wg sync.WaitGroup + threadpool := 2 + for i := 0; i < threadpool; i++ { + wg.Add(1) + go upload(&wg, c, filesCh) + } + for _, filePath := range filePaths { + filesCh <- filePath + } + close(filesCh) + wg.Wait() +} diff --git a/example/object/directory.go b/example/object/directory.go new file mode 100644 index 0000000..57a2af1 --- /dev/null +++ b/example/object/directory.go @@ -0,0 +1,62 @@ +package main + +import ( + "context" + "fmt" + "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: false, + }, + }, + }) + // 创建文件夹 + name := "example/" + _, err := c.Object.Put(context.Background(), name, strings.NewReader(""), nil) + log_status(err) + + // 查看文件夹是否存在 + _, err = c.Object.Head(context.Background(), name, nil) + log_status(err) + + // 删除文件夹 + _, err = c.Object.Delete(context.Background(), name) + log_status(err) +} diff --git a/example/object/moveObject.go b/example/object/moveObject.go new file mode 100644 index 0000000..b533d84 --- /dev/null +++ b/example/object/moveObject.go @@ -0,0 +1,68 @@ +package main + +import ( + "context" + "net/url" + "os" + "strings" + + "net/http" + + "fmt" + + "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, + }, + }, + }) + + source := "test/oldfile" + f := strings.NewReader("test") + + // 上传文件 + _, err := c.Object.Put(context.Background(), source, f, nil) + log_status(err) + + // 重命名 + dest := "test/newfile" + soruceURL := fmt.Sprintf("%s/%s", u.Host, source) + _, _, err := c.Object.Copy(context.Background(), dest, soruceURL, nil) + log_status(err) + if err == nil { + _, err = c.Object.Delete(context.Background(), source, nil) + log_status(err) + } +} diff --git a/example/object/uploadFile.go b/example/object/uploadFile.go index c2d3a12..6005220 100644 --- a/example/object/uploadFile.go +++ b/example/object/uploadFile.go @@ -33,12 +33,12 @@ func log_status(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{ - SecretID: os.Getenv("COS_Key"), - SecretKey: os.Getenv("COS_Secret"), + SecretID: os.Getenv("COS_SECRETID"), + SecretKey: os.Getenv("COS_SECRETKEY"), Transport: &debug.DebugRequestTransport{ RequestHeader: true, RequestBody: false, @@ -49,7 +49,7 @@ func main() { }) name := "test/uploadFile.go" - f, err := os.Open(os.Args[0]) + f, err := os.Open("test") if err != nil { log_status(err) return @@ -59,13 +59,12 @@ func main() { log_status(err) return } - fmt.Println(s.Size()) opt := &cos.ObjectPutOptions{ ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{ - ContentLength: int(s.Size()), + ContentLength: s.Size(), }, } - //opt.ContentLength = int(s.Size()) + //opt.ContentLength = s.Size() _, err = c.Object.Put(context.Background(), name, f, opt) log_status(err) diff --git a/example/object/uploadPart.go b/example/object/uploadPart.go index 66cefd4..efd3b9f 100644 --- a/example/object/uploadPart.go +++ b/example/object/uploadPart.go @@ -74,7 +74,7 @@ func main() { } opt := &cos.ObjectUploadPartOptions{ Listener: &cos.DefaultProgressListener{}, - ContentLength: int(stat.Size()), + ContentLength: stat.Size(), } resp, err := c.Object.UploadPart( context.Background(), name, uploadID, 1, fd, opt, diff --git a/object.go b/object.go index bf9ef66..281a035 100644 --- a/object.go +++ b/object.go @@ -182,6 +182,9 @@ type ObjectPutOptions struct { // // https://www.qcloud.com/document/product/436/7749 func (s *ObjectService) Put(ctx context.Context, name string, r io.Reader, uopt *ObjectPutOptions) (*Response, error) { + if r == nil { + return nil, fmt.Errorf("reader is nil") + } if err := CheckReaderLen(r); err != nil { return nil, err } diff --git a/object_part.go b/object_part.go index 2a6e5bf..3e04aed 100644 --- a/object_part.go +++ b/object_part.go @@ -70,6 +70,9 @@ type ObjectUploadPartOptions struct { // // https://www.qcloud.com/document/product/436/7750 func (s *ObjectService) UploadPart(ctx context.Context, name, uploadID string, partNumber int, r io.Reader, uopt *ObjectUploadPartOptions) (*Response, error) { + if r == nil { + return nil, fmt.Errorf("reader is nil") + } if err := CheckReaderLen(r); err != nil { return nil, err }