Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from dreamCodeMan/dev
Browse files Browse the repository at this point in the history
修复bug,在请求签名认证时候没有对文件名做url编码,另外go语言中url.QueryEscape将空格编码为+,需要修改为%20,因此新增escape函数对字符进行编码处理
  • Loading branch information
nelsonken authored Feb 25, 2018
2 parents 0a1445a + 27b22ca commit 1f2ab50
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
3 changes: 1 addition & 2 deletions cos/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/hex"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -103,7 +102,7 @@ func getHeadStr(headers map[string]string) string {

tmp := []string{}
for k, v := range headers {
str := fmt.Sprintf("%s=%s", strings.ToLower(k), url.QueryEscape(v))
str := fmt.Sprintf("%s=%s", strings.ToLower(k), escape(v))
tmp = append(tmp, str)
}
sort.Strings(tmp)
Expand Down
37 changes: 27 additions & 10 deletions cos/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,24 @@ 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)
//fmt.Printf("%-18s: %d\n", "Content-Length", resq.ContentLength)
//fmt.Printf("%-18s: %s\n", "Content-Type", resq.c)
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
}

// 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
}
Expand All @@ -56,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
}
Expand All @@ -90,12 +101,12 @@ func (b *Bucket) UploadObjectBySlice(ctx context.Context, dst, src string, taskN
if err != nil {
return err
}
//fmt.Println(uploadID)

fd, err := os.Open(src)
if err != nil {
return err
}
defer fd.Close()

slices, err := b.PerformSliceUpload(ctx, dst, uploadID, fd, taskNum)
if err != nil {
Expand All @@ -116,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 {
Expand Down Expand Up @@ -147,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
}
Expand Down Expand Up @@ -212,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不匹配"}
Expand Down
19 changes: 16 additions & 3 deletions cos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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{}

Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cos/conf.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cos

const (
defaultPartSize = 60 * 1024 * 1024
defaultPartSize = 80 * 1024 * 1024
defaultRetryTimes = 3
defaultUA = "cos-go-sdk-v5.2.9"
defaultDomain = "myqcloud.com"
Expand Down
13 changes: 11 additions & 2 deletions cos/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
)
Expand All @@ -25,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)
}

Expand Down Expand Up @@ -66,11 +70,16 @@ func getQueryStr(params map[string]interface{}) string {

func (conn *Conn) buildURL(bucket, object, queryStr string) string {
domain := fmt.Sprintf("%s-%s.cos.%s.%s", bucket, conn.conf.AppID, conn.conf.Region, conn.conf.Domain)
url := fmt.Sprintf("http://%s/%s%s", domain, object, queryStr)
url := fmt.Sprintf("http://%s/%s%s", domain, escape(object), queryStr)

return url
}

func escape(str string) string {
//go语言中将空格编码为+,需要改为%20
return strings.Replace(url.QueryEscape(str), "+", "%20", -1)
}

func setHeader(req *http.Request, headers map[string]string) {
if headers == nil {
return
Expand Down

0 comments on commit 1f2ab50

Please sign in to comment.