Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 24c6ec4

Browse files
committed
Merge pull request #453 from jrperritt/object-etag-fix
Object create: ETag bug fix
2 parents c2fa289 + d200ea3 commit 24c6ec4

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

openstack/objectstorage/v1/objects/requests.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package objects
22

33
import (
4+
"bytes"
45
"crypto/hmac"
56
"crypto/md5"
67
"crypto/sha1"
78
"fmt"
89
"io"
9-
"net/http"
1010
"strings"
1111
"time"
1212

@@ -212,25 +212,28 @@ func Create(c *gophercloud.ServiceClient, containerName, objectName string, cont
212212
url += query
213213
}
214214

215+
hash := md5.New()
216+
217+
contentBuffer := bytes.NewBuffer([]byte{})
218+
_, err := io.Copy(contentBuffer, io.TeeReader(content, hash))
219+
if err != nil {
220+
res.Err = err
221+
return res
222+
}
223+
224+
localChecksum := hash.Sum(nil)
225+
h["ETag"] = fmt.Sprintf("%x", localChecksum)
226+
215227
ropts := gophercloud.RequestOpts{
216-
RawBody: content,
228+
RawBody: strings.NewReader(contentBuffer.String()),
217229
MoreHeaders: h,
218230
}
219231

220-
doUpload := func() (*http.Response, error) {
232+
for i := 1; i <= 3; i++ {
221233
resp, err := c.Request("PUT", url, ropts)
222234
if resp != nil {
223235
res.Header = resp.Header
224236
}
225-
return resp, err
226-
}
227-
228-
hash := md5.New()
229-
io.Copy(hash, content)
230-
localChecksum := hash.Sum(nil)
231-
232-
for i := 1; i <= 3; i++ {
233-
resp, err := doUpload()
234237
if resp.Header.Get("ETag") == fmt.Sprintf("%x", localChecksum) {
235238
res.Err = err
236239
break

0 commit comments

Comments
 (0)