-
Notifications
You must be signed in to change notification settings - Fork 199
/
qiniu-upload.go
148 lines (123 loc) · 3.51 KB
/
qiniu-upload.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
package main
import (
"flag"
"fmt"
"os"
"qiniu/api.v6/auth/digest"
"qiniu/api.v6/conf"
rio "qiniu/api.v6/resumable/io"
"qiniu/api.v6/rs"
"qiniu/rpc"
"qshell"
"runtime"
"sync"
"time"
)
var upSettings = rio.Settings{
Workers: 1,
ChunkSize: 4 * 1024 * 1024,
TryTimes: 7,
}
func main() {
//set cpu count
runtime.GOMAXPROCS(runtime.NumCPU())
var bucket string
var key string
var localFile string
var mimeType string
var upHost string
var overwrite bool
var blocks int
flag.StringVar(&bucket, "bucket", "", "bucket to save to")
flag.StringVar(&key, "key", "", "key to save in bucket")
flag.StringVar(&localFile, "file", "", "local file to upload")
flag.StringVar(&mimeType, "mime", "", "mime type to set for file")
flag.StringVar(&upHost, "host", "", "qiniu upload host")
flag.StringVar(&upHost, "overwrite", "", "overwrite the existing file")
flag.IntVar(&blocks, "block", 1, "block count to upload simultaneously")
flag.Parse()
if bucket == "" || key == "" || localFile == "" {
fmt.Println("Err: please specify bucket, key and local file")
return
}
if blocks > 0 {
upSettings.Workers = blocks
}
ResumablePut(bucket, key, localFile, mimeType, upHost, overwrite)
}
func ResumablePut(bucket, key, localFile, mimeType, upHost string, overwrite bool) {
account := qshell.Account{}
account, gErr := qshell.GetAccount()
if gErr != nil {
fmt.Println(gErr)
return
}
fStat, statErr := os.Stat(localFile)
if statErr != nil {
fmt.Println("Local file error", statErr)
return
}
fsize := fStat.Size()
mac := digest.Mac{account.AccessKey, []byte(account.SecretKey)}
policy := rs.PutPolicy{}
policy.Expires = 3600 * 24 * 5
if overwrite {
policy.Scope = fmt.Sprintf("%s:%s", bucket, key)
} else {
policy.Scope = bucket
}
putExtra := rio.PutExtra{}
if mimeType != "" {
putExtra.MimeType = mimeType
}
if upHost != "" {
conf.UP_HOST = upHost
}
progressHandler := ProgressHandler{
rwLock: &sync.RWMutex{},
fsize: fsize,
offsets: make(map[int]int64, 0),
}
putExtra.Notify = progressHandler.Notify
putExtra.NotifyErr = progressHandler.NotifyErr
uptoken := policy.Token(&mac)
putRet := rio.PutRet{}
startTime := time.Now()
rio.SetSettings(&upSettings)
putClient := rio.NewClient(uptoken, "")
fmt.Println(fmt.Sprintf("Uploading %s => %s : %s ...", localFile, bucket, key))
err := rio.PutFile(putClient, nil, &putRet, key, localFile, &putExtra)
fmt.Println()
if err != nil {
if v, ok := err.(*rpc.ErrorInfo); ok {
fmt.Println(fmt.Sprintf("Put file error, %d %s, Reqid: %s", v.Code, v.Err, v.Reqid))
} else {
fmt.Println("Put file error,", err)
}
} else {
fmt.Println("Put file", localFile, "=>", bucket, ":", putRet.Key, "(", putRet.Hash, ")", "success!")
}
lastNano := time.Now().UnixNano() - startTime.UnixNano()
lastTime := fmt.Sprintf("%.2f", float32(lastNano)/1e9)
avgSpeed := fmt.Sprintf("%.1f", float32(fsize)*1e6/float32(lastNano))
fmt.Println("Last time:", lastTime, "s, Average Speed:", avgSpeed, "KB/s")
}
type ProgressHandler struct {
rwLock *sync.RWMutex
offsets map[int]int64
fsize int64
}
func (this *ProgressHandler) Notify(blkIdx int, blkSize int, ret *rio.BlkputRet) {
this.rwLock.Lock()
defer this.rwLock.Unlock()
this.offsets[blkIdx] = int64(ret.Offset)
var uploaded int64
for _, offset := range this.offsets {
uploaded += offset
}
percent := fmt.Sprintf("\rProgress: %.2f%%", float64(uploaded)/float64(this.fsize)*100)
fmt.Print(percent)
os.Stdout.Sync()
}
func (this *ProgressHandler) NotifyErr(blkIdx int, blkSize int, err error) {
}