Skip to content

Commit 8daa2ea

Browse files
author
zhangliang
committed
Fix reaPath
1 parent 6ed82de commit 8daa2ea

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

Diff for: lib/qiniu/upload.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package qiniu
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"os"
78
"path/filepath"
8-
"strings"
9-
10-
"io"
9+
"time"
1110

1211
"github.com/qiniu/go-sdk/v7/auth/qbox"
1312
"github.com/qiniu/go-sdk/v7/storage"
@@ -25,7 +24,7 @@ type PutRsp struct {
2524
Bucket string
2625
}
2726

28-
// 上传一个文件夹
27+
// UploadDir 上传一个文件夹
2928
func (u Uploader) UploadDir(zone *storage.Zone, bucket string, keyPrefix string, dirPath string) (err error) {
3029
fmt.Printf("upload dir: '%s' to bucket '%s', prefix: '%s'\n", dirPath, bucket, keyPrefix)
3130

@@ -37,16 +36,19 @@ func (u Uploader) UploadDir(zone *storage.Zone, bucket string, keyPrefix string,
3736
return nil
3837
}
3938

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+
}
4743

44+
start := time.Now()
4845
_, 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
5052
})
5153
if err != nil {
5254
return

Diff for: lib/qiniu/upload_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
func TestUploadDir(t *testing.T) {
9-
u := NewQiniu("I50Yj4ceahNUfdGxS-7p5KrnWiZF18APP32t59jV", "bIdalJ61Q_S32R5CLzZw3-4DCL2YBQpvOqk-BRjW").Uploader()
10-
err := u.UploadDir(&storage.ZoneHuadong, "nameimtest", "drone/", `E:\tmp\cdn-file\baidu\share\api`)
9+
u := NewQiniu("", "").Uploader()
10+
err := u.UploadDir(&storage.ZoneHuadong, "nameimtest", "test2/", `../qiniu/`)
1111
if err != nil {
1212
t.Fatal(err)
1313
}
@@ -16,7 +16,7 @@ func TestUploadDir(t *testing.T) {
1616
}
1717

1818
func TestUploadFile(t *testing.T) {
19-
u := NewQiniu("I50Yj4ceahNUfdGxS-7p5KrnWiZF18APP32t59jV", "bIdalJ61Q_S32R5CLzZw3-4DCL2YBQpvOqk-BRjW").Uploader()
19+
u := NewQiniu("", "").Uploader()
2020
_, err := u.UploadFile(&storage.ZoneHuadong, "nameimtest", "drone/", `Z:\go_path\src\github.com\bysir-zl\drone-qiniu\Dockerfile`)
2121
if err != nil {
2222
t.Fatal(err)

0 commit comments

Comments
 (0)