@@ -10,6 +10,7 @@ import (
10
10
"io"
11
11
"log"
12
12
"os"
13
+ "os/user"
13
14
"path/filepath"
14
15
"sort"
15
16
"strings"
@@ -559,17 +560,21 @@ func (uploader *Uploader) UploadDir(rootDir string, bucket, prefix string) error
559
560
if ! strings .HasSuffix (prefix , "/" ) && len (prefix ) > 0 {
560
561
prefix = prefix + "/"
561
562
}
562
- if ! strings .HasSuffix (rootDir , "/" ) && len (rootDir ) > 0 {
563
- rootDir = rootDir + "/"
563
+ rootDir , err := uploader .toAbs (rootDir )
564
+ if err != nil {
565
+ return err
564
566
}
567
+
565
568
chFiles := make (chan fileInfoType )
566
569
var consumerWgc sync.WaitGroup
570
+ var fileCounter FileCounter
567
571
for i := 0 ; i < uploader .opts .Jobs ; i ++ {
568
572
consumerWgc .Add (1 )
569
573
go func () {
570
574
defer consumerWgc .Done ()
571
575
for file := range chFiles {
572
- uploader .upload (file , consumerWgc )
576
+ fileCounter .addTotalNum (1 )
577
+ uploader .upload (file , & fileCounter )
573
578
}
574
579
}()
575
580
}
@@ -590,9 +595,31 @@ func (uploader *Uploader) UploadDir(rootDir string, bucket, prefix string) error
590
595
})
591
596
close (chFiles )
592
597
consumerWgc .Wait ()
598
+ fmt .Printf ("Done. Total num: %d, success num: %d, fail num: %d \n " , fileCounter .TotalNum , fileCounter .SuccessNum , fileCounter .FailNum )
593
599
return nil
594
600
}
595
601
602
+ func (uploader * Uploader ) toAbs (rootDir string ) (string , error ) {
603
+ if rootDir == "~" || strings .HasPrefix (rootDir , "~/" ) {
604
+ currentUser , err := user .Current ()
605
+ if err != nil {
606
+ log .Fatalf (err .Error ())
607
+ return rootDir , err
608
+ }
609
+
610
+ homeDir := currentUser .HomeDir
611
+ rootDir = strings .Replace (rootDir , "~" , homeDir , 1 )
612
+ }
613
+ rootDir , err := filepath .Abs (rootDir )
614
+ if err != nil {
615
+ return rootDir , err
616
+ }
617
+ if ! strings .HasSuffix (rootDir , "/" ) && len (rootDir ) > 0 {
618
+ rootDir = rootDir + "/"
619
+ }
620
+ return rootDir , nil
621
+ }
622
+
596
623
func (uploader * Uploader ) uploadFile (fileIfo fileInfoType , call func (success bool )) {
597
624
598
625
file , err := os .Open (fileIfo .filePath )
@@ -625,12 +652,15 @@ func makeObjectName(RootDir, Prefix, filePath string) string {
625
652
objectName := Prefix + resDir
626
653
return objectName
627
654
}
628
- func (uploader * Uploader ) upload (file fileInfoType , consumerWgc sync.WaitGroup ) {
655
+
656
+ func (uploader * Uploader ) upload (file fileInfoType , fileCounter * FileCounter ) {
629
657
uploader .uploadFile (file , func (success bool ) {
630
- consumerWgc .Done ()
631
- //completed.Inc()
632
658
if success {
659
+ fileCounter .addSuccessNum (1 )
633
660
fmt .Println (fmt .Sprintf ("%s successfully uploaded " , file .objectKey ))
661
+ } else {
662
+ fileCounter .addFailNum (1 )
663
+ fmt .Println (fmt .Sprintf ("%s upload failed" , file .objectKey ))
634
664
}
635
665
})
636
666
}
0 commit comments