Switch reading from S3 to io.Copy from io.ReadFull (#4225)

* Switch reading from S3 to io.Copy from io.ReadFull

If the Content-Length header wasn't being sent back, the current
behavior could panic. It's unclear when it will not be sent; it appears
to be CORS dependent. But this works around it by not trying to
preallocate a buffer of a specific size and instead just read until EOF.

In addition I noticed that Close wasn't being called.
https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#GetObjectOutput
specifies that Body is an io.ReadCloser so I added a call to Close.

Fixes #4222

* Add some extra efficiency
This commit is contained in:
Jeff Mitchell 2018-03-30 12:42:48 -04:00 committed by GitHub
parent 0c433c2f15
commit 82b493adfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -188,16 +188,20 @@ func (s *S3Backend) Get(ctx context.Context, key string) (*physical.Entry, error
if resp == nil {
return nil, fmt.Errorf("got nil response from S3 but no error")
}
defer resp.Body.Close()
data := make([]byte, *resp.ContentLength)
_, err = io.ReadFull(resp.Body, data)
data := bytes.NewBuffer(nil)
if resp.ContentLength != nil {
data = bytes.NewBuffer(make([]byte, 0, *resp.ContentLength))
}
_, err = io.Copy(data, resp.Body)
if err != nil {
return nil, err
}
ent := &physical.Entry{
Key: key,
Value: data,
Value: data.Bytes(),
}
return ent, nil