Skip to content

Commit

Permalink
update PresignedURL && add batchGet demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Nov 29, 2021
1 parent b5482a8 commit 4c06894
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
55 changes: 42 additions & 13 deletions example/object/batchGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"path"
"strings"
"sync"

"github.com/tencentyun/cos-go-sdk-v5"
Expand All @@ -32,11 +33,11 @@ func log_status(err error) {
}
}

func upload(wg *sync.WaitGroup, c *cos.Client, keysCh <-chan string) {
func download(wg *sync.WaitGroup, c *cos.Client, keysCh <-chan []string) {
defer wg.Done()
for key := range keysCh {
// 下载文件到当前目录
_, filename := filepath.Split(key)
for keys := range keysCh {
key := keys[0]
filename := keys[1]
_, err := c.Object.GetToFile(context.Background(), key, filename, nil)
if err != nil {
log_status(err)
Expand All @@ -51,24 +52,52 @@ func main() {
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
RequestHeader: false,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: false,
ResponseHeader: true,
ResponseHeader: false,
ResponseBody: false,
},
},
})
keysCh := make(chan string, 2)
keys := []string{"test/test1", "test/test2", "test/test3"}
// 多线程执行
keysCh := make(chan []string, 3)
var wg sync.WaitGroup
threadpool := 2
threadpool := 3
for i := 0; i < threadpool; i++ {
wg.Add(1)
go upload(&wg, c, keysCh)
go download(&wg, c, keysCh)
}
for _, key := range keys {
keysCh <- key
isTruncated := true
prefix := "dir" // 下载 dir 目录下所有文件
marker := ""
localDir := "./local/"
for isTruncated {
opt := &cos.BucketGetOptions{
Prefix: prefix,
Marker: marker,
EncodingType: "url", // url编码
}
// 列出目录
v, _, err := c.Bucket.Get(context.Background(), opt)
if err != nil {
fmt.Println(err)
break
}
for _, c := range v.Contents {
key, _ := cos.DecodeURIComponent(c.Key) //EncodingType: "url",先对 key 进行 url decode
localfile := localDir + key
if _, err := os.Stat(path.Dir(localfile)); err != nil && os.IsNotExist(err) {
os.MkdirAll(path.Dir(localfile), os.ModePerm)
}
// 目录不需要下载
if strings.HasSuffix(localfile, "/") {
continue
}
keysCh <- []string{key, localfile}
}
marker = v.NextMarker
isTruncated = v.IsTruncated
}
close(keysCh)
wg.Wait()
Expand Down
9 changes: 8 additions & 1 deletion object.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,16 @@ type PresignedURLOptions struct {
// GetPresignedURL get the object presigned to down or upload file by url
// 预签名函数,signHost: 默认签入Header Host, 您也可以选择不签入Header Host,但可能导致请求失败或安全漏洞
func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt interface{}, signHost ...bool) (*url.URL, error) {
// 兼容 name 以 / 开头的情况
if strings.HasPrefix(name, "/") {
name = encodeURIComponent("/") + encodeURIComponent(name[1:], []byte{'/'})
} else {
name = encodeURIComponent(name, []byte{'/'})
}

sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/" + encodeURIComponent(name, []byte{'/'}),
uri: "/" + name,
method: httpMethod,
optQuery: opt,
optHeader: opt,
Expand Down

0 comments on commit 4c06894

Please sign in to comment.