-
Notifications
You must be signed in to change notification settings - Fork 32
/
s3.go
56 lines (46 loc) · 1.18 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package store
import (
"net/http"
"time"
"golang.org/x/net/context"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
)
// S3Client implements Client and handles fetching Files from S3 buckets.
type S3Client struct {
bucket string
auth aws.Auth
region aws.Region
}
// NewS3Client creates a new Client implementation which will proxy filesystem calls to an
// S3 bucket using the given authentication and region information.
func NewS3Client(bucket string, auth aws.Auth, region aws.Region) *S3Client {
return &S3Client{
bucket: bucket,
auth: auth,
region: region,
}
}
// Get implements Client.
func (c *S3Client) Get(ctx context.Context, path string) (*File, error) {
s3 := s3.New(c.auth, c.region)
b := s3.Bucket(c.bucket)
k, err := b.GetKey(path)
if err != nil {
return nil, err
}
rc, err := b.GetReader(path)
if err != nil {
return nil, err
}
modTime, _ := time.Parse(http.TimeFormat, k.LastModified)
return &File{
ReadCloser: rc,
Name: k.Key,
ModTime: modTime,
Size: k.Size,
}, nil
}