Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Jul 9, 2017
2 parents 000df3a + 813fa4a commit a241753
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.6.0 (2017-07-09)

* 增加说明在某些情况下 ObjectPutHeaderOptions.ContentLength 必须要指定
* 增加 ObjectUploadPartOptions.ContentLength


## 0.5.0 (2017-06-28)

* 修复 ACL 相关 API 突然失效的问题.
Expand Down
4 changes: 2 additions & 2 deletions bucket_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func TestBucketService_PutACL_with_header_opt(t *testing.T) {
testFormValues(t, r, vs)
testHeader(t, r, "x-cos-acl", "private")

want := http.NoBody
v := r.Body
want := 0
v, _ := r.Body.Read([]byte{})
if !reflect.DeepEqual(v, want) {
t.Errorf("Bucket.PutACL request body: %#v, want %#v", v, want)
}
Expand Down
10 changes: 5 additions & 5 deletions cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"reflect"
"text/template"

"strconv"

"github.com/google/go-querystring/query"
"github.com/mozillazg/go-httpheader"
)
Expand Down Expand Up @@ -121,7 +123,6 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
var reader io.Reader
contentType := ""
contentMD5 := ""
contentLength := ""
xsha1 := ""
if body != nil {
// 上传文件
Expand All @@ -136,7 +137,6 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
reader = bytes.NewReader(b)
contentMD5 = base64.StdEncoding.EncodeToString(calMD5Digest(b))
//xsha1 = base64.StdEncoding.EncodeToString(calSHA1Digest(b))
contentLength = fmt.Sprintf("%d", len(b))
}
} else {
contentType = contentTypeXML
Expand All @@ -151,10 +151,10 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
if err != nil {
return
}

if contentLength != "" {
req.Header.Set("Content-Length", contentLength)
if v := req.Header.Get("Content-Length"); req.ContentLength == 0 && v != "" && v != "0" {
req.ContentLength, _ = strconv.ParseInt(v, 10, 64)
}

if contentMD5 != "" {
req.Header["Content-MD5"] = []string{contentMD5}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/bucket/putACL.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
{
Grantee: &cos.ACLGrantee{
Type: "RootAccount",
ID: "qcs::cam::uin/100000760461:uin/100000760461",
ID: "qcs::cam::uin/100000760461:uin/100000760461",
},

Permission: "FULL_CONTROL",
Expand Down
2 changes: 1 addition & 1 deletion examples/object/putACL.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {
{
Grantee: &cos.ACLGrantee{
Type: "RootAccount",
ID: "qcs::cam::uin/100000760461:uin/100000760461",
ID: "qcs::cam::uin/100000760461:uin/100000760461",
},

Permission: "FULL_CONTROL",
Expand Down
53 changes: 53 additions & 0 deletions examples/object/uploadFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"net/url"
"os"

"net/http"

"fmt"

"github.com/mozillazg/go-cos"
"github.com/mozillazg/go-cos/examples"
)

func main() {
u, _ := url.Parse("https://test-1253846586.cn-north.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: &examples.DebugRequestTransport{
RequestHeader: true,
RequestBody: false,
ResponseHeader: true,
ResponseBody: true,
},
},
})

name := "test/uploadFile.go"
f, err := os.Open(os.Args[0])
if err != nil {
panic(err)
}
s, err := f.Stat()
if err != nil {
panic(err)
}
fmt.Println(s.Size())
opt := &cos.ObjectPutOptions{
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
ContentLength: int(s.Size()),
},
}
//opt.ContentLength = int(s.Size())

_, err = c.Object.Put(context.Background(), name, f, opt)
if err != nil {
panic(err)
}
}
1 change: 1 addition & 0 deletions examples/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ echo '##### object ####'

run ./bucket/putCORS.go
run ./object/put.go
run ./object/uploadFile.go
run ./object/putACL.go
run ./object/append.go
run ./object/get.go
Expand Down
5 changes: 5 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ObjectPutHeaderOptions struct {
ContentDisposition string `header:"Content-Disposition,omitempty" url:"-"`
ContentEncoding string `header:"Content-Encoding,omitempty" url:"-"`
ContentType string `header:"Content-Type,omitempty" url:"-"`
ContentLength int `header:"Content-Length,omitempty" url:"-"`
Expect string `header:"Expect,omitempty" url:"-"`
Expires string `header:"Expires,omitempty" url:"-"`
XCosContentSHA1 string `header:"x-cos-content-sha1,omitempty" url:"-"`
Expand All @@ -66,6 +67,8 @@ type ObjectPutOptions struct {

// Put Object请求可以将一个文件(Oject)上传至指定Bucket。
//
// 当 r 不是 bytes.Buffer/bytes.Reader/strings.Reader 时,必须指定 opt.ObjectPutHeaderOptions.ContentLength
//
// https://www.qcloud.com/document/product/436/7749
func (s *ObjectService) Put(ctx context.Context, name string, r io.Reader, opt *ObjectPutOptions) (*Response, error) {
sendOpt := sendOptions{
Expand Down Expand Up @@ -146,6 +149,8 @@ func (s *ObjectService) Options(ctx context.Context, name string, opt *ObjectOpt
//
// Appendable的文件不可以被复制,不参与版本管理,不参与生命周期管理,不可跨区域复制。
//
// 当 r 不是 bytes.Buffer/bytes.Reader/strings.Reader 时,必须指定 opt.ObjectPutHeaderOptions.ContentLength
//
// https://www.qcloud.com/document/product/436/7741
func (s *ObjectService) Append(ctx context.Context, name string, position int, r io.Reader, opt *ObjectPutOptions) (*Response, error) {
u := fmt.Sprintf("/%s?append&position=%d", encodeURIComponent(name), position)
Expand Down
4 changes: 2 additions & 2 deletions object_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func TestObjectService_PutACL_with_header_opt(t *testing.T) {
testFormValues(t, r, vs)
testHeader(t, r, "x-cos-acl", "private")

want := http.NoBody
v := r.Body
want := 0
v, _ := r.Body.Read([]byte{})
if !reflect.DeepEqual(v, want) {
t.Errorf("Object.PutACL request body: %#v, want %#v", v, want)
}
Expand Down
3 changes: 3 additions & 0 deletions object_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (s *ObjectService) InitiateMultipartUpload(ctx context.Context, name string
type ObjectUploadPartOptions struct {
Expect string `header:"Expect,omitempty" url:"-"`
XCosContentSHA1 string `header:"x-cos-content-sha1" url:"-"`
ContentLength int `header:"Content-Length,omitempty" url:"-"`
}

// UploadPart ...
Expand All @@ -53,6 +54,8 @@ type ObjectUploadPartOptions struct {
//
// 当传入uploadID和partNumber都相同的时候,后传入的块将覆盖之前传入的块。当uploadID不存在时会返回404错误,NoSuchUpload.
//
// 当 r 不是 bytes.Buffer/bytes.Reader/strings.Reader 时,必须指定 opt.ContentLength
//
// https://www.qcloud.com/document/product/436/7750
func (s *ObjectService) UploadPart(ctx context.Context, name, uploadID string, partNumber int, r io.Reader, opt *ObjectUploadPartOptions) (*Response, error) {
u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
Expand Down

0 comments on commit a241753

Please sign in to comment.