diff --git a/cos/bucket.go b/cos/bucket.go index 65ccc0b..a534ddf 100644 --- a/cos/bucket.go +++ b/cos/bucket.go @@ -32,9 +32,13 @@ type ObjectSlice struct { // 获得云存储上文件信息 func (b *Bucket) HeadObject(ctx context.Context, object string) error { resq, err := b.conn.Do(ctx, "HEAD", b.Name, object, nil, nil, nil) - for k, v := range resq.Header { - value := fmt.Sprintf("%s", v) - fmt.Printf("%-18s: %s\n", k, strings.Replace(strings.Replace(value, "[", "", -1), "]", "", -1)) + if err == nil { + defer resq.Body.Close() + } else { + for k, v := range resq.Header { + value := fmt.Sprintf("%s", v) + fmt.Printf("%-18s: %s\n", k, strings.Replace(strings.Replace(value, "[", "", -1), "]", "", -1)) + } } return err @@ -42,7 +46,10 @@ func (b *Bucket) HeadObject(ctx context.Context, object string) error { // UploadObject 上传文件 func (b *Bucket) UploadObject(ctx context.Context, object string, content io.Reader, acl *AccessControl) error { - _, err := b.conn.Do(ctx, "PUT", b.Name, object, nil, acl.GenHead(), content) + res, err := b.conn.Do(ctx, "PUT", b.Name, object, nil, acl.GenHead(), content) + if err == nil { + defer res.Body.Close() + } return err } @@ -54,14 +61,20 @@ func (b *Bucket) CopyObject(ctx context.Context, src, dst string, acl *AccessCon "x-cos-source-url": srcURL, } - _, err := b.conn.Do(ctx, "PUT", b.Name, dst, nil, header, nil) + res, err := b.conn.Do(ctx, "PUT", b.Name, dst, nil, header, nil) + if err == nil { + defer res.Body.Close() + } return err } // DeleteObject delete object func (b *Bucket) DeleteObject(ctx context.Context, obj string) error { - _, err := b.conn.Do(ctx, "DELETE", b.Name, obj, nil, nil, nil) + res, err := b.conn.Do(ctx, "DELETE", b.Name, obj, nil, nil, nil) + if err == nil { + defer res.Body.Close() + } return err } @@ -90,10 +103,10 @@ func (b *Bucket) UploadObjectBySlice(ctx context.Context, dst, src string, taskN } fd, err := os.Open(src) - defer fd.Close() if err != nil { return err } + defer fd.Close() slices, err := b.PerformSliceUpload(ctx, dst, uploadID, fd, taskNum) if err != nil { @@ -114,6 +127,8 @@ func (b *Bucket) InitSliceUpload(ctx context.Context, obj string, headers map[st if err != nil { return "", err } + defer res.Body.Close() + imur := &InitiateMultipartUploadResult{} err = XMLDecode(res.Body, imur) if err != nil { @@ -145,7 +160,10 @@ func (b *Bucket) CompleteSliceUpload(ctx context.Context, dst, uploadID string, param := map[string]interface{}{ "uploadId": uploadID, } - _, err = b.conn.Do(ctx, "POST", b.Name, dst, param, nil, bytes.NewReader(cmuXML)) + res, err := b.conn.Do(ctx, "POST", b.Name, dst, param, nil, bytes.NewReader(cmuXML)) + if err == nil { + defer res.Body.Close() + } return err } @@ -210,6 +228,7 @@ func (b *Bucket) UploadSlice(ctx context.Context, uploadID, dst string, number i if err != nil { return FileError{"PUT数据错误:" + err.Error()} } + defer res.Body.Close() if strings.Trim(res.Header.Get("Etag"), "\"") != etag { return FileError{"cos-etag与文件MD5不匹配"} diff --git a/cos/client.go b/cos/client.go index 3c14123..393539b 100644 --- a/cos/client.go +++ b/cos/client.go @@ -51,6 +51,7 @@ func (c *Client) GetBucketList(ctx context.Context) (*ListAllMyBucketsResult, er if err != nil { return nil, err } + defer res.Body.Close() res, err = checkHTTPErr(res) if err != nil { @@ -68,7 +69,10 @@ func (c *Client) GetBucketList(ctx context.Context) (*ListAllMyBucketsResult, er // CreateBucket 建立bucket func (c *Client) CreateBucket(ctx context.Context, name string, acl *AccessControl) error { - _, err := c.conn.Do(ctx, "PUT", name, "", nil, acl.GenHead(), nil) + res, err := c.conn.Do(ctx, "PUT", name, "", nil, acl.GenHead(), nil) + if err == nil { + defer res.Body.Close() + } return err } @@ -88,6 +92,7 @@ func (c *Client) GetBucketACL(ctx context.Context, name string) (*AccessControlP if err != nil { return nil, err } + defer res.Body.Close() aclp := &AccessControlPolicy{} @@ -102,14 +107,20 @@ func (c *Client) GetBucketACL(ctx context.Context, name string) (*AccessControlP // SetBucketACL set bucket's acl func (c *Client) SetBucketACL(ctx context.Context, name string, acl *AccessControl) error { params := map[string]interface{}{"acl": ""} - _, err := c.conn.Do(ctx, "PUT", name, "", params, acl.GenHead(), nil) + res, err := c.conn.Do(ctx, "PUT", name, "", params, acl.GenHead(), nil) + if err == nil { + defer res.Body.Close() + } return err } // BucketExists bucket exists? func (c *Client) BucketExists(ctx context.Context, name string) error { - _, err := c.conn.Do(ctx, "HEAD", name, "", nil, nil, nil) + res, err := c.conn.Do(ctx, "HEAD", name, "", nil, nil, nil) + if err == nil { + defer res.Body.Close() + } return err } @@ -120,6 +131,7 @@ func (c *Client) ListBucketContents(ctx context.Context, name string, qc *QueryC if err != nil { return nil, err } + defer resp.Body.Close() lbr := &ListBucketResult{} err = XMLDecode(resp.Body, lbr) @@ -136,6 +148,7 @@ func (c *Client) ListUploading(ctx context.Context, bucket string, lu *ListUploa if err != nil { return nil, err } + defer res.Body.Close() lmur := &ListMultipartUploadsResult{} err = XMLDecode(res.Body, lmur) diff --git a/cos/conn.go b/cos/conn.go index c44cf96..7bef916 100644 --- a/cos/conn.go +++ b/cos/conn.go @@ -26,17 +26,20 @@ func (conn *Conn) Do(ctx context.Context, method, bucket, object string, params if err != nil { return nil, err } - //fmt.Println(url) + conn.signHeader(req, params, headers) req.Header.Set("User-Agent", conn.conf.UA) req.Header.Set("Content-Length", strconv.FormatInt(req.ContentLength, 10)) setHeader(req, headers) res, err := conn.c.Do(req) + if err != nil { return nil, err } + if res.StatusCode < 200 || res.StatusCode >= 300 { + defer res.Body.Close() return checkHTTPErr(res) }