-
Notifications
You must be signed in to change notification settings - Fork 11
/
s3.go
107 lines (90 loc) · 2.78 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package storage
import (
"bytes"
"fmt"
"io/ioutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/pkg/errors"
)
var s3BucketURL = "https://%s.s3.amazonaws.com%s"
// S3Client provides a mockable subset of the S3 API
type S3Client interface {
HeadBucket(*s3.HeadBucketInput) (*s3.HeadBucketOutput, error)
GetObject(*s3.GetObjectInput) (*s3.GetObjectOutput, error)
PutObject(*s3.PutObjectInput) (*s3.PutObjectOutput, error)
}
// S3Options are options for an S3 client
type S3Options struct {
AWSAccessKeyID string
AWSSecretAccessKey string
Endpoint string
Region string
DisableSSL bool
ForcePathStyle bool
}
// NewS3Client creates a new S3 client
func NewS3Client(opts *S3Options) (S3Client, error) {
s3Session, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(opts.AWSAccessKeyID, opts.AWSSecretAccessKey, ""),
Endpoint: aws.String(opts.Endpoint),
Region: aws.String(opts.Region),
DisableSSL: aws.Bool(opts.DisableSSL),
S3ForcePathStyle: aws.Bool(opts.ForcePathStyle),
})
if err != nil {
return nil, err
}
return s3.New(s3Session), nil
}
type s3Storage struct {
client S3Client
bucket string
}
// NewS3 creates a new S3 storage service
func NewS3(client S3Client, bucket string) Storage {
return &s3Storage{client: client, bucket: bucket}
}
func (s *s3Storage) Name() string {
return "S3"
}
// Test tests whether our S3 client is properly configured
func (s *s3Storage) Test() error {
_, err := s.client.HeadBucket(&s3.HeadBucketInput{
Bucket: aws.String(s.bucket),
})
return err
}
func (s *s3Storage) Get(path string) (string, []byte, error) {
out, err := s.client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
})
if err != nil {
return "", nil, errors.Wrapf(err, "error getting S3 object")
}
contents, err := ioutil.ReadAll(out.Body)
if err != nil {
return "", nil, errors.Wrapf(err, "error reading S3 object")
}
return aws.StringValue(out.ContentType), contents, nil
}
// Put writes the passed in file to the bucket with the passed in content type
func (s *s3Storage) Put(path string, contentType string, contents []byte) (string, error) {
_, err := s.client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Body: bytes.NewReader(contents),
Key: aws.String(path),
ContentType: aws.String(contentType),
ACL: aws.String(s3.BucketCannedACLPublicRead),
})
if err != nil {
return "", errors.Wrapf(err, "error putting S3 object")
}
return s.url(path), nil
}
func (s *s3Storage) url(path string) string {
return fmt.Sprintf(s3BucketURL, s.bucket, path)
}