-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridfs2s3.go
275 lines (228 loc) · 6.73 KB
/
gridfs2s3.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"log"
"math/rand"
"mime"
"path/filepath"
"runtime"
"strings"
"time"
)
var (
gitRepo = "No repo info"
gitCommit = "No Hash Provided"
appVersion = "No Version Provided"
buildStamp = "No Time Provided"
)
var mongoUrl MongoDsn
var workBucket BucketInfo
var optionsApp CommonOptions
var maxRetry uint
//var SessionMongo *mgo.Session
var replacer = strings.NewReplacer("_", "/")
const goroutinesNum = 12
//type MongoFileInfo struct {
// SessionMongo *mgo.Session
// GFile *mgo.GridFile
//}
type Element struct {
Id bson.ObjectId `bson:"_id"`
Filename string `bson:",omitempty"`
ContentType string `bson:"contentType,omitempty"`
UploadDate time.Time `bson:"uploadDate"`
Length int64 `bson:",minsize"`
ItemNum int32 `bson:"itemNum,omitempty"`
ItemTotal int32 `bson:"itemTotal,omitempty"`
RetryCount int `bson:",omitempty"`
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func ByteCountBinary(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
func wrapUploadFile(connection *mgo.Session, fileInfo Element) {
var delay int64
var retryCount uint
maxRetry = 5
//delay = 0
for retryCount = 0; retryCount < maxRetry; retryCount++ {
if retryCount != 0 {
delay = int64(rand.Intn(1000))
}
time.Sleep(time.Duration(delay) * time.Millisecond)
fileInfo.RetryCount = int(retryCount)
err := uploadFile(connection, fileInfo)
if err != nil {
log.Printf("Critical: %s; try upload %s, retry %d", err, fileInfo.Filename, retryCount)
continue
} else {
break
}
}
if retryCount == maxRetry {
log.Printf("Critical: error upload %s", fileInfo.Filename)
}
}
func uploadFile(connection *mgo.Session, fileInfo Element) error {
startAt := time.Now()
gfs := connection.DB(mongoUrl.Db).GridFS("fs")
file, err := gfs.Open(fileInfo.Filename)
check(err)
defer file.Close()
var fileType string
if file.ContentType() == "" {
fileType = mime.TypeByExtension(filepath.Ext(fileInfo.Filename))
} else {
fileType = file.ContentType()
}
sessAws, err := session.NewSession(&aws.Config{
Region: aws.String("eu-central-1"),
MaxRetries: aws.Int(5)},
)
check(err)
uploader := s3manager.NewUploader(sessAws)
uploadPath := fmt.Sprintf("%s/%s/%s/%s", workBucket.Project, workBucket.Environment, workBucket.Prefix, replacer.Replace(fileInfo.Filename))
// Upload the file's body to S3 bucket as an object with the key being the
// same as the filename.
result, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(workBucket.bucketName),
Key: aws.String(filepath.Clean(uploadPath)),
Body: file,
ContentType: aws.String(fileType),
})
if err != nil {
log.Printf("Error: %s, retry count: %d", err, fileInfo.RetryCount)
return err
}
log.Printf("%d/%d Done: %s in %.2f secs, size %s", fileInfo.ItemNum, fileInfo.ItemTotal, result.Location, time.Since(startAt).Seconds(), ByteCountBinary(fileInfo.Length))
//time.Sleep(200*time.Millisecond)
return nil
}
func init() {
flag.StringVar(&workBucket.Project, "project", "", "Project name")
flag.StringVar(&workBucket.bucketName, "bucket", "", "S3 bucket")
flag.StringVar(&workBucket.Environment, "env", "", "Project environment")
flag.BoolVar(&optionsApp.version, "version", false, "Show version info")
flag.StringVar(&workBucket.Prefix, "prefix", "uploads", "path prefix")
flag.Parse()
optionsApp.checkParams()
workBucket.checkParams()
mongoUrl.init()
}
func deduplicate(elements []Element) []Element {
keys := make(map[string]bool)
list := make([]Element, 0, len(elements))
for _, entry := range elements {
if _, present := keys[entry.Filename]; !present {
keys[entry.Filename] = true
list = append(list, entry)
}
}
return list
}
func startWorker(session *mgo.Session, workerNum int, in <-chan Element) {
log.Printf("Info: worker %d started\n", workerNum+1)
connection := session.Copy()
defer connection.Close()
for input := range in {
//fmt.Printf("Worker %d\tfile: %s\n",workerNum,input.FileName)
wrapUploadFile(connection, input)
runtime.Gosched()
}
log.Printf("Info: worker %d stoped\n", workerNum+1)
}
func main() {
startAt := time.Now()
runtime.GOMAXPROCS(0)
worketInput := make(chan Element, 2)
SessionMongo, err := mgo.Dial(mongoUrl.buildDsn())
if err != nil {
panic(err)
}
defer SessionMongo.Close()
// Optional. Switch the session to a monotonic behavior.
SessionMongo.SetMode(mgo.Monotonic, true)
for i := 0; i < goroutinesNum; i++ {
go startWorker(SessionMongo, i, worketInput)
}
mainSession := SessionMongo.Copy()
var results []Element
// Search all files in GridFS
gfs := mainSession.DB(mongoUrl.Db).GridFS("fs")
iter := gfs.Find(nil).Sort("filename").Iter()
err = iter.All(&results)
if err != nil {
log.Println(err)
}
normalizedResults := deduplicate(results)
var itemNum int32
var itemTotal int32
var normalizedTotal int32
var goodCount int32
var wrongCount int32
goodCount = 0
itemTotal = int32(len(results))
normalizedTotal = int32(len(normalizedResults))
wrongCount = 0
log.Println("Info: find ", itemTotal, "objects")
// destroy results slice for free memory
results = nil
for _, element := range normalizedResults {
itemNum += 1
if element.Filename != "" && !strings.Contains(element.Filename, "unison") {
goodCount += 1
element.ItemTotal = normalizedTotal
element.ItemNum = itemNum
worketInput <- element
} else {
wrongCount += 1
log.Printf("%d/%d Error: %s has no filename\n", itemNum, itemTotal, element.Id.Hex())
}
}
//var f *mgo.GridFile
//
//for gfs.OpenNext(iter, &f) {
// if f.Name() != "" && !strings.Contains(f.Name(), "unison") {
// worketInput <- MongoFileInfo{SessionMongo, f}
// } else {
// log.Printf("Error: %s has no filename\n", f.Id())
// }
// if iter.Timeout() {
// log.Println("Info: cursor timeout occurred")
// continue
// }
// if iter.Err() != nil {
// log.Printf("Error: %s, Id %s, file %s\n", iter.Err(), f.Id(), f.Name())
// iter.Close()
// }
//}
close(worketInput)
time.Sleep(20 * time.Second)
if iter.Close() != nil {
panic(iter.Close())
}
log.Printf("Total obects in DB: %d", itemTotal)
log.Printf("Total files for upload: %d", normalizedTotal)
log.Printf("Wrong files: %d", wrongCount)
log.Printf("Uploaded files: %d", goodCount)
log.Printf("Total time: %.1f minutes", time.Since(startAt).Minutes())
}