add example
This commit is contained in:
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() {
|
||||
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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user