From dc08e4098ffbb3d7d95c9decac7407e67349baa9 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 8 Apr 2020 09:13:26 -0700 Subject: [PATCH] add etag only for PUT or large chunked uploads --- weed/filer2/filechunks.go | 4 ++-- weed/server/filer_server_handlers_write.go | 13 ++++++++++--- weed/server/volume_server_handlers_write.go | 8 ++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/weed/filer2/filechunks.go b/weed/filer2/filechunks.go index e8e4c305b..48eaeea27 100644 --- a/weed/filer2/filechunks.go +++ b/weed/filer2/filechunks.go @@ -22,14 +22,14 @@ func TotalSize(chunks []*filer_pb.FileChunk) (size uint64) { func ETag(entry *filer_pb.Entry) (etag string) { if entry.Attributes == nil || entry.Attributes.Md5 == nil { - ETagChunks(entry.Chunks) + return ETagChunks(entry.Chunks) } return fmt.Sprintf("%x", entry.Attributes.Md5) } func ETagEntry(entry *Entry) (etag string) { if entry.Attr.Md5 == nil { - ETagChunks(entry.Chunks) + return ETagChunks(entry.Chunks) } return fmt.Sprintf("%x", entry.Attr.Md5) } diff --git a/weed/server/filer_server_handlers_write.go b/weed/server/filer_server_handlers_write.go index 33e6ee043..ce184875c 100644 --- a/weed/server/filer_server_handlers_write.go +++ b/weed/server/filer_server_handlers_write.go @@ -220,8 +220,13 @@ func (fs *FilerServer) uploadToVolumeServer(r *http.Request, u *url.URL, auth se defer func() { stats.FilerRequestHistogram.WithLabelValues("postUpload").Observe(time.Since(start).Seconds()) }() ret = &operation.UploadResult{} + md5Hash := md5.New() - var body = ioutil.NopCloser(io.TeeReader(r.Body, md5Hash)) + body := r.Body + if r.Method == "PUT" { + // only PUT or large chunked files has Md5 in attributes + body = ioutil.NopCloser(io.TeeReader(r.Body, md5Hash)) + } request := &http.Request{ Method: r.Method, @@ -286,8 +291,10 @@ func (fs *FilerServer) uploadToVolumeServer(r *http.Request, u *url.URL, auth se } } // use filer calculated md5 ETag, instead of the volume server crc ETag - md5value = md5Hash.Sum(nil) - ret.ETag = fmt.Sprintf("%x", md5value) + if r.Method == "PUT" { + md5value = md5Hash.Sum(nil) + } + ret.ETag = getEtag(resp) return } diff --git a/weed/server/volume_server_handlers_write.go b/weed/server/volume_server_handlers_write.go index 3a71a1332..968ece11f 100644 --- a/weed/server/volume_server_handlers_write.go +++ b/weed/server/volume_server_handlers_write.go @@ -166,3 +166,11 @@ func setEtag(w http.ResponseWriter, etag string) { } } } + +func getEtag(resp *http.Response) (etag string){ + etag = resp.Header.Get("ETag") + if strings.HasPrefix(etag, "\"") && strings.HasSuffix(etag, "\""){ + return etag[1:len(etag)-1] + } + return +}