-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
216 lines (190 loc) · 4.16 KB
/
upload.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
package main
import (
"bytes"
"flag"
"fmt"
"github.com/cupcake/goamz/aws"
"github.com/cupcake/goamz/s3"
"io"
"os"
"path"
)
const (
READ_BUFFER_SIZE = 6000000
WRITE_BUFFER_SIZE = 1000000
S3_BUCKET = ""
AWS_KEY = ""
AWS_SECRET = ""
AWS_REGION = ""
AWS_ENDPOINT = ""
)
var (
fileName string
)
func main() {
flag.StringVar(&fileName, "f", "", "File to upload")
flag.Parse()
if fileName == "" {
fmt.Println("No file given")
return
}
r, writer := io.Pipe()
c := make(chan error)
go s3Read(r, c)
go writeFromFile(writer, c)
err := <-c
if err != nil {
fmt.Println("error occured", err)
}
fmt.Println("Finished upload")
}
func getS3Bucket(bucketName string) (*s3.Bucket, error) {
auth := aws.Auth{AWS_KEY, AWS_SECRET}
region := aws.Region{Name: AWS_REGION, S3Endpoint: AWS_ENDPOINT}
s3Client := s3.New(auth, region)
bucket := s3Client.Bucket(bucketName)
return bucket, nil
}
func writeFromFile(writer *io.PipeWriter, c chan error) {
fileReader, err := os.Open(fileName)
if err != nil {
c <- err
return
}
buf := make([]byte, WRITE_BUFFER_SIZE)
i := 0
for {
var readCount int
readCount, err = fileReader.Read(buf)
if err == io.EOF {
fmt.Println("file closed")
writer.Close()
break
}
count, err := writer.Write(buf[0:readCount])
if err != nil {
c <- err
return
}
fmt.Printf("Write %d bytes\n", count)
i++
}
}
func fileRead(r *io.PipeReader, c chan error) {
var buf = make([]byte, READ_BUFFER_SIZE)
fileWriter, err := os.Create("/test/test.csv")
if err != nil {
fileWriter.Close()
c <- err
return
}
totalReadCount := 0
for {
fmt.Println("Wating to Read")
fmt.Printf("Byte array length %d", len(buf))
count, err := r.Read(buf)
fmt.Printf("Read %d bytes\n", count)
totalReadCount += count
if err == io.EOF {
fmt.Println("Reach end of file")
if count > 0 {
fileWriter.Write(buf[0 : totalReadCount-1])
}
fileWriter.Close()
c <- nil
break
}
if err != nil {
fmt.Println("Error reading file", err)
fileWriter.Close()
c <- err
break
}
if len(buf) >= READ_BUFFER_SIZE {
fmt.Println("Writing to File")
fileWriter.Write(buf[0 : totalReadCount-1])
totalReadCount = 0
}
}
}
func s3Read(r *io.PipeReader, c chan error) {
var buf = new(bytes.Buffer)
var readBytes = make([]byte, READ_BUFFER_SIZE)
bucket, err := getS3Bucket(S3_BUCKET)
if err != nil {
c <- err
return
}
multi, err := bucket.Multi(path.Base(fileName), "text/plain", s3.Private)
if err != nil {
c <- err
return
}
var parts []s3.Part
var part s3.Part
i := 1
for {
fmt.Println("Wating to Read")
//The read will wait until there has been something written to the pipe.
count, err := r.Read(readBytes)
if count != 0 {
buf.Write(readBytes[0:count])
}
fmt.Printf("Read %d bytes\n", count)
if err == io.EOF {
//If there is a buffer with length greater than zero than put that part.
if buf.Len() > 0 {
fmt.Printf("Putting %d bytes to s3\n", buf.Len())
part, err = multi.PutPart(i, bytes.NewReader(buf.Bytes()))
fmt.Println("Done putting to s3")
fmt.Printf("Part size %d put to s3.", part.Size)
if err != nil {
multi.Abort()
c <- err
break
}
parts = append(parts, part)
}
if len(parts) > 0 {
fmt.Printf("Completing s3 upload %d\n", len(parts))
err = multi.Complete(parts)
fmt.Println("Completed s3 upload")
if err != nil {
c <- err
break
}
} else {
fmt.Println("File not big enough for multipart")
multi.Abort()
if err := bucket.PutReader(path.Base(fileName), buf, int64(buf.Len()), "text/plain", s3.Private); err != nil {
c <- err
break
}
}
fmt.Println("Finished Writing")
c <- nil
break
}
if err != nil {
if multi != nil {
multi.Abort()
}
fmt.Println("Reader errored", err)
c <- err
break
}
if buf.Len() >= READ_BUFFER_SIZE {
part, err := multi.PutPart(i, bytes.NewReader(buf.Bytes()))
if err != nil {
fmt.Println("Error putting part", err)
multi.Abort()
c <- err
return
}
fmt.Printf("Part size %d put to s3.", part.Size)
parts = append(parts, part)
i++
buf.Reset()
}
}
}