Browse Source

s3: close response body with copying object

pull/3570/head
chrislu 2 years ago
parent
commit
a54f30b6c6
  1. 3
      weed/s3api/s3api_object_copy_handlers.go
  2. 12
      weed/util/http_util.go

3
weed/s3api/s3api_object_copy_handlers.go

@ -174,11 +174,12 @@ func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Req
srcUrl := fmt.Sprintf("http://%s%s/%s%s", srcUrl := fmt.Sprintf("http://%s%s/%s%s",
s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject)) s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject))
dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, s3a.maybeGetFilerJwtAuthorizationToken(false), rangeHeader)
resp, dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, s3a.maybeGetFilerJwtAuthorizationToken(false), rangeHeader)
if err != nil { if err != nil {
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource) s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
return return
} }
defer util.CloseResponse(resp)
defer dataReader.Close() defer dataReader.Close()
glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl) glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)

12
weed/util/http_util.go

@ -370,11 +370,11 @@ func readEncryptedUrl(fileUrl string, cipherKey []byte, isContentCompressed bool
return false, nil return false, nil
} }
func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (io.ReadCloser, error) {
func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (*http.Response, io.ReadCloser, error) {
req, err := http.NewRequest("GET", fileUrl, nil) req, err := http.NewRequest("GET", fileUrl, nil)
if err != nil { if err != nil {
return nil, err
return nil, nil, err
} }
if rangeHeader != "" { if rangeHeader != "" {
req.Header.Add("Range", rangeHeader) req.Header.Add("Range", rangeHeader)
@ -388,10 +388,11 @@ func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (io.R
r, err := client.Do(req) r, err := client.Do(req)
if err != nil { if err != nil {
return nil, err
return nil, nil, err
} }
if r.StatusCode >= 400 { if r.StatusCode >= 400 {
return nil, fmt.Errorf("%s: %s", fileUrl, r.Status)
CloseResponse(r)
return nil, nil, fmt.Errorf("%s: %s", fileUrl, r.Status)
} }
var reader io.ReadCloser var reader io.ReadCloser
@ -399,12 +400,11 @@ func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (io.R
switch contentEncoding { switch contentEncoding {
case "gzip": case "gzip":
reader, err = gzip.NewReader(r.Body) reader, err = gzip.NewReader(r.Body)
defer reader.Close()
default: default:
reader = r.Body reader = r.Body
} }
return reader, nil
return r, reader, nil
} }
func CloseResponse(resp *http.Response) { func CloseResponse(resp *http.Response) {

Loading…
Cancel
Save