add example
This commit is contained in:
4
ci.go
4
ci.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
"hash/crc64"
|
"hash/crc64"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"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
|
// 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) {
|
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 {
|
if err := CheckReaderLen(r); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
75
example/object/batchGet.go
Normal file
75
example/object/batchGet.go
Normal file
@@ -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
Normal file
73
example/object/batchUpload.go
Normal file
@@ -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
Normal file
62
example/object/directory.go
Normal file
@@ -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
Normal file
68
example/object/moveObject.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,12 +33,12 @@ func log_status(err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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}
|
b := &cos.BaseURL{BucketURL: u}
|
||||||
c := cos.NewClient(b, &http.Client{
|
c := cos.NewClient(b, &http.Client{
|
||||||
Transport: &cos.AuthorizationTransport{
|
Transport: &cos.AuthorizationTransport{
|
||||||
SecretID: os.Getenv("COS_Key"),
|
SecretID: os.Getenv("COS_SECRETID"),
|
||||||
SecretKey: os.Getenv("COS_Secret"),
|
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||||
Transport: &debug.DebugRequestTransport{
|
Transport: &debug.DebugRequestTransport{
|
||||||
RequestHeader: true,
|
RequestHeader: true,
|
||||||
RequestBody: false,
|
RequestBody: false,
|
||||||
@@ -49,7 +49,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
name := "test/uploadFile.go"
|
name := "test/uploadFile.go"
|
||||||
f, err := os.Open(os.Args[0])
|
f, err := os.Open("test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log_status(err)
|
log_status(err)
|
||||||
return
|
return
|
||||||
@@ -59,13 +59,12 @@ func main() {
|
|||||||
log_status(err)
|
log_status(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(s.Size())
|
|
||||||
opt := &cos.ObjectPutOptions{
|
opt := &cos.ObjectPutOptions{
|
||||||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
|
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)
|
_, err = c.Object.Put(context.Background(), name, f, opt)
|
||||||
log_status(err)
|
log_status(err)
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
opt := &cos.ObjectUploadPartOptions{
|
opt := &cos.ObjectUploadPartOptions{
|
||||||
Listener: &cos.DefaultProgressListener{},
|
Listener: &cos.DefaultProgressListener{},
|
||||||
ContentLength: int(stat.Size()),
|
ContentLength: stat.Size(),
|
||||||
}
|
}
|
||||||
resp, err := c.Object.UploadPart(
|
resp, err := c.Object.UploadPart(
|
||||||
context.Background(), name, uploadID, 1, fd, opt,
|
context.Background(), name, uploadID, 1, fd, opt,
|
||||||
|
|||||||
@@ -182,6 +182,9 @@ type ObjectPutOptions struct {
|
|||||||
//
|
//
|
||||||
// https://www.qcloud.com/document/product/436/7749
|
// https://www.qcloud.com/document/product/436/7749
|
||||||
func (s *ObjectService) Put(ctx context.Context, name string, r io.Reader, uopt *ObjectPutOptions) (*Response, error) {
|
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 {
|
if err := CheckReaderLen(r); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,9 @@ type ObjectUploadPartOptions struct {
|
|||||||
//
|
//
|
||||||
// https://www.qcloud.com/document/product/436/7750
|
// 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) {
|
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 {
|
if err := CheckReaderLen(r); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user