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

fix put object with content length #426

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
44 changes: 44 additions & 0 deletions oss/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6022,3 +6022,47 @@ func (s *OssBucketSuite) TestPutObjectWithCallbackResult(c *C) {
c.Assert(err, IsNil)
c.Assert(str, Equals, objectValue)
}

func (s *OssBucketSuite) TestPutObjectWithContentLength(c *C) {
objectName := objectNamePrefix + RandStr(8)
objectValue := RandStr(1023)
err := s.bucket.PutObject(objectName, strings.NewReader(objectValue), ContentLength(100))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), " transport connection broken"), Equals, true)

length := int64(len(objectValue))
err = s.bucket.PutObject(objectName, strings.NewReader(objectValue), ContentLength(length))
c.Assert(err, IsNil)

length = int64(100)
fd := strings.NewReader(objectValue)
err = s.bucket.PutObject(objectName, fd, ContentLength(length))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), " transport connection broken"), Equals, true)

fd2 := io.LimitReader(strings.NewReader(objectValue), length)
err = s.bucket.PutObject(objectName, fd2, ContentLength(length))
c.Assert(err, IsNil)

client, err := New(endpoint, accessID, accessKey, EnableMD5(true), EnableCRC(true))
c.Assert(err, IsNil)
s.client = client

err = s.bucket.PutObject(objectName, strings.NewReader(objectValue), ContentLength(100))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), " transport connection broken"), Equals, true)

length = int64(len(objectValue))
err = s.bucket.PutObject(objectName, strings.NewReader(objectValue), ContentLength(length))
c.Assert(err, IsNil)

length = int64(100)
fd = strings.NewReader(objectValue)
err = s.bucket.PutObject(objectName, fd, ContentLength(length))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), " transport connection broken"), Equals, true)

fd2 = io.LimitReader(strings.NewReader(objectValue), length)
err = s.bucket.PutObject(objectName, fd2, ContentLength(length))
c.Assert(err, IsNil)
}
27 changes: 15 additions & 12 deletions oss/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ func (conn Conn) doRequest(ctx context.Context, method string, uri *url.URL, can
if ctx != nil {
req = req.WithContext(ctx)
}
if headers != nil {
for k, v := range headers {
req.Header.Set(k, v)
}
}
tracker := &readerTracker{completedBytes: 0}
fd, crc := conn.handleBody(req, data, initCRC, listener, tracker)
if fd != nil {
Expand Down Expand Up @@ -342,13 +347,6 @@ func (conn Conn) doRequest(ctx context.Context, method string, uri *url.URL, can
if akIf.GetSecurityToken() != "" {
req.Header.Set(HTTPHeaderOssSecurityToken, akIf.GetSecurityToken())
}

if headers != nil {
for k, v := range headers {
req.Header.Set(k, v)
}
}

conn.signHeader(req, canonicalizedResource)

// Transfer started
Expand Down Expand Up @@ -461,12 +459,17 @@ func (conn Conn) handleBody(req *http.Request, body io.Reader, initCRC uint64,
var file *os.File
var crc hash.Hash64
reader := body
readerLen, err := GetReaderLen(reader)
if err == nil {
req.ContentLength = readerLen
contentLength := req.Header.Get(HTTPHeaderContentLength)
if contentLength != "" {
length, _ := strconv.ParseInt(contentLength, 10, 64)
req.ContentLength = length
} else {
readerLen, err := GetReaderLen(reader)
if err == nil {
req.ContentLength = readerLen
}
req.Header.Set(HTTPHeaderContentLength, strconv.FormatInt(req.ContentLength, 10))
}
req.Header.Set(HTTPHeaderContentLength, strconv.FormatInt(req.ContentLength, 10))

// MD5
if body != nil && conn.config.IsEnableMD5 && req.Header.Get(HTTPHeaderContentMD5) == "" {
md5 := ""
Expand Down
12 changes: 9 additions & 3 deletions oss/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,12 @@ func GetReaderLen(reader io.Reader) (int64, error) {
case *os.File:
fInfo, fError := v.Stat()
if fError != nil {
err = fmt.Errorf("can't get reader content length,%s", fError.Error())
} else {
contentLength = fInfo.Size()
err = fmt.Errorf("can't get reader content length: %s", fError.Error())
}
if !isRegularFile(fInfo) {
err = fmt.Errorf("can't get reader content length: not a regular file")
}
contentLength = fInfo.Size()
case *io.LimitedReader:
contentLength = int64(v.N)
case *LimitedReadCloser:
Expand All @@ -476,6 +478,10 @@ func GetReaderLen(reader io.Reader) (int64, error) {
return contentLength, err
}

func isRegularFile(fInfo os.FileInfo) bool {
return fInfo.Mode()&os.ModeType == 0
}

func LimitReadCloser(r io.Reader, n int64) io.Reader {
var lc LimitedReadCloser
lc.R = r
Expand Down