-
Notifications
You must be signed in to change notification settings - Fork 181
/
cp.go
90 lines (76 loc) · 1.71 KB
/
cp.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
package main
import (
"fmt"
"io"
"log"
"net/url"
"os"
"strings"
"github.com/rlmcpherson/s3gof3r"
)
type CpArg struct {
Source string `name:"source"`
Dest string `name:"dest"`
}
type cpOpts struct {
DataOpts
CommonOpts
CpArg `positional-args:"true" required:"true"`
UpOpts
}
var cp cpOpts
func (cp *cpOpts) Execute(args []string) (err error) {
k, err := getAWSKeys()
if err != nil {
return
}
conf := new(s3gof3r.Config)
*conf = *s3gof3r.DefaultConfig
s3 := s3gof3r.New(cp.EndPoint, k)
conf.Concurrency = cp.Concurrency
if cp.NoSSL {
conf.Scheme = "http"
}
conf.PartSize = cp.PartSize
conf.Md5Check = !cp.NoMd5
conf.NTry = cp.NTry
s3gof3r.SetLogger(os.Stderr, "", log.LstdFlags, cp.Debug)
src, err := func(src string) (io.ReadCloser, error) {
if !strings.HasPrefix(strings.ToLower(src), "s3") {
return os.Open(src)
}
u, err := url.ParseRequestURI(src)
if err != nil {
return nil, fmt.Errorf("parse error: %s", err)
}
r, _, err := s3.Bucket(u.Host).GetReader(u.Path, conf)
return r, err
}(cp.Source)
if err != nil {
return
}
defer checkClose(src, err)
dst, err := func(dst string) (io.WriteCloser, error) {
if !strings.HasPrefix(strings.ToLower(dst), "s3") {
return os.Create(dst)
}
u, err := url.ParseRequestURI(dst)
if err != nil {
return nil, fmt.Errorf("parse error: %s", err)
}
return s3.Bucket(u.Host).PutWriter(u.Path, ACL(cp.Header, cp.ACL), conf)
}(cp.Dest)
if err != nil {
return
}
defer checkClose(dst, err)
_, err = io.Copy(dst, src)
return
}
func init() {
cmd, err := parser.AddCommand("cp", "copy S3 objects", "copy S3 objects to or from S3 and local files", &cp)
if err != nil {
log.Fatal(err)
}
cmd.Aliases = []string{"copy"}
}