/
main.go
247 lines (195 loc) · 4.85 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/BurntSushi/toml"
"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"
)
type config struct {
AccessKey string `toml:"access_key_id"`
SecretAcessKey string `toml:"secret_access_key"`
Buckets map[string]bucket
}
type bucket struct {
Region string
Name string
CNAME bool
}
type objectInfo struct {
body io.ReadSeeker
key string
contentType string
}
type result struct {
url string
err error
}
var (
private bool
timestamp bool
bucketName string
path string
)
func main() {
flag.BoolVar(&private, "p", false, "private upload")
flag.BoolVar(×tamp, "t", false, "add timestamp")
flag.StringVar(&bucketName, "bucket", "default", "bucket to upload")
flag.StringVar(&path, "path", "", "path prefix")
flag.Parse()
files := flag.Args()
if len(files) == 0 {
fmt.Fprintf(os.Stderr, "Usage: %s [-p] [-t] [-path <pathPrefix>] [-bucket <bucketName>] <filename>...\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
user, err := user.Current()
if err != nil {
exit(err)
}
var conf config
credentialsPath := filepath.Join(user.HomeDir, ".aws-credentials.toml")
if _, err := toml.DecodeFile(credentialsPath, &conf); err != nil {
exit(fmt.Errorf("invalid config file: %v", err))
}
if _, ok := conf.Buckets[bucketName]; !ok {
exit(fmt.Errorf("missing config for bucket: %v", bucketName))
}
awsConfig := aws.NewConfig().WithCredentials(credentials.NewStaticCredentials(conf.AccessKey, conf.SecretAcessKey, "")).WithRegion(conf.Buckets[bucketName].Region)
svc := s3.New(session.New(awsConfig))
results := make(chan result, len(files))
for _, file := range files {
go uploadFile(file, conf, svc, results)
}
for range files {
res := <-results
if res.err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("failed to upload object: %v", res.err))
} else {
fmt.Printf("uploaded %s\n", res.url)
}
}
close(results)
}
func uploadFile(file string, conf config, svc *s3.S3, results chan<- result) {
obj, err := newObjectInfo(file)
if err != nil {
results <- result{err: fmt.Errorf("unable to read file: %v", err)}
return
}
bname := conf.Buckets[bucketName].Name
params := &s3.PutObjectInput{
Bucket: &bname,
Key: &obj.key,
Body: obj.body,
ContentType: &obj.contentType,
}
if !private {
params.ACL = aws.String("public-read")
}
_, err = svc.PutObject(params)
if err != nil {
results <- result{err: err}
} else {
results <- result{url: buildOutputURL(obj, conf)}
}
}
func newObjectInfo(s string) (objectInfo, error) {
_, err := os.Stat(s)
if err == nil {
content, err := ioutil.ReadFile(s)
if err != nil {
return objectInfo{}, err
}
obj := buildObjectInfo(content, s)
return obj, nil
} else if os.IsNotExist(err) && isURL(s) {
fmt.Printf("%s is not a local file, will attempt to fetch it as an URL\n", s)
content, err := fetchRemoteContent(s)
if err != nil {
return objectInfo{}, err
}
obj := buildObjectInfo(content, s)
return obj, nil
}
return objectInfo{}, err
}
func buildKey(s string) string {
split := strings.Split(filepath.Base(s), ".")
var key string
if timestamp {
key = fmt.Sprintf("%s-%d", split[0], time.Now().Unix())
} else {
key = split[0]
}
if path != "" {
key = fmt.Sprintf("%s/%s", path, key)
}
if len(split) == 2 {
key += fmt.Sprintf(".%s", split[1])
}
return key
}
func buildObjectInfo(content []byte, s string) objectInfo {
return objectInfo{
body: bytes.NewReader(content),
key: buildKey(s),
contentType: mime.TypeByExtension(filepath.Ext(s)),
}
}
func fetchRemoteContent(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("not found: %s", url)
}
return ioutil.ReadAll(resp.Body)
}
func exit(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func isURL(str string) bool {
prefixes := []string{"http://", "https://"}
for _, prefix := range prefixes {
if strings.HasPrefix(str, prefix) {
return true
}
}
return false
}
func buildOutputURL(obj objectInfo, conf config) string {
var url string
bucket := conf.Buckets[bucketName]
if private {
url = fmt.Sprintf("https://s3-%s.amazonaws.com/%s/%s", bucket.Region, bucket.Name, obj.key)
} else {
url = buildPublicURL(obj, conf)
}
return url
}
func buildPublicURL(obj objectInfo, conf config) string {
var url string
bucket := conf.Buckets[bucketName]
if bucket.CNAME {
url = fmt.Sprintf("http://%s/%s", bucket.Name, obj.key)
} else {
url = fmt.Sprintf("http://%s.s3.amazonaws.com/%s", bucket.Name, obj.key)
}
return url
}