@@ -3,11 +3,10 @@ package qiniu
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io"
6
7
"os"
7
8
"path/filepath"
8
- "strings"
9
-
10
- "io"
9
+ "time"
11
10
12
11
"github.com/qiniu/go-sdk/v7/auth/qbox"
13
12
"github.com/qiniu/go-sdk/v7/storage"
@@ -25,7 +24,7 @@ type PutRsp struct {
25
24
Bucket string
26
25
}
27
26
28
- // 上传一个文件夹
27
+ // UploadDir 上传一个文件夹
29
28
func (u Uploader ) UploadDir (zone * storage.Zone , bucket string , keyPrefix string , dirPath string ) (err error ) {
30
29
fmt .Printf ("upload dir: '%s' to bucket '%s', prefix: '%s'\n " , dirPath , bucket , keyPrefix )
31
30
@@ -37,16 +36,19 @@ func (u Uploader) UploadDir(zone *storage.Zone, bucket string, keyPrefix string,
37
36
return nil
38
37
}
39
38
40
- // 相对路径
41
- reaPath := strings .Replace (path , dirPath , "" , - 1 )
42
- reaPath = strings .Trim (reaPath , string (os .PathSeparator ))
43
-
44
- // for windows
45
- // 将分隔符替换为/
46
- reaPath = strings .Replace (reaPath , string (os .PathSeparator ), "/" , - 1 )
39
+ reaPath , err := filepath .Rel (dirPath , path )
40
+ if err != nil {
41
+ return err
42
+ }
47
43
44
+ start := time .Now ()
48
45
_ , err = u .UploadFile (zone , bucket , keyPrefix + reaPath , path )
49
- return err
46
+ if err != nil {
47
+ return fmt .Errorf ("upload file '%s' error: %w" , keyPrefix + reaPath , err )
48
+ }
49
+ fmt .Printf ("uploaded file '%s' success, spend time: %v\n " , keyPrefix + reaPath , time .Since (start ))
50
+
51
+ return nil
50
52
})
51
53
if err != nil {
52
54
return
0 commit comments