-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
86 lines (72 loc) · 1.9 KB
/
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
package filemgr
import (
"fmt"
"github.com/pkg/errors"
"log"
"os"
"strings"
"github.com/ufilesdk-dev/ufile-gosdk"
)
const (
putUpload = iota
postUpload
mput
asyncmput
)
func (c *Client) Upload(file string) (err error) {
file = strings.TrimSpace(file)
if file == "" {
fmt.Println("[ERR] File name should be specified. ")
return errors.Errorf("[ERR] File name should be specified. ")
}
req, err := ufsdk.NewFileRequest(c.Conf, nil)
if err != nil {
fmt.Println(err.Error())
return err
}
fileInfo, err := os.Stat(file)
if err != nil {
fmt.Printf("[ERR] %s not exist. \n", file)
return err
}
tmp := strings.Split(file, "/")
fileName := tmp[len(tmp) - 1]
uploadKey := c.Namespace + "@" + fileName
var uploadType int
// 大于100M
if fileInfo.Size() > 104857600 {
uploadType = 2
}
scheduleUpload(file, uploadKey, uploadType, req)
return nil
}
func scheduleUpload(filePath, keyName string, uploadType int, req *ufsdk.UFileRequest) {
log.Println("上传的文件 Key 为:", keyName)
var err error
switch uploadType {
case putUpload:
// 文件小于100M
log.Println("正在使用PUT接口上传文件...")
err = req.PutFile(filePath, keyName, "")
break
case postUpload:
// 文件小于100M
log.Println("正在使用 POST 接口上传文件...")
err = req.PostFile(filePath, keyName, "")
case mput:
// 文件大于100M
log.Println("正在使用同步分片上传接口上传文件...")
err = req.MPut(filePath, keyName, "")
case asyncmput:
// 文件大于100M
log.Println("正在使用异步分片上传接口上传文件...")
err = req.AsyncMPut(filePath, keyName, "")
}
if err != nil {
log.Println("文件上传失败!!,错误信息为:", err.Error())
//如果 err 给出的提示信息不够,你可 dump 整个 response 出来查看 http 的返回。
log.Printf("%s\n", req.DumpResponse(true))
return
}
log.Println("文件上传成功!!")
}