jojoliang
4 years ago
9 changed files with 295 additions and 8 deletions
-
4ci.go
-
75example/object/batchGet.go
-
73example/object/batchUpload.go
-
62example/object/directory.go
-
68example/object/moveObject.go
-
13example/object/uploadFile.go
-
2example/object/uploadPart.go
-
3object.go
-
3object_part.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() |
|||
} |
@ -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() |
|||
} |
@ -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) |
|||
} |
@ -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) |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue