-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3urlupload.go
165 lines (132 loc) · 2.52 KB
/
s3urlupload.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
package s3urlupload
import (
"errors"
"io"
"net/http"
"strings"
"sync"
"github.com/rlmcpherson/s3gof3r"
)
type Config struct {
AwsAccessKey string
AwsSecretKey string
AwsS3Endpoint string
AwsS3Bucket string
Workers uint
GetFilePath func(string) string
}
func Init(c Config) *S3UrlUpload {
if c.Workers == 0 {
c.Workers = 1
}
if c.GetFilePath == nil {
c.GetFilePath = func(url string) string {
parts := strings.Split(url, "/")
return parts[len(parts)-1]
}
}
s3 := s3gof3r.New(c.AwsS3Endpoint, s3gof3r.Keys{
AccessKey: c.AwsAccessKey,
SecretKey: c.AwsSecretKey,
})
b := s3.Bucket(c.AwsS3Bucket)
return &S3UrlUpload{
config: &c,
bucket: b,
}
}
type download struct {
Body io.ReadCloser
URL string
Name string
Error error
}
type Result struct {
URL string
Error error
}
type S3UrlUpload struct {
config *Config
bucket *s3gof3r.Bucket
}
func (s3uu *S3UrlUpload) Run(files ...string) <-chan Result {
count := len(files)
jobs := make(chan string, count)
results := make(chan Result, count)
workers := int(s3uu.config.Workers)
var wg sync.WaitGroup
wg.Add(workers)
for w := 1; w <= workers; w++ {
go s3uu.worker(jobs, results, &wg)
}
for _, f := range files {
jobs <- f
}
close(jobs)
go func() {
wg.Wait()
close(results)
}()
return results
}
func (s3uu *S3UrlUpload) download(url string) <-chan download {
out := make(chan download)
go func() {
defer close(out)
d := download{
URL: url,
Name: s3uu.config.GetFilePath(url),
}
resp, err := http.Get(url)
if err != nil {
d.Error = err
out <- d
return
}
if resp.Status != "200 OK" {
d.Error = errors.New("Status was not OK")
out <- d
return
}
d.Body = resp.Body
out <- d
}()
return out
}
func (s3uu *S3UrlUpload) upload(in <-chan download) <-chan Result {
var doUpload = func(d download) error {
defer d.Body.Close()
w, err := s3uu.bucket.PutWriter(d.Name, nil, nil)
if err != nil {
return err
}
defer w.Close()
_, err = io.Copy(w, d.Body)
if err != nil {
return err
}
return nil
}
out := make(chan Result)
go func() {
defer close(out)
for d := range in {
result := Result{URL: d.URL}
if d.Error != nil {
result.Error = d.Error
out <- result
continue
}
result.Error = doUpload(d)
out <- result
}
}()
return out
}
func (s3uu *S3UrlUpload) worker(jobs <-chan string, results chan<- Result, wg *sync.WaitGroup) {
for j := range jobs {
result := <-s3uu.upload(s3uu.download(j))
results <- result
}
wg.Done()
}