-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3cp.go
318 lines (283 loc) · 7.21 KB
/
s3cp.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"context"
"errors"
"fmt"
"github.com/amoghe/distillog"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/jessevdk/go-flags"
"io"
"net/url"
"os"
"s3cp/logger"
"strings"
"sync"
)
var Commit = "not set"
var Options struct {
Destination string `short:"d" long:"destination" description:"Setting this makes all the arguments at the end become sources for use with e.g. xargs"`
Version bool `short:"V" long:"version" description:"Print version"`
Verbose bool `short:"v" long:"verbose" description:"Verbose"`
AccessKey string `long:"access-key" description:"Access key"`
SecretKey string `long:"secret-key" description:"Secret key"`
WriteKeys bool `long:"write-keys" description:"Write access key and secret key to ~/.aws to avoid having it visible in /bin/ps aux"`
}
var bucketToHost = make(map[string]types.BucketLocationConstraint)
var mutex = &sync.RWMutex{}
func resolveBucket(ctx context.Context, client *s3.Client, bucket string) (region types.BucketLocationConstraint, err error) {
mutex.RLock()
region = bucketToHost[bucket]
mutex.RUnlock()
if region != "" {
return
}
mutex.Lock()
defer mutex.Unlock()
if region = bucketToHost[bucket]; region != "" {
return
}
response, err := client.GetBucketLocation(ctx, &s3.GetBucketLocationInput{Bucket: aws.String(bucket)})
if err == nil {
region = response.LocationConstraint
bucketToHost[bucket] = region
}
return
}
type uploadFunc func(filename string, data io.Reader) (err error)
var log distillog.Logger
func doEverything() (err error) {
ctx := context.Background()
args, err := flags.ParseArgs(&Options, os.Args)
if err != nil {
return
}
var minLogLevel logger.LogLevel
if Options.Verbose {
minLogLevel = logger.Debug
} else {
minLogLevel = logger.Info
}
log = logger.ConfigurableVerboseLogger{
ProxyLog: distillog.NewStdoutLogger("s3cp"),
MinLogLevel: minLogLevel,
}
if Options.Version {
log.Errorf("Compiled from %s", Commit)
os.Exit(0)
}
hasAccessKey := Options.AccessKey != ""
hasSecretKey := Options.SecretKey != ""
if hasAccessKey != hasSecretKey {
return errors.New("either none or both of --access-key and --secret-key")
}
if Options.WriteKeys {
if err = writeConfig(hasAccessKey); err != nil {
return
}
log.Infoln("Wrote config. Exiting")
os.Exit(0)
}
sources := args[1:]
if len(sources) == 0 {
return errors.New("not enough arguments")
}
var dest string
if "" == Options.Destination {
dest = sources[len(sources)-1]
sources = sources[:len(sources)-1]
} else {
dest = Options.Destination
}
if len(sources) == 0 {
return errors.New("not enough arguments")
}
cfg, err := config.LoadDefaultConfig(ctx, func(options *config.LoadOptions) error {
if hasAccessKey {
options.Credentials = credentials.NewStaticCredentialsProvider(Options.AccessKey, Options.SecretKey, "")
}
options.Region = "us-west-2"
return nil
})
if err != nil {
return
}
client := s3.NewFromConfig(cfg)
if len(sources) > 1 && !strings.HasSuffix(dest, "/") {
return errors.New(fmt.Sprintf("Must have a trailing slash (directory indicator) when copying multiple files"))
}
uploadFn, err := prepareDestinationFunction(dest, ctx, client)
if err != nil {
return
}
successful := true
var wg sync.WaitGroup
for _, source := range sources {
wg.Add(1)
go func(source string) {
defer wg.Done()
if strings.HasPrefix(source, "s3://") {
err = copyFromS3(source, ctx, client, uploadFn)
} else {
err = copyFromLocalFilesystem(source, uploadFn)
}
if err != nil {
successful = false
log.Errorf("Failed to copy '%s': %s", source, err)
} else {
log.Debugf("Copied '%s' successfully", source)
}
}(source)
}
wg.Wait()
if !successful {
os.Exit(1)
}
return
}
func copyFromLocalFilesystem(source string, uploadFn uploadFunc) (err error) {
file, err := os.Open(source)
if err != nil {
return
}
defer func(file *os.File) { _ = file.Close() }(file)
if err = uploadFn(source, file); err != nil {
return
}
err = file.Close()
return
}
func copyFromS3(source string, ctx context.Context, client *s3.Client, uploadFn uploadFunc) (err error) {
parsed, err := url.Parse(source)
if err != nil {
return
}
region, err := resolveBucket(ctx, client, parsed.Hostname())
if err != nil {
return
}
getObjectInput := &s3.GetObjectInput{
Bucket: aws.String(parsed.Hostname()),
Key: aws.String(strings.TrimPrefix(parsed.Path, "/")),
}
output, err := client.GetObject(
ctx,
getObjectInput,
func(options *s3.Options) {
options.Region = string(region)
},
)
if err != nil {
return
}
defer func(Body io.ReadCloser) { _ = Body.Close() }(output.Body)
if err = uploadFn(source, output.Body); err != nil {
return
}
err = output.Body.Close()
return
}
func prepareDestinationFunction(dest string, ctx context.Context, client *s3.Client) (result uploadFunc, err error) {
if strings.HasPrefix(dest, "s3://") {
var parsed *url.URL
parsed, err = url.Parse(dest)
if err != nil {
return
}
if parsed.Path == "" {
return nil, errors.New(fmt.Sprintf("no path in %s", dest))
}
result = func(filename string, data io.Reader) (err error) {
var key string
if strings.HasSuffix(dest, "/") {
key = parsed.Path + filename[strings.LastIndex(filename, "/")+1:]
} else {
key = parsed.Path
}
var region types.BucketLocationConstraint
region, err = resolveBucket(ctx, client, parsed.Hostname())
if err != nil {
return err
}
_, err = client.PutObject(
ctx,
&s3.PutObjectInput{
Bucket: aws.String(parsed.Hostname()),
Key: aws.String(strings.TrimPrefix(key, "/")),
Body: data,
},
func(options *s3.Options) {
options.Region = string(region)
},
)
return
}
} else {
result = func(filename string, data io.Reader) (err error) {
var path string
if strings.HasSuffix(dest, "/") {
path = dest + filename[strings.LastIndex(filename, "/")+1:]
} else {
path = dest
}
writeFile, err := os.Create(path)
if err != nil {
return
}
defer func(open *os.File) {
_ = open.Close()
}(writeFile)
_, err = io.Copy(writeFile, data)
return
}
}
return
}
func writeConfig(hasAccessKey bool) (err error) {
if !hasAccessKey {
return errors.New("writing keys but no keys set")
}
dir, err := os.UserHomeDir()
if err != nil {
return
}
dotAwsDir := dir + "/.aws"
if err = os.MkdirAll(dotAwsDir, 0600); err != nil {
return
}
file, err := os.Create(dotAwsDir + "/credentials")
if err != nil {
return
}
if err = file.Chmod(0600); err != nil {
return
}
if _, err = file.WriteString(fmt.Sprintf("[default]\naws_access_key_id = %s\naws_secret_access_key = %s\n", Options.AccessKey, Options.SecretKey)); err != nil {
return
}
if err = file.Close(); err != nil {
return
}
file, err = os.Create(dotAwsDir + "/config")
if err != nil {
return
}
if err = file.Chmod(0600); err != nil {
return
}
if _, err = file.WriteString("[default]\n"); err != nil {
return
}
err = file.Close()
return
}
func main() {
err := doEverything()
if err != nil {
log.Errorln(err)
os.Exit(1)
}
}