Skip to content

Commit

Permalink
upload part 支持上传0字节
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Jul 3, 2024
1 parent 0f07c52 commit ee70042
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
20 changes: 12 additions & 8 deletions object_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ type ObjectUploadPartOptions struct {
//
// https://www.qcloud.com/document/product/436/7750
func (s *ObjectService) UploadPart(ctx context.Context, name, uploadID string, partNumber int, r io.Reader, uopt *ObjectUploadPartOptions) (*Response, error) {
if r == nil {
return nil, fmt.Errorf("reader is nil")
if (r == nil || r == http.NoBody) && uopt != nil && uopt.ContentLength != 0 {
return nil, fmt.Errorf("ContentLength must be 0 when reader is nil or http.NoBody.")
}
if err := CheckReaderLen(r); err != nil {
return nil, err
Expand Down Expand Up @@ -125,12 +125,16 @@ func (s *ObjectService) UploadPart(ctx context.Context, name, uploadID string, p
}
retryErr := &RetryError{}
for nr := 0; nr < count; nr++ {
reader := TeeReader(r, nil, totalBytes, nil)
if s.client.Conf.EnableCRC {
reader.writer = crc64.New(crc64.MakeTable(crc64.ECMA))
}
if opt != nil && opt.Listener != nil {
reader.listener = opt.Listener
var reader io.Reader
if r != nil && r != http.NoBody {
tReader := TeeReader(r, nil, totalBytes, nil)
if s.client.Conf.EnableCRC {
tReader.writer = crc64.New(crc64.MakeTable(crc64.ECMA))
}
if opt != nil && opt.Listener != nil {
tReader.listener = opt.Listener
}
reader = tReader
}
u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
sendOpt := sendOptions{
Expand Down
46 changes: 46 additions & 0 deletions object_part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,52 @@ func TestObjectService_UploadPart(t *testing.T) {

}

func TestObjectService_UploadPartWithEmptyData(t *testing.T) {
setup()
defer teardown()

name := "test/hello.txt"
uploadID := "xxxxx"
partNumber := 1

mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
vs := values{
"uploadId": uploadID,
"partNumber": "1",
}
testFormValues(t, r, vs)
if r.Header.Get("Content-Length") != "0" {
t.Fatalf("UploadPart Content-Length is not 0")
}
b, _ := ioutil.ReadAll(r.Body)
if len(b) != 0 {
t.Fatalf("data is not empty")
}
})

_, err := client.Object.UploadPart(context.Background(),
name, uploadID, partNumber, nil, nil)
if err != nil {
t.Fatalf("Object.UploadPart returned error: %v", err)
}

_, err = client.Object.UploadPart(context.Background(),
name, uploadID, partNumber, http.NoBody, nil)
if err != nil {
t.Fatalf("Object.UploadPart returned error: %v", err)
}

_, err = client.Object.UploadPart(context.Background(),
name, uploadID, partNumber, http.NoBody, &ObjectUploadPartOptions{
ContentLength: 1,
})
if err == nil || err.Error() != "ContentLength must be 0 when reader is nil or http.NoBody." {
t.Fatalf("Object.UploadPart returned error: %v", err)
}

}

func TestObjectService_ListUploads(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit ee70042

Please sign in to comment.