Skip to content

Commit bbaa588

Browse files
author
zhangliang
committed
parallel upload
1 parent fc22669 commit bbaa588

File tree

7 files changed

+59
-17
lines changed

7 files changed

+59
-17
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# drone-qiniu
2-
上传文件到七牛的drone插件
2+
上传文件到七牛的 drone 插件
33

44
## Build
55
```
@@ -12,9 +12,9 @@ GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o qiniu
1212
SET ACCESS_KEY=xxx
1313
SET SECRET_KEY=xxx
1414
SET ZONE=huadong
15-
SET BUCKET=nameimtest
15+
SET BUCKET=mybucket
1616
SET PREFIX=drone/
17-
SET PATH=e:\drone-qiniu
17+
SET dir=./dist
1818
```
1919

2020
## Usage

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/zbysir/drone-qiniu
33
go 1.15
44

55
require (
6+
github.com/dustin/go-humanize v1.0.1
67
github.com/qiniu/go-sdk/v7 v7.21.1
78
github.com/urfave/cli/v2 v2.3.0
89
)

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
99
github.com/dave/jennifer v1.6.1/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc=
1010
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1111
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
13+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
1214
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
1315
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
1416
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=

Diff for: lib/qiniu/upload.go

+43-11
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package qiniu
33
import (
44
"context"
55
"fmt"
6+
"github.com/dustin/go-humanize"
7+
"github.com/qiniu/go-sdk/v7/auth/qbox"
8+
"github.com/qiniu/go-sdk/v7/storage"
69
"io"
710
"os"
811
"path/filepath"
12+
"sync"
913
"time"
10-
11-
"github.com/qiniu/go-sdk/v7/auth/qbox"
12-
"github.com/qiniu/go-sdk/v7/storage"
1314
)
1415

1516
type Uploader struct {
@@ -24,9 +25,40 @@ type PutRsp struct {
2425
Bucket string
2526
}
2627

28+
type Assets struct {
29+
Path string
30+
Key string
31+
}
32+
2733
// UploadDir 上传一个文件夹
28-
func (u Uploader) UploadDir(zone *storage.Zone, bucket string, keyPrefix string, dirPath string) (err error) {
29-
fmt.Printf("upload dir: '%s' to bucket '%s', prefix: '%s'\n", dirPath, bucket, keyPrefix)
34+
func (u Uploader) UploadDir(zone *storage.Zone, bucket string, keyPrefix string, dirPath string, parallel int) (err error) {
35+
if parallel == 0 {
36+
parallel = 5
37+
}
38+
fmt.Printf("upload dir: '%s' to bucket '%s', prefix: '%s', parallel: '%d'\n", dirPath, bucket, keyPrefix, parallel)
39+
40+
as := make(chan Assets, 10)
41+
42+
// 上传
43+
var wg sync.WaitGroup
44+
for i := 0; i < parallel; i++ {
45+
wg.Add(1)
46+
go func() {
47+
defer wg.Done()
48+
for item := range as {
49+
if err != nil {
50+
return
51+
}
52+
start := time.Now()
53+
rsp, e := u.UploadFile(zone, bucket, item.Key, item.Path)
54+
if e != nil {
55+
err = fmt.Errorf("upload file '%s' error: %w", item.Key, err)
56+
return
57+
}
58+
fmt.Printf("uploaded file '%s' success, size: %s, spend time: %v\n", item.Key, humanize.IBytes(uint64(rsp.Fsize)), time.Since(start))
59+
}
60+
}()
61+
}
3062

3163
err = filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
3264
if err != nil {
@@ -41,19 +73,19 @@ func (u Uploader) UploadDir(zone *storage.Zone, bucket string, keyPrefix string,
4173
return err
4274
}
4375

44-
start := time.Now()
45-
_, err = u.UploadFile(zone, bucket, keyPrefix+reaPath, path)
46-
if err != nil {
47-
return fmt.Errorf("upload file '%s' error: %w", keyPrefix+reaPath, err)
76+
as <- Assets{
77+
Path: path,
78+
Key: keyPrefix + reaPath,
4879
}
49-
fmt.Printf("uploaded file '%s' success, spend time: %v\n", keyPrefix+reaPath, time.Since(start))
5080

5181
return nil
5282
})
5383
if err != nil {
5484
return
5585
}
56-
return
86+
87+
wg.Wait()
88+
return err
5789
}
5890

5991
// UploadFile 服务端表单直传 + 自定义回 JSON

Diff for: lib/qiniu/upload_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func TestUploadDir(t *testing.T) {
99
u := NewQiniu("", "").Uploader()
10-
err := u.UploadDir(&storage.ZoneHuadong, "nameimtest", "test/", `../qiniu/`)
10+
err := u.UploadDir(&storage.ZoneHuadong, "nameimtest", "test/", `../qiniu/`, 1)
1111
if err != nil {
1212
t.Fatal(err)
1313
}

Diff for: main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func main() {
99
c := cli.NewApp()
10-
c.Name = "qiniu plugin"
10+
c.Name = "qiniu drone plugin"
1111
c.Usage = ""
1212
c.Version = ""
1313

@@ -19,6 +19,7 @@ func main() {
1919
Zone: ctx.String("zone"),
2020
Prefix: ctx.String("prefix"),
2121
Dir: ctx.String("dir"),
22+
Parallel: ctx.Int("parallel"),
2223
}
2324

2425
return p.Exec()
@@ -55,6 +56,11 @@ func main() {
5556
Usage: "local dir",
5657
EnvVars: []string{"DIR", "PLUGIN_DIR"},
5758
},
59+
&cli.StringFlag{
60+
Name: "parallel",
61+
Usage: "parallel num",
62+
EnvVars: []string{"PARALLEL", "PLUGIN_PARALLEL"},
63+
},
5864
}
5965

6066
err := c.Run(os.Args)

Diff for: plugin.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Plugin struct {
1212
Bucket string
1313
Prefix string
1414
Dir string
15+
Parallel int
1516
}
1617

1718
func (p Plugin) Exec() error {
@@ -22,5 +23,5 @@ func (p Plugin) Exec() error {
2223
return errors.New("bad zone")
2324
}
2425

25-
return u.UploadDir(zone, p.Bucket, p.Prefix, p.Dir)
26+
return u.UploadDir(zone, p.Bucket, p.Prefix, p.Dir, p.Parallel)
2627
}

0 commit comments

Comments
 (0)