~andyxu/goamz/goamz

« back to all changes in this revision

Viewing changes to s3/s3.go

  • Committer: Andy Xu
  • Date: 2014-10-09 08:15:26 UTC
  • Revision ID: a@andyxu.cn-20141009081526-tmdgrgcusn84dhtm
fix the SignV4 method for S3: add x-amz-date and x-amz-content-sha256 in the header

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
import (
12
12
        "bytes"
 
13
        "crypto/sha256"
13
14
        "encoding/xml"
14
15
        "fmt"
15
16
        "io"
25
26
        "time"
26
27
)
27
28
 
28
 
const debug = false
 
29
const (
 
30
        debug                   = false
 
31
        ISO8601BasicFormat      = "20060102T150405Z"
 
32
        ISO8601BasicFormatShort = "20060102"
 
33
)
29
34
 
30
35
// The S3 type encapsulates operations with an S3 region.
31
36
type S3 struct {
159
164
// It is the caller's responsibility to call Close on rc when
160
165
// finished reading.
161
166
func (b *Bucket) GetReader(path string) (rc io.ReadCloser, err error) {
 
167
        headers := map[string][]string{
 
168
                "X-Amz-Content-Sha256": {fmt.Sprintf("%x", sha256.Sum256([]byte("")))},
 
169
                "X-Amz-Date":           {time.Now().In(time.UTC).Format(ISO8601BasicFormat)},
 
170
        }
162
171
        req := &request{
163
 
                bucket: b.Name,
164
 
                path:   path,
 
172
                bucket:  b.Name,
 
173
                path:    path,
 
174
                headers: headers,
165
175
        }
166
176
        err = b.S3.prepare(req)
167
177
        if err != nil {
191
201
// PutReader inserts an object into the S3 bucket by consuming data
192
202
// from r until EOF.
193
203
func (b *Bucket) PutReader(path string, r io.Reader, length int64, contType string, perm ACL) error {
 
204
        payloadBytes, err := ioutil.ReadAll(r)
 
205
        if err != nil {
 
206
                return err
 
207
        }
 
208
        r = ioutil.NopCloser(bytes.NewBuffer(payloadBytes))
 
209
 
194
210
        headers := map[string][]string{
195
 
                "Content-Length": {strconv.FormatInt(length, 10)},
196
 
                "Content-Type":   {contType},
197
 
                "x-amz-acl":      {string(perm)},
 
211
                "Content-Length":       {strconv.FormatInt(length, 10)},
 
212
                "Content-Type":         {contType},
 
213
                "X-Amz-Acl":            {string(perm)},
 
214
                "X-Amz-Content-Sha256": {fmt.Sprintf("%x", sha256.Sum256(payloadBytes))},
 
215
                "X-Amz-Date":           {time.Now().In(time.UTC).Format(ISO8601BasicFormat)},
198
216
        }
199
217
        req := &request{
200
218
                method:  "PUT",
455
473
        }
456
474
        req.headers["Host"] = []string{u.Host}
457
475
        req.headers["Date"] = []string{time.Now().In(time.UTC).Format(time.RFC1123)}
458
 
        sign(s3.Auth, req.method, req.signpath, req.params, req.headers)
459
476
        return nil
460
477
}
461
478
 
486
503
        if req.payload != nil {
487
504
                hreq.Body = ioutil.NopCloser(req.payload)
488
505
        }
 
506
        s3.Sign(&hreq, s3.Auth)
489
507
 
490
508
        hresp, err := http.DefaultClient.Do(&hreq)
491
509
        if err != nil {