From 35642e50731639c982bf3302e1088fc7963ae9bb Mon Sep 17 00:00:00 2001 From: jojoliang Date: Wed, 25 Aug 2021 20:46:10 +0800 Subject: [PATCH] add put with transport --- example/object/put_with_transport.go | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 example/object/put_with_transport.go diff --git a/example/object/put_with_transport.go b/example/object/put_with_transport.go new file mode 100644 index 0000000..d5301c2 --- /dev/null +++ b/example/object/put_with_transport.go @@ -0,0 +1,65 @@ +package main + +import ( + "context" + "fmt" + "net" + "net/http" + "net/url" + "os" + "time" + + "github.com/tencentyun/cos-go-sdk-v5" +) + +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"), + // base on http.DefaultTransport + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + DualStack: true, + }).DialContext, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + // ResponseHeaderTimeout: 1 * time.Second, + // MaxIdleConnsPerHost: 100, + // MaxIdleConns: 100, + }, + }, + }) + + // Case1 上传对象 + name := "test/example" + // Case3 通过本地文件上传对象 + _, err := c.Object.PutFromFile(context.Background(), name, "./test", nil) // 请求的超时时间为 min{context超时时间, HTTP超时时间} + log_status(err) +}