Browse Source

add example

master
jojoliang 4 years ago
parent
commit
4cc93a9ebd
  1. 4
      ci.go
  2. 75
      example/object/batchGet.go
  3. 73
      example/object/batchUpload.go
  4. 62
      example/object/directory.go
  5. 68
      example/object/moveObject.go
  6. 13
      example/object/uploadFile.go
  7. 2
      example/object/uploadPart.go
  8. 3
      object.go
  9. 3
      object_part.go

4
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
}

75
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()
}

73
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()
}

62
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)
}

68
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)
}
}

13
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)

2
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,

3
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
}

3
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
}

Loading…
Cancel
Save