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: Read the entire reader before writing to bucket #7462

Merged
Merged
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
20 changes: 9 additions & 11 deletions pkg/cloud/buckets/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -116,24 +117,21 @@ func WriteBucketURL(u *url.URL, data io.Reader, timeout time.Duration) error {

// WriteBucket writes the data to a bucket URL and key of the for 's3://bucketName' and key 'foo/bar/whatnot.txt'
// with the given timeout
func WriteBucket(bucketURL string, key string, data io.Reader, timeout time.Duration) (err error) {
func WriteBucket(bucketURL string, key string, reader io.Reader, timeout time.Duration) (err error) {
ctx, _ := context.WithTimeout(context.Background(), timeout)
bucket, err := blob.Open(ctx, bucketURL)
if err != nil {
return errors.Wrapf(err, "failed to open bucket %s", bucketURL)
}
w, err := bucket.NewWriter(ctx, key, nil)
data, err := ioutil.ReadAll(reader)
if err != nil {
return errors.Wrapf(err, "failed to create key %s in bucket %s", key, bucketURL)
return errors.Wrapf(err, "failed to read data for key %s in bucket %s", key, bucketURL)
}
defer func() {
if e := w.Close(); e != nil && err == nil {
err = e
}
err = errors.Wrapf(err, "failed to write key %s in bucket %s", key, bucketURL)
}()
_, err = io.Copy(w, data)
return
err = bucket.WriteAll(ctx, key, data, nil)
if err != nil {
return errors.Wrapf(err, "failed to write key %s in bucket %s", key, bucketURL)
}
return nil
}

// SplitBucketURL splits the full bucket URL into the URL to open the bucket and the file name to refer to
Expand Down