Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add context support #313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@
}
```

### context包支持 (Context support 需要go1.7+版本)

```go
client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
if err != nil {
// HandleError(err)
}


bucket, err := client.Bucket("my-bucket")
if err != nil {
// HandleError(err)
}


ctx := context.WithTimeout(context.Background(), time.Second)
// context api 没有采用golang常见的申明风格 (因为需要支持go1.6以下版本)
// 这里参考http.Request的实现,详见https://github.com/aliyun/aliyun-oss-go-sdk/issues/100
err = bucket.WithContext(ctx).DeleteObject("my-object")
if err != nil {
// HandleError(err)
}
```

#### 其它
更多的示例程序,请参看OSS Go SDK安装路径(即GOPATH变量中的第一个路径)下的`src\github.com\aliyun\aliyun-oss-go-sdk\sample`,该目录下为示例程序,
或者参看`https://github.com/aliyun/aliyun-oss-go-sdk`下sample目录中的示例文件。
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@
}
```

### Context support (go1.7+)

```go
client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
if err != nil {
// HandleError(err)
}


bucket, err := client.Bucket("my-bucket")
if err != nil {
// HandleError(err)
}


ctx := context.WithTimeout(context.Background(), time.Second)
// context api is not the golang idiomatic way, instead we use the http.Request approach
// for more detail, please check https://github.com/aliyun/aliyun-oss-go-sdk/issues/100
err = bucket.WithContext(ctx).DeleteObject("my-object")
if err != nil {
// HandleError(err)
}
```





## Complete Example
More example projects can be found at 'src\github.com\aliyun\aliyun-oss-go-sdk\sample' under the installation path of the OSS Go SDK (the first path of the GOPATH variable). The directory contains example projects.
Or you can refer to the example objects in the sample directory under 'https://github.com/aliyun/aliyun-oss-go-sdk'.
Expand Down
32 changes: 1 addition & 31 deletions oss/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import (
"time"
)

// Bucket implements the operations of object.
type Bucket struct {
Client Client
BucketName string
}


// PutObject creates a new object and it will overwrite the original one if it exists already.
//
Expand Down Expand Up @@ -1218,32 +1214,6 @@ func (bucket Bucket) Do(method, objectName string, params map[string]interface{}
return bucket.do(method, objectName, params, options, data, listener)
}

// Private
func (bucket Bucket) do(method, objectName string, params map[string]interface{}, options []Option,
data io.Reader, listener ProgressListener) (*Response, error) {
headers := make(map[string]string)
err := handleOptions(headers, options)
if err != nil {
return nil, err
}

err = CheckBucketName(bucket.BucketName)
if len(bucket.BucketName) > 0 && err != nil {
return nil, err
}

resp, err := bucket.Client.Conn.Do(method, bucket.BucketName, objectName,
params, headers, data, 0, listener)

// get response header
respHeader, _ := FindOption(options, responseHeader, nil)
if respHeader != nil && resp != nil {
pRespHeader := respHeader.(*http.Header)
*pRespHeader = resp.Headers
}

return resp, err
}

func (bucket Bucket) doURL(method HTTPMethod, signedURL string, params map[string]interface{}, options []Option,
data io.Reader, listener ProgressListener) (*Response, error) {
Expand Down
38 changes: 38 additions & 0 deletions oss/bucket_1_6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// +build !go1.7

package oss

// Bucket implements the operations of object.
type Bucket struct {
Client Client
BucketName string
}


// Private
func (bucket Bucket) do(method, objectName string, params map[string]interface{}, options []Option,
data io.Reader, listener ProgressListener) (*Response, error) {
headers := make(map[string]string)
err := handleOptions(headers, options)
if err != nil {
return nil, err
}

err = CheckBucketName(bucket.BucketName)
if len(bucket.BucketName) > 0 && err != nil {
return nil, err
}

resp, err := bucket.Client.Conn.Do(method, bucket.BucketName, objectName,
params, headers, data, 0, listener)

// get response header
respHeader, _ := FindOption(options, responseHeader, nil)
if respHeader != nil && resp != nil {
pRespHeader := respHeader.(*http.Header)
*pRespHeader = resp.Headers
}

return resp, err
}

52 changes: 52 additions & 0 deletions oss/bucket_1_7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// +build go1.7

package oss

import (
"context"
"io"
"net/http"
)

// Bucket implements the operations of object.
type Bucket struct {
Client Client
BucketName string
ctx context.Context
}

// WithContext support go1.7
func (bucket Bucket)WithContext(ctx context.Context) Bucket {
b := bucket // do a copy
b.ctx = ctx
return b
}


// Private with ctx support
func (bucket Bucket) do(method, objectName string, params map[string]interface{}, options []Option,
data io.Reader, listener ProgressListener) (*Response, error) {
headers := make(map[string]string)
err := handleOptions(headers, options)
if err != nil {
return nil, err
}

err = CheckBucketName(bucket.BucketName)
if len(bucket.BucketName) > 0 && err != nil {
return nil, err
}

conn := bucket.Client.Conn.WithContext(bucket.ctx)
resp, err := conn.Do(method, bucket.BucketName, objectName,
params, headers, data, 0, listener)

// get response header
respHeader, _ := FindOption(options, responseHeader, nil)
if respHeader != nil && resp != nil {
pRespHeader := respHeader.(*http.Header)
*pRespHeader = resp.Headers
}

return resp, err
}
52 changes: 3 additions & 49 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"net"
Expand All @@ -20,13 +19,6 @@ import (
// Users use oss.New to create Client instance.
//
type (
// Client OSS client
Client struct {
Config *Config // OSS client configuration
Conn *Conn // Send HTTP request
HTTPClient *http.Client //http.Client to use - if nil will make its own
}

// ClientOption client option such as UseCname, Timeout, SecurityToken.
ClientOption func(*Client)
)
Expand Down Expand Up @@ -92,8 +84,8 @@ func (client Client) Bucket(bucketName string) (*Bucket, error) {
}

return &Bucket{
client,
bucketName,
Client: client,
BucketName: bucketName,
}, nil
}

Expand Down Expand Up @@ -1970,42 +1962,4 @@ func InsecureSkipVerify(enabled bool) ClientOption {
return func(client *Client) {
client.Config.InsecureSkipVerify = enabled
}
}

// Private
func (client Client) do(method, bucketName string, params map[string]interface{},
headers map[string]string, data io.Reader, options ...Option) (*Response, error) {
err := CheckBucketName(bucketName)
if len(bucketName) > 0 && err != nil {
return nil, err
}

// option headers
addHeaders := make(map[string]string)
err = handleOptions(addHeaders, options)
if err != nil {
return nil, err
}

// merge header
if headers == nil {
headers = make(map[string]string)
}

for k, v := range addHeaders {
if _, ok := headers[k]; !ok {
headers[k] = v
}
}

resp, err := client.Conn.Do(method, bucketName, "", params, headers, data, 0, nil)

// get response header
respHeader, _ := FindOption(options, responseHeader, nil)
if respHeader != nil {
pRespHeader := respHeader.(*http.Header)
*pRespHeader = resp.Headers
}

return resp, err
}
}
54 changes: 54 additions & 0 deletions oss/client_1_6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// +build !go1.7

package oss

import (
"net/http"
"io"
)

// Client OSS client
type Client struct {
Config *Config // OSS client configuration
Conn *Conn // Send HTTP request
HTTPClient *http.Client //http.Client to use - if nil will make its own
}


// Private
func (client Client) do(method, bucketName string, params map[string]interface{},
headers map[string]string, data io.Reader, options ...Option) (*Response, error) {
err := CheckBucketName(bucketName)
if len(bucketName) > 0 && err != nil {
return nil, err
}

// option headers
addHeaders := make(map[string]string)
err = handleOptions(addHeaders, options)
if err != nil {
return nil, err
}

// merge header
if headers == nil {
headers = make(map[string]string)
}

for k, v := range addHeaders {
if _, ok := headers[k]; !ok {
headers[k] = v
}
}

resp, err := client.Conn.Do(method, bucketName, "", params, headers, data, 0, nil)

// get response header
respHeader, _ := FindOption(options, responseHeader, nil)
if respHeader != nil {
pRespHeader := respHeader.(*http.Header)
*pRespHeader = resp.Headers
}

return resp, err
}
Loading