Merge lp:~andyxu/goamz/goamz into lp:goamz

Proposed by AndyXu
Status: Needs review
Proposed branch: lp:~andyxu/goamz/goamz
Merge into: lp:goamz
Diff against target: 93 lines (+28/-7)
2 files modified
aws/sign.go (+3/-0)
s3/s3.go (+25/-7)
To merge this branch: bzr merge lp:~andyxu/goamz/goamz
Reviewer Review Type Date Requested Status
goamz maintainers Pending
Review via email: mp+237737@code.launchpad.net

Commit message

Fix SignV4 method for S3

Description of the change

The SignV4 method which is implemented in the last commit was not called by S3, therefore this library will not work for Amazon S3 if you choose to use SignV4 Algorithm.

The S3 object is already implemented as 'Signer' interface in the last commit, so in stead of using signing function inside S3 package, method 'Sign' is called now(line 82 and 90 in the diff).

Note that the 'x-amz-date' and 'x-amz-content-sha256' header are both necessary for GET and PUT (at least in mainland China) even though the manual says they are optional.

This commit is tested against a S3 instance in mainland China.

To post a comment you must log in.
Revision history for this message
AndyXu (andyxu) wrote :

All interfaces in the library were not touched at all, just fixed the internal logic(see line 82 and 90 in the diff.)

Revision history for this message
Dimiter Naydenov (dimitern) wrote :

@andyxu can you re-propose this against github.com/go-amz/amz ?

Unmerged revisions

50. By AndyXu

fix the SignV4 method for S3: add x-amz-date and x-amz-content-sha256 in the header

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'aws/sign.go'
2--- aws/sign.go 2014-08-12 16:50:47 +0000
3+++ aws/sign.go 2014-10-09 08:40:10 +0000
4@@ -243,6 +243,9 @@
5 // Returns a SHA256 checksum of the request body. Represented as a
6 // lowercase hexadecimal string.
7 func payloadHash(req *http.Request, hasher hasher) (string, error) {
8+ if req.Body == nil {
9+ return hasher([]byte("")), nil
10+ }
11 if b, err := ioutil.ReadAll(req.Body); err != nil {
12 return "", err
13 } else {
14
15=== modified file 's3/s3.go'
16--- s3/s3.go 2014-02-12 13:42:33 +0000
17+++ s3/s3.go 2014-10-09 08:40:10 +0000
18@@ -10,6 +10,7 @@
19
20 import (
21 "bytes"
22+ "crypto/sha256"
23 "encoding/xml"
24 "fmt"
25 "io"
26@@ -25,7 +26,11 @@
27 "time"
28 )
29
30-const debug = false
31+const (
32+ debug = false
33+ ISO8601BasicFormat = "20060102T150405Z"
34+ ISO8601BasicFormatShort = "20060102"
35+)
36
37 // The S3 type encapsulates operations with an S3 region.
38 type S3 struct {
39@@ -159,9 +164,14 @@
40 // It is the caller's responsibility to call Close on rc when
41 // finished reading.
42 func (b *Bucket) GetReader(path string) (rc io.ReadCloser, err error) {
43+ headers := map[string][]string{
44+ "X-Amz-Content-Sha256": {fmt.Sprintf("%x", sha256.Sum256([]byte("")))},
45+ "X-Amz-Date": {time.Now().In(time.UTC).Format(ISO8601BasicFormat)},
46+ }
47 req := &request{
48- bucket: b.Name,
49- path: path,
50+ bucket: b.Name,
51+ path: path,
52+ headers: headers,
53 }
54 err = b.S3.prepare(req)
55 if err != nil {
56@@ -191,10 +201,18 @@
57 // PutReader inserts an object into the S3 bucket by consuming data
58 // from r until EOF.
59 func (b *Bucket) PutReader(path string, r io.Reader, length int64, contType string, perm ACL) error {
60+ payloadBytes, err := ioutil.ReadAll(r)
61+ if err != nil {
62+ return err
63+ }
64+ r = ioutil.NopCloser(bytes.NewBuffer(payloadBytes))
65+
66 headers := map[string][]string{
67- "Content-Length": {strconv.FormatInt(length, 10)},
68- "Content-Type": {contType},
69- "x-amz-acl": {string(perm)},
70+ "Content-Length": {strconv.FormatInt(length, 10)},
71+ "Content-Type": {contType},
72+ "X-Amz-Acl": {string(perm)},
73+ "X-Amz-Content-Sha256": {fmt.Sprintf("%x", sha256.Sum256(payloadBytes))},
74+ "X-Amz-Date": {time.Now().In(time.UTC).Format(ISO8601BasicFormat)},
75 }
76 req := &request{
77 method: "PUT",
78@@ -455,7 +473,6 @@
79 }
80 req.headers["Host"] = []string{u.Host}
81 req.headers["Date"] = []string{time.Now().In(time.UTC).Format(time.RFC1123)}
82- sign(s3.Auth, req.method, req.signpath, req.params, req.headers)
83 return nil
84 }
85
86@@ -486,6 +503,7 @@
87 if req.payload != nil {
88 hreq.Body = ioutil.NopCloser(req.payload)
89 }
90+ s3.Sign(&hreq, s3.Auth)
91
92 hresp, err := http.DefaultClient.Do(&hreq)
93 if err != nil {

Subscribers

People subscribed via source and target branches

to all changes: