/
main.go
82 lines (73 loc) · 1.63 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
// Command s3cp copies a file to or from Amazon S3.
//
// Usage:
//
// s3cp file url
// s3cp url file
//
// The file does not need to be seekable or stat-able. You can use s3cp to
// upload data of indeterminate length, such as from a pipe.
//
// Examples:
// $ s3cp file.txt https://mybucket.s3.amazonaws.com/file.txt
// $ gendata | s3cp /dev/stdin https://mybucket.s3.amazonaws.com/log
// $ s3cp https://mybucket.s3.amazonaws.com/image.jpg pic.jpg
//
// Environment:
//
// S3_ACCESS_KEY – an AWS Access Key Id (required)
//
// S3_SECRET_KEY – an AWS Secret Access Key (required)
package main
import (
"fmt"
"github.com/kr/s3/s3util"
"io"
"os"
"strings"
)
func main() {
s3util.DefaultConfig.AccessKey = os.Getenv("S3_ACCESS_KEY")
s3util.DefaultConfig.SecretKey = os.Getenv("S3_SECRET_KEY")
args := os.Args[1:]
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "Usage: s3cp file url")
fmt.Fprintln(os.Stderr, " s3cp url file")
os.Exit(1)
}
r, err := open(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
w, err := create(args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
_, err = io.Copy(w, r)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
err = w.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func open(s string) (io.ReadCloser, error) {
if isURL(s) {
return s3util.Open(s, nil)
}
return os.Open(s)
}
func create(s string) (io.WriteCloser, error) {
if isURL(s) {
return s3util.Create(s, nil, nil)
}
return os.Create(s)
}
func isURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}