forked from qiniu/qshell
/
qupload.go
880 lines (782 loc) · 25.4 KB
/
qupload.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
package qshell
import (
"bufio"
"encoding/json"
"fmt"
"github.com/astaxie/beego/logs"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"net"
"net/http"
"os"
"path/filepath"
"qiniu/api.v6/auth/digest"
"qiniu/api.v6/conf"
fio "qiniu/api.v6/io"
rio "qiniu/api.v6/resumable/io"
"qiniu/api.v6/rs"
"qiniu/rpc"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
/*
Config file like:
{
"src_dir" : "/Users/jemy/Photos",
"file_list" : "",
"bucket" : "test-bucket",
"put_threshold" : 10000000,
"key_prefix" : "2014/12/01/",
"ignore_dir" : false,
"overwrite" : false,
"check_exists" : true,
"skip_file_prefixes" : "IMG_",
"skip_path_prefixes" : "tmp/,bin/,obj/",
"skip_suffixes" : ".exe,.obj,.class",
"skip_fixed_strings" : ".svn,.git",
"up_host" : "http://upload.qiniu.com",
"bind_up_ip" : "",
"bind_rs_ip" : "",
"bind_nic_ip" : "",
"rescan_local" : false,
"delete_on_success" : false
}
or the simplest one
{
"src_dir" : "/Users/jemy/Photos",
"bucket" : "test-bucket",
}
*/
const (
DEFAULT_PUT_THRESHOLD int64 = 10 * 1024 * 1024 //10MB
MIN_UPLOAD_THREAD_COUNT int64 = 1
MAX_UPLOAD_THREAD_COUNT int64 = 2000
)
type UploadInfo struct {
TotalFileCount int64 `json:"total_file_count"`
}
type UploadConfig struct {
//basic config
SrcDir string `json:"src_dir"`
Bucket string `json:"bucket"`
//optional config
FileList string `json:"file_list,omitempty"`
PutThreshold int64 `json:"put_threshold,omitempty"`
KeyPrefix string `json:"key_prefix,omitempty"`
IgnoreDir bool `json:"ignore_dir,omitempty"`
Overwrite bool `json:"overwrite,omitempty"`
CheckExists bool `json:"check_exists,omitempty"`
CheckHash bool `json:"check_hash,omitempty"`
CheckSize bool `json:"check_size,omitempty"`
SkipFilePrefixes string `json:"skip_file_prefixes,omitempty"`
SkipPathPrefixes string `json:"skip_path_prefixes,omitempty"`
SkipFixedStrings string `json:"skip_fixed_strings,omitempty"`
SkipSuffixes string `json:"skip_suffixes,omitempty"`
RescanLocal bool `json:"rescan_local,omitempty"`
FileType int `json:"file_type,omitempty"`
//advanced config
UpHost string `json:"up_host,omitempty"`
BindUpIp string `json:"bind_up_ip,omitempty"`
BindRsIp string `json:"bind_rs_ip,omitempty"`
//local network interface card config
BindNicIp string `json:"bind_nic_ip,omitempty"`
//log settings
LogLevel string `json:"log_level,omitempty"`
LogFile string `json:"log_file,omitempty"`
LogRotate int `json:"log_rotate,omitempty"`
LogStdout bool `json:"log_stdout,omitempty"`
//more settings
DeleteOnSuccess bool `json:"delete_on_success,omitempty"`
DisableResume bool `json:"disable_resume,omitempty"`
IsHostFileSpecified bool `json:"-"`
}
var upSettings = rio.Settings{
Workers: 16,
ChunkSize: 4 * 1024 * 1024,
TryTimes: 3,
}
var uploadTasks chan func()
var initUpOnce sync.Once
func doUpload(tasks chan func()) {
for {
task := <-tasks
task()
}
}
var currentFileCount int64
var successFileCount int64
var notOverwriteCount int64
var failureFileCount int64
var skippedFileCount int64
// FileExporter
type FileExporter struct {
SuccessFname string
SuccessLock sync.RWMutex
SuccessWriter *bufio.Writer
FailureFname string
FailureLock sync.RWMutex
FailureWriter *bufio.Writer
OverwriteFname string
OverwriteLock sync.RWMutex
OverwriteWriter *bufio.Writer
}
// QiniuUpload
func QiniuUpload(threadCount int, uploadConfig *UploadConfig, exporter *FileExporter) {
timeStart := time.Now()
//create job id
jobId := Md5Hex(fmt.Sprintf("%s:%s", uploadConfig.SrcDir, uploadConfig.Bucket))
//local storage path
storePath := filepath.Join(QShellRootPath, ".qshell", QAccountName, "qupload", jobId)
if mkdirErr := os.MkdirAll(storePath, 0775); mkdirErr != nil {
logs.Error("Failed to mkdir `%s` due to `%s`", storePath, mkdirErr)
os.Exit(STATUS_HALT)
}
defaultLogFile := filepath.Join(storePath, fmt.Sprintf("%s.log", jobId))
//init log level
logLevel := logs.LevelInformational
logRotate := 1
if uploadConfig.LogRotate > 0 {
logRotate = uploadConfig.LogRotate
}
switch uploadConfig.LogLevel {
case "debug":
logLevel = logs.LevelDebug
case "info":
logLevel = logs.LevelInformational
case "warn":
logLevel = logs.LevelWarning
case "error":
logLevel = logs.LevelError
default:
logLevel = logs.LevelInformational
}
//init log writer
if uploadConfig.LogFile == "" {
//set default log file
uploadConfig.LogFile = defaultLogFile
}
if !uploadConfig.LogStdout {
logs.GetBeeLogger().DelLogger(logs.AdapterConsole)
}
//open log file
fmt.Println("Writing upload log to file", uploadConfig.LogFile)
//daily rotate
logCfg := BeeLogConfig{
Filename: uploadConfig.LogFile,
Level: logLevel,
Daily: true,
MaxDays: logRotate,
}
logs.SetLogger(logs.AdapterFile, logCfg.ToJson())
fmt.Println()
//init file list writer
var successListFp *os.File
var failureListFp *os.File
var overwriteListFp *os.File
var openErr error
var successListWriter *bufio.Writer
var failureListWriter *bufio.Writer
var overwriteListWriter *bufio.Writer
if exporter.SuccessFname != "" {
successListFp, openErr = os.Create(exporter.SuccessFname)
if openErr != nil {
logs.Error("Open success list file error, %s", openErr)
} else {
defer successListFp.Close()
successListWriter = bufio.NewWriter(successListFp)
exporter.SuccessWriter = successListWriter
}
}
if exporter.FailureFname != "" {
failureListFp, openErr = os.Create(exporter.FailureFname)
if openErr != nil {
logs.Error("Open fail list file error, %s", openErr)
} else {
defer failureListFp.Close()
failureListWriter = bufio.NewWriter(failureListFp)
exporter.FailureWriter = failureListWriter
}
}
if exporter.OverwriteFname != "" {
overwriteListFp, openErr = os.Create(exporter.OverwriteFname)
if openErr != nil {
logs.Error("Open overwrite list file error, %s", openErr)
} else {
defer overwriteListFp.Close()
overwriteListWriter = bufio.NewWriter(overwriteListFp)
exporter.OverwriteWriter = overwriteListWriter
}
}
//global up settings
account, gErr := GetAccount()
if gErr != nil {
fmt.Println(gErr)
os.Exit(STATUS_HALT)
}
mac := digest.Mac{AccessKey: account.AccessKey, SecretKey: []byte(account.SecretKey)}
if !uploadConfig.IsHostFileSpecified {
//get bucket zone info
bucketInfo, gErr := GetBucketInfo(&mac, uploadConfig.Bucket)
if gErr != nil {
logs.Error("Get bucket region info error,", gErr)
os.Exit(STATUS_HALT)
}
//set up host
SetZone(bucketInfo.Region)
}
//chunk upload threshold
putThreshold := DEFAULT_PUT_THRESHOLD
if uploadConfig.PutThreshold > 0 {
putThreshold = uploadConfig.PutThreshold
}
//use host if not empty, overwrite the default config
if uploadConfig.UpHost != "" {
conf.UP_HOST = strings.TrimSuffix(uploadConfig.UpHost, "/")
}
//set resume upload settings
rio.SetSettings(&upSettings)
//make SrcDir the full path
uploadConfig.SrcDir, _ = filepath.Abs(uploadConfig.SrcDir)
//find the local file list, by specified or by config
var cacheResultName string
var cacheCountName string
var totalFileCount int64
var cacheErr error
_, localFileStatErr := os.Stat(uploadConfig.FileList)
if uploadConfig.FileList != "" && localFileStatErr == nil {
//use specified file list
cacheResultName = uploadConfig.FileList
totalFileCount = GetFileLineCount(cacheResultName)
} else {
cacheResultName = filepath.Join(storePath, fmt.Sprintf("%s.cache", jobId))
cacheCountName = filepath.Join(storePath, fmt.Sprintf("%s.count", jobId))
totalFileCount, cacheErr = prepareCacheFileList(cacheResultName, cacheCountName,
uploadConfig.SrcDir, uploadConfig.RescanLocal)
if cacheErr != nil {
os.Exit(STATUS_HALT)
}
}
//leveldb folder
leveldbFileName := filepath.Join(storePath, jobId+".ldb")
ldb, err := leveldb.OpenFile(leveldbFileName, nil)
if err != nil {
logs.Error("Open leveldb `%s` failed due to %s", leveldbFileName, err)
os.Exit(STATUS_HALT)
}
defer ldb.Close()
//open cache list file
cacheResultFileHandle, err := os.Open(cacheResultName)
if err != nil {
logs.Error("Open list file `%s` failed due to %s", cacheResultName, err)
os.Exit(STATUS_HALT)
}
defer cacheResultFileHandle.Close()
bScanner := bufio.NewScanner(cacheResultFileHandle)
bScanner.Split(bufio.ScanLines)
ldbWOpt := opt.WriteOptions{
Sync: true,
}
//init wait group
upWaitGroup := sync.WaitGroup{}
initUpOnce.Do(func() {
uploadTasks = make(chan func(), threadCount)
for i := 0; i < threadCount; i++ {
go doUpload(uploadTasks)
}
})
//check bind net interface card
var transport *http.Transport
var dialer = net.Dialer{}
if uploadConfig.BindNicIp != "" {
dialer.LocalAddr = &net.TCPAddr{
IP: net.ParseIP(uploadConfig.BindNicIp),
}
}
//set keepalive and timeout
dialer.Timeout = time.Duration(60) * time.Second
dialer.KeepAlive = time.Second * 30
var rsClient rs.Client
transport = &http.Transport{
Dial: dialer.Dial,
}
rsClient = rs.NewMacEx(&mac, transport, uploadConfig.BindRsIp)
//scan lines and upload
for bScanner.Scan() {
line := bScanner.Text()
items := strings.Split(line, "\t")
if len(items) != 3 {
logs.Error("Invalid cache line `%s`", line)
continue
}
localFileRelativePath := items[0]
currentFileCount += 1
//check skip local file or folder
if skip, prefix := hitByPathPrefixes(uploadConfig.SkipPathPrefixes, localFileRelativePath); skip {
logs.Informational("Skip by path prefix `%s` for local file path `%s`", prefix, localFileRelativePath)
skippedFileCount += 1
continue
}
if skip, prefix := hitByFilePrefixes(uploadConfig.SkipFilePrefixes, localFileRelativePath); skip {
logs.Informational("Skip by file prefix `%s` for local file path `%s`", prefix, localFileRelativePath)
skippedFileCount += 1
continue
}
if skip, fixedStr := hitByFixesString(uploadConfig.SkipFixedStrings, localFileRelativePath); skip {
logs.Informational("Skip by fixed string `%s` for local file path `%s`", fixedStr, localFileRelativePath)
skippedFileCount += 1
continue
}
if skip, suffix := hitBySuffixes(uploadConfig.SkipSuffixes, localFileRelativePath); skip {
logs.Informational("Skip by suffix `%s` for local file `%s`", suffix, localFileRelativePath)
skippedFileCount += 1
continue
}
//pack the upload file key
localFileLastModified, _ := strconv.ParseInt(items[2], 10, 64)
uploadFileKey := localFileRelativePath
//check ignore dir
if uploadConfig.IgnoreDir {
uploadFileKey = filepath.Base(uploadFileKey)
}
//check prefix
if uploadConfig.KeyPrefix != "" {
uploadFileKey = strings.Join([]string{uploadConfig.KeyPrefix, uploadFileKey}, "")
}
//convert \ to / under windows
if runtime.GOOS == "windows" {
uploadFileKey = strings.Replace(uploadFileKey, "\\", "/", -1)
}
localFilePath := filepath.Join(uploadConfig.SrcDir, localFileRelativePath)
localFileStat, statErr := os.Stat(localFilePath)
if statErr != nil {
failureFileCount += 1
logs.Error("Error stat local file `%s` due to `%s`", localFilePath, statErr)
continue
}
localFileSize := localFileStat.Size()
ldbKey := fmt.Sprintf("%s => %s", localFilePath, uploadFileKey)
if totalFileCount != 0 {
fmt.Printf("Uploading %s [%d/%d, %.1f%%] ...\n", ldbKey, currentFileCount, totalFileCount,
float32(currentFileCount)*100/float32(totalFileCount))
} else {
fmt.Printf("Uploading %s ...\n", ldbKey)
}
//check exists
needToUpload, isOverwrite := checkFileNeedToUpload(uploadConfig, &rsClient, ldb, &ldbWOpt, ldbKey, localFilePath,
uploadFileKey, localFileLastModified, localFileSize)
if !needToUpload {
//no need to upload
continue
}
logs.Informational("Uploading file %s => %s : %s", localFilePath, uploadConfig.Bucket, uploadFileKey)
//start to upload
upWaitGroup.Add(1)
uploadTasks <- func() {
defer upWaitGroup.Done()
policy := rs.PutPolicy{}
policy.Scope = uploadConfig.Bucket
if uploadConfig.Overwrite {
policy.Scope = fmt.Sprintf("%s:%s", uploadConfig.Bucket, uploadFileKey)
policy.InsertOnly = 0
}
policy.FileType = uploadConfig.FileType
policy.Expires = 7 * 24 * 3600
upToken := policy.Token(&mac)
if localFileSize > putThreshold {
resumableUploadFile(uploadConfig, transport, ldb, &ldbWOpt, ldbKey, upToken, storePath,
localFilePath, uploadFileKey, localFileLastModified, isOverwrite, exporter)
} else {
formUploadFile(uploadConfig, transport, ldb, &ldbWOpt, ldbKey, upToken,
localFilePath, uploadFileKey, localFileLastModified, isOverwrite, exporter)
}
}
}
upWaitGroup.Wait()
//flush
exportWriters := []*bufio.Writer{
exporter.SuccessWriter,
exporter.FailureWriter,
exporter.OverwriteWriter,
}
for _, writer := range exportWriters {
if writer != nil {
writer.Flush()
}
}
logs.Informational("-------------Upload Result--------------")
logs.Informational("%20s%10d", "Total:", totalFileCount)
logs.Informational("%20s%10d", "Success:", successFileCount)
logs.Informational("%20s%10d", "Failure:", failureFileCount)
logs.Informational("%20s%10d", "NotOverwrite:", notOverwriteCount)
logs.Informational("%20s%10d", "Skipped:", skippedFileCount)
logs.Informational("%20s%15s", "Duration:", time.Since(timeStart))
logs.Info("----------------------------------------")
fmt.Println("\nSee upload log at path", uploadConfig.LogFile)
if failureFileCount > 0 {
os.Exit(STATUS_ERROR)
} else {
os.Exit(STATUS_OK)
}
}
func prepareCacheFileList(cacheResultName, cacheCountName, srcDir string, rescanLocal bool) (totalFileCount int64, cacheErr error) {
//cache file
cacheTempName := fmt.Sprintf("%s.temp", cacheResultName)
rescanLocalDir := false
if _, statErr := os.Stat(cacheResultName); statErr == nil {
//file exists
rescanLocalDir = rescanLocal
} else {
rescanLocalDir = true
}
if rescanLocalDir {
logs.Informational("Listing local sync dir, this can take a long time for big directory, please wait patiently")
totalFileCount, cacheErr = DirCache(srcDir, cacheTempName)
if cacheErr != nil {
return
}
if rErr := os.Rename(cacheTempName, cacheResultName); rErr != nil {
logs.Error("Rename the temp cached file error", rErr)
cacheErr = rErr
return
}
//write the total count to local file
if cFp, cErr := os.Create(cacheCountName); cErr == nil {
func() {
defer cFp.Close()
uploadInfo := UploadInfo{
TotalFileCount: totalFileCount,
}
uploadInfoBytes, mErr := json.Marshal(&uploadInfo)
if mErr == nil {
if _, wErr := cFp.Write(uploadInfoBytes); wErr != nil {
logs.Warning("Write local cached count file error %s", cErr)
} else {
cFp.Close()
}
}
}()
} else {
logs.Error("Open local cached count file error %s,", cErr)
}
} else {
logs.Informational("Use the last cached local sync dir file list")
//read from local cache
if rFp, rErr := os.Open(cacheCountName); rErr == nil {
func() {
defer rFp.Close()
uploadInfo := UploadInfo{}
decoder := json.NewDecoder(rFp)
if dErr := decoder.Decode(&uploadInfo); dErr == nil {
totalFileCount = uploadInfo.TotalFileCount
}
}()
} else {
logs.Warning("Open local cached count file error %s,", rErr)
totalFileCount = GetFileLineCount(cacheResultName)
}
}
return
}
func hitByPathPrefixes(pathPrefixesStr, localFileRelativePath string) (hit bool, pathPrefix string) {
if pathPrefixesStr != "" {
//unpack skip prefix
pathPrefixes := strings.Split(pathPrefixesStr, ",")
for _, prefix := range pathPrefixes {
if strings.TrimSpace(prefix) == "" {
continue
}
if strings.HasPrefix(localFileRelativePath, prefix) {
pathPrefix = prefix
hit = true
break
}
}
}
return
}
func hitByFilePrefixes(filePrefixesStr, localFileRelativePath string) (hit bool, filePrefix string) {
if filePrefixesStr != "" {
//unpack skip prefix
filePrefixes := strings.Split(filePrefixesStr, ",")
for _, prefix := range filePrefixes {
if strings.TrimSpace(prefix) == "" {
continue
}
localFileName := filepath.Base(localFileRelativePath)
if strings.HasPrefix(localFileName, prefix) {
filePrefix = prefix
hit = true
break
}
}
}
return
}
func hitByFixesString(fixedStringsStr, localFileRelativePath string) (hit bool, hitFixedStr string) {
if fixedStringsStr != "" {
//unpack fixed strings
fixedStrings := strings.Split(fixedStringsStr, ",")
for _, fixedStr := range fixedStrings {
if strings.TrimSpace(fixedStr) == "" {
continue
}
if strings.Contains(localFileRelativePath, fixedStr) {
hitFixedStr = fixedStr
hit = true
break
}
}
}
return
}
func hitBySuffixes(suffixesStr, localFileRelativePath string) (hit bool, hitSuffix string) {
if suffixesStr != "" {
suffixes := strings.Split(suffixesStr, ",")
for _, suffix := range suffixes {
if strings.TrimSpace(suffix) == "" {
continue
}
if strings.HasSuffix(localFileRelativePath, suffix) {
hitSuffix = suffix
hit = true
break
}
}
}
return
}
func checkFileNeedToUpload(uploadConfig *UploadConfig, rsClient *rs.Client, ldb *leveldb.DB, ldbWOpt *opt.WriteOptions,
ldbKey, localFilePath, uploadFileKey string, localFileLastModified, localFileSize int64) (needToUpload, isOverwrite bool) {
//default to upload
needToUpload = true
//check before to upload
if uploadConfig.CheckExists {
rsEntry, checkErr := rsClient.Stat(nil, uploadConfig.Bucket, uploadFileKey)
if checkErr == nil {
ldbValue := fmt.Sprintf("%d", localFileLastModified)
if uploadConfig.CheckHash {
//compare hash
localEtag, cErr := GetEtag(localFilePath)
if cErr != nil {
logs.Error("File `%s` calc local hash failed, %s", uploadFileKey, cErr)
atomic.AddInt64(&failureFileCount, 1)
needToUpload = false
}
if rsEntry.Hash == localEtag {
logs.Informational("File `%s` exists in bucket, hash match, ignore this upload", uploadFileKey)
atomic.AddInt64(&skippedFileCount, 1)
putErr := ldb.Put([]byte(ldbKey), []byte(ldbValue), ldbWOpt)
if putErr != nil {
logs.Error("Put key `%s` into leveldb error due to `%s`", ldbKey, putErr)
}
needToUpload = false
} else {
if !uploadConfig.Overwrite {
logs.Warning("Skip upload of unmatch hash file `%s` because `overwrite` is false", localFilePath)
atomic.AddInt64(¬OverwriteCount, 1)
needToUpload = false
} else {
isOverwrite = true
logs.Informational("File `%s` exists in bucket, but hash not match, go to upload", uploadFileKey)
}
}
} else {
if uploadConfig.CheckSize {
if rsEntry.Fsize == localFileSize {
logs.Info("File `%s` exists in bucket, size match, ignore this upload", uploadFileKey)
atomic.AddInt64(&skippedFileCount, 1)
putErr := ldb.Put([]byte(ldbKey), []byte(ldbValue), ldbWOpt)
if putErr != nil {
logs.Error("Put key `%s` into leveldb error due to `%s`", ldbKey, putErr)
}
needToUpload = false
} else {
if !uploadConfig.Overwrite {
logs.Warning("Skip upload of unmatch size file `%s` because `overwrite` is false", localFilePath)
atomic.AddInt64(¬OverwriteCount, 1)
needToUpload = false
} else {
isOverwrite = true
logs.Info("File `%s` exists in bucket, but size not match, go to upload", uploadFileKey)
}
}
} else {
logs.Info("File `%s` exists in bucket, no hash or size check, ignore this upload", uploadFileKey)
atomic.AddInt64(&skippedFileCount, 1)
putErr := ldb.Put([]byte(ldbKey), []byte(ldbValue), ldbWOpt)
if putErr != nil {
logs.Error("Put key `%s` into leveldb error due to `%s`", ldbKey, putErr)
}
needToUpload = false
}
}
} else {
if _, ok := checkErr.(*rpc.ErrorInfo); !ok {
//not logic error, should be network error
logs.Error("Get file `%s` stat error, %s", uploadFileKey, checkErr)
atomic.AddInt64(&failureFileCount, 1)
needToUpload = false
}
}
} else {
//check leveldb
ldbFlmd, err := ldb.Get([]byte(ldbKey), nil)
flmd, _ := strconv.ParseInt(string(ldbFlmd), 10, 64)
//not exist, return ErrNotFound
//check last modified
if err == nil {
if localFileLastModified == flmd {
logs.Informational("Skip by local leveldb log for file `%s`", localFilePath)
atomic.AddInt64(&skippedFileCount, 1)
needToUpload = false
} else {
if !uploadConfig.Overwrite {
//no overwrite set
logs.Warning("Skip upload of changed file `%s` because `overwrite` is false",
localFilePath)
atomic.AddInt64(¬OverwriteCount, 1)
needToUpload = false
} else {
isOverwrite = true
}
}
}
}
return
}
func formUploadFile(uploadConfig *UploadConfig, transport *http.Transport,
ldb *leveldb.DB, ldbWOpt *opt.WriteOptions, ldbKey string, upToken string,
localFilePath, uploadFileKey string, localFileLastModified int64, isOverwrite bool, exporter *FileExporter) {
var putClient rpc.Client
if transport != nil {
putClient = rpc.NewClientEx(transport, uploadConfig.BindUpIp)
} else {
putClient = rpc.NewClient(uploadConfig.BindUpIp)
}
putRet := fio.PutRet{}
putExtra := fio.PutExtra{
CheckCrc: 1,
}
err := fio.PutFile(putClient, nil, &putRet, upToken, uploadFileKey, localFilePath, &putExtra)
if err != nil {
atomic.AddInt64(&failureFileCount, 1)
var errMsg string
if pErr, ok := err.(*rpc.ErrorInfo); ok {
errMsg = pErr.Err
} else {
errMsg = err.Error()
}
logs.Error("Form upload file `%s` => `%s` failed due to nerror `%s`", localFilePath, uploadFileKey, errMsg)
if exporter.FailureWriter != nil {
exporter.FailureLock.Lock()
exporter.FailureWriter.WriteString(fmt.Sprintf("%s\t%s\t%s\n", localFilePath, uploadFileKey, errMsg))
exporter.FailureWriter.Flush()
exporter.FailureLock.Unlock()
}
} else {
atomic.AddInt64(&successFileCount, 1)
logs.Informational("Upload file `%s` => `%s : %s` success", localFilePath, uploadConfig.Bucket, uploadFileKey)
putErr := ldb.Put([]byte(ldbKey), []byte(fmt.Sprintf("%d", localFileLastModified)), ldbWOpt)
if putErr != nil {
logs.Error("Put key `%s` into leveldb error due to `%s`", ldbKey, putErr)
}
//delete on success
if uploadConfig.DeleteOnSuccess {
deleteErr := os.Remove(localFilePath)
if deleteErr != nil {
logs.Error("Delete `%s` on upload success error due to `%s`", localFilePath, deleteErr)
} else {
logs.Info("Delete `%s` on upload success done", localFilePath)
}
}
if exporter.SuccessWriter != nil {
exporter.SuccessLock.Lock()
exporter.SuccessWriter.WriteString(fmt.Sprintf("%s\t%s\n", localFilePath, uploadFileKey))
exporter.SuccessWriter.Flush()
exporter.SuccessLock.Unlock()
}
if isOverwrite && exporter.OverwriteWriter != nil {
exporter.OverwriteLock.Lock()
exporter.OverwriteWriter.WriteString(fmt.Sprintf("%s\t%s\n", localFilePath, uploadFileKey))
exporter.OverwriteWriter.Flush()
exporter.OverwriteLock.Unlock()
}
}
}
func resumableUploadFile(uploadConfig *UploadConfig, transport *http.Transport,
ldb *leveldb.DB, ldbWOpt *opt.WriteOptions, ldbKey string, upToken string, storePath,
localFilePath, uploadFileKey string, localFileLastModified int64, isOverwrite bool, exporter *FileExporter) {
var putClient rpc.Client
if transport != nil {
putClient = rio.NewClientEx(upToken, transport, uploadConfig.BindUpIp)
} else {
putClient = rio.NewClient(upToken, uploadConfig.BindUpIp)
}
//params
putRet := rio.PutRet{}
putExtra := rio.PutExtra{}
var progressFilePath string
if !uploadConfig.DisableResume {
//progress file
progressFileKey := Md5Hex(fmt.Sprintf("%s:%s|%s:%s", uploadConfig.SrcDir,
uploadConfig.Bucket, localFilePath, uploadFileKey))
progressFilePath = filepath.Join(storePath, fmt.Sprintf("%s.progress", progressFileKey))
putExtra.ProgressFile = progressFilePath
}
//resumable upload
err := rio.PutFile(putClient, nil, &putRet, uploadFileKey, localFilePath, &putExtra)
if err != nil {
if progressFilePath != "" {
os.Remove(progressFilePath)
}
atomic.AddInt64(&failureFileCount, 1)
var errMsg string
if pErr, ok := err.(*rpc.ErrorInfo); ok {
errMsg = pErr.Err
} else {
errMsg = err.Error()
}
logs.Error("Resumable upload file `%s` => `%s` failed due to nerror `%s`", localFilePath, uploadFileKey, errMsg)
if exporter.FailureWriter != nil {
exporter.FailureLock.Lock()
exporter.FailureWriter.WriteString(fmt.Sprintf("%s\t%s\t%s\n", localFilePath, uploadFileKey, errMsg))
exporter.FailureWriter.Flush()
exporter.FailureLock.Unlock()
}
} else {
if progressFilePath != "" {
os.Remove(progressFilePath)
}
atomic.AddInt64(&successFileCount, 1)
logs.Informational("Upload file `%s` => `%s` success", localFilePath, uploadFileKey)
putErr := ldb.Put([]byte(ldbKey), []byte(fmt.Sprintf("%d", localFileLastModified)), ldbWOpt)
if putErr != nil {
logs.Error("Put key `%s` into leveldb error due to `%s`", ldbKey, putErr)
}
//delete on success
if uploadConfig.DeleteOnSuccess {
deleteErr := os.Remove(localFilePath)
if deleteErr != nil {
logs.Error("Delete `%s` on upload success error due to `%s`", localFilePath, deleteErr)
} else {
logs.Info("Delete `%s` on upload success done", localFilePath)
}
}
if exporter.SuccessWriter != nil {
exporter.SuccessLock.Lock()
exporter.SuccessWriter.WriteString(fmt.Sprintf("%s\t%s\n", localFilePath, uploadFileKey))
exporter.SuccessWriter.Flush()
exporter.SuccessLock.Unlock()
}
if isOverwrite && exporter.OverwriteWriter != nil {
exporter.OverwriteLock.Lock()
exporter.OverwriteWriter.WriteString(fmt.Sprintf("%s\t%s\n", localFilePath, uploadFileKey))
exporter.OverwriteWriter.Flush()
exporter.OverwriteLock.Unlock()
}
}
}