forked from surma/s3put
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3put.go
213 lines (195 loc) · 4.86 KB
/
s3put.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
package main
import (
"github.com/voxelbrain/goptions"
"io"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"log"
"mime"
"os"
"path/filepath"
"sync"
)
const (
VERSION = "1.0.1"
)
type Item struct {
Prefix string
Path string
os.FileInfo
}
var (
options = struct {
AccessKey string `goptions:"-k, --access-key, obligatory, description='AWS Access Key ID'"`
SecretKey string `goptions:"-s, --secret-key, obligatory, description='AWS Secret Access Key'"`
Region string `goptions:"-r, --region, description='API Region name (default: us-west-1)'"`
Bucket string `goptions:"-b, --bucket, obligatory, description='Bucket to push to'"`
Concurrency int `goptions:"-c, --concurrency, description='Number of coroutines (default: 10)'"`
goptions.Remainder
goptions.Verbs
Put struct {
Prefix string `goptions:"-p, --prefix, description='Prefix to prepend to the items'"`
} `goptions:"put"`
Get struct {
Prefix string `goptions:"-p, --prefix, description='Only get items starting with prefix'"`
} `goptions:"get"`
}{
Concurrency: 10,
Region: aws.USWest.Name,
}
)
func init() {
err := goptions.Parse(&options)
if err != nil || len(options.Remainder) <= 0 || len(options.Verbs) <= 0 {
if err != goptions.ErrHelpRequest && err != nil {
log.Printf("Error: %s", err)
}
goptions.PrintHelp()
os.Exit(1)
}
}
func main() {
auth := aws.Auth{
AccessKey: options.AccessKey,
SecretKey: options.SecretKey,
}
region, ok := aws.Regions[options.Region]
if !ok {
log.Fatalf("Invalid region name %s", options.Region)
}
s3i := s3.New(auth, region)
bucket := s3i.Bucket(options.Bucket)
switch options.Verbs {
case "put":
c := listLocalFiles(options.Remainder...)
putFiles(bucket, c)
case "get":
c := listBucketFiles(bucket)
getFiles(bucket, c)
}
}
func listLocalFiles(path ...string) <-chan *Item {
c := make(chan *Item)
go func() {
for _, prefix := range options.Remainder {
newprefix, err := filepath.Abs(prefix)
if err != nil {
log.Printf("Path %s could not be made absolute: %s. Skipping...", prefix, err)
continue
}
if fi, err := os.Stat(newprefix); err != nil || !fi.IsDir() {
if err != nil {
log.Printf("Could not stat %s: %s. Skipping...", newprefix, err)
} else if !fi.IsDir() {
c <- &Item{
Prefix: filepath.Dir(newprefix),
Path: newprefix,
FileInfo: fi,
}
}
continue
}
log.Printf("Traversing %s...", newprefix)
filepath.Walk(newprefix, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
c <- &Item{
Prefix: newprefix,
Path: path,
FileInfo: info,
}
return nil
})
}
close(c)
}()
return c
}
func listBucketFiles(bucket *s3.Bucket) <-chan *Item {
c := make(chan *Item)
go func() {
marker := ""
for {
resp, err := bucket.List(options.Get.Prefix, "", marker, 1000)
if err != nil {
log.Printf("Could not list items in bucket: %s", err)
}
for _, item := range resp.Contents {
c <- &Item{
Prefix: options.Get.Prefix,
Path: item.Key,
FileInfo: nil,
}
marker = item.Key
}
if !resp.IsTruncated {
break
}
}
close(c)
}()
return c
}
func putFiles(bucket *s3.Bucket, c <-chan *Item) {
var wg sync.WaitGroup
wg.Add(options.Concurrency)
for i := 0; i < options.Concurrency; i++ {
go func() {
for item := range c {
func() {
f, err := os.Open(item.Path)
if err != nil {
log.Printf("Pushing %s failed: %s", item.Path, err)
}
defer f.Close()
path := item.Path[len(item.Prefix)+1:]
err = bucket.PutReader(options.Put.Prefix+path, f, item.FileInfo.Size(), mime.TypeByExtension(filepath.Ext(item.Path)), s3.BucketOwnerFull)
if err != nil {
log.Printf("Uploading %s failed: %s", path, err)
return
}
log.Printf("Uploading %s done", path)
}()
}
wg.Done()
}()
}
wg.Wait()
}
func getFiles(bucket *s3.Bucket, c <-chan *Item) {
var wg sync.WaitGroup
wg.Add(options.Concurrency)
for i := 0; i < options.Concurrency; i++ {
go func() {
for item := range c {
func() {
itempath := item.Path[len(item.Prefix):]
dirname, fname := filepath.Split(itempath)
dirname = filepath.Join(options.Remainder[0], dirname)
err := os.MkdirAll(dirname, os.FileMode(0755))
if err != nil {
log.Printf("Could not create target folder %s: %s", dirname, err)
return
}
f, err := os.Create(filepath.Join(dirname, fname))
if err != nil {
log.Printf("Opening %s failed: %s", item.Path, err)
return
}
defer f.Close()
rc, err := bucket.GetReader(item.Path)
if err != nil {
log.Printf("Downloading %s failed: %s", item.Path, err)
return
}
defer rc.Close()
io.Copy(f, rc)
log.Printf("Downloading %s done", item.Path)
}()
}
wg.Done()
}()
}
wg.Wait()
}