forked from bep/s3deploy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
157 lines (124 loc) · 3.6 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright © 2018 Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package lib
import (
"context"
"errors"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
_ remoteStore = (*s3Store)(nil)
_ file = (*s3File)(nil)
)
type s3Store struct {
bucket string
r routes
svc *s3.S3
}
type s3File struct {
o *s3.Object
}
func (f *s3File) Key() string {
return *f.o.Key
}
func (f *s3File) ETag() string {
return *f.o.ETag
}
func (f *s3File) Size() int64 {
return *f.o.Size
}
func newRemoteStore(cfg Config) (remoteStore, error) {
var s *s3Store
creds, err := s.createCredentials(cfg)
if err != nil {
return nil, err
}
region := new(string)
if cfg.RegionName != "" {
region = &cfg.RegionName
}
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
// The region may be set in global config. See SharedConfigState.
Region: region,
// The credentials object to use when signing requests.
// Uses -key and -secret from command line if provided.
// Defaults to a chain of credential providers to search for
// credentials in environment variables, shared credential file,
// and EC2 Instance Roles.
Credentials: creds,
},
// This is the default in session.NewSession, but let us be explicit.
// The end user can override this with AWS_SDK_LOAD_CONFIG=1.
// See https://docs.aws.amazon.com/sdk-for-go/api/aws/session/#hdr-Sessions_from_Shared_Config
SharedConfigState: session.SharedConfigStateFromEnv,
})
if err != nil {
return nil, err
}
s = &s3Store{svc: s3.New(sess), bucket: cfg.BucketName, r: cfg.conf.Routes}
return s, nil
}
func (s *s3Store) FileMap(opts ...opOption) (map[string]file, error) {
m := make(map[string]file)
err := s.svc.ListObjectsPages(&s3.ListObjectsInput{
Bucket: aws.String(s.bucket),
}, func(res *s3.ListObjectsOutput, last bool) (shouldContinue bool) {
for _, o := range res.Contents {
m[*o.Key] = &s3File{o: o}
}
return true
})
if err != nil {
return nil, err
}
return m, nil
}
func (s *s3Store) Put(ctx context.Context, f localFile, opts ...opOption) error {
headers := f.Headers()
withHeaders := func(r *request.Request) {
for k, v := range headers {
r.HTTPRequest.Header.Add(k, v)
}
}
_, err := s.svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(f.Key()),
Body: f.Content(),
ACL: aws.String("public-read"),
ContentLength: aws.Int64(f.Size()),
}, withHeaders)
return err
}
func (s *s3Store) DeleteObjects(ctx context.Context, keys []string, opts ...opOption) error {
ids := make([]*s3.ObjectIdentifier, len(keys))
for i := 0; i < len(keys); i++ {
ids[i] = &s3.ObjectIdentifier{Key: aws.String(keys[i])}
}
_, err := s.svc.DeleteObjectsWithContext(ctx, &s3.DeleteObjectsInput{
Bucket: aws.String(s.bucket),
Delete: &s3.Delete{
Objects: ids,
},
})
return err
}
func (s *s3Store) createCredentials(cfg Config) (*credentials.Credentials, error) {
accessKey, secretKey := cfg.AccessKey, cfg.SecretKey
if accessKey != "" && secretKey != "" {
return credentials.NewStaticCredentials(accessKey, secretKey, os.Getenv("AWS_SESSION_TOKEN")), nil
}
if accessKey != "" || secretKey != "" {
// provided one but not both
return nil, errors.New("AWS key and secret are required")
}
// Use AWS default
return nil, nil
}