/
main.go
81 lines (71 loc) · 1.51 KB
/
main.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
/// 七牛客户端
package main
import (
"encoding/json"
"flag"
"fmt"
"golang.org/x/net/context"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"qiniupkg.com/api.v7/kodocli"
)
const (
UPTOKEN_URL = "http://qiniu-manager.herokuapp.com/uptoken"
DOMAIN = "7xkp7e.com1.z0.glb.clouddn.com"
)
type Uptoken struct {
Value string `json:"uptoken"`
}
func usage() {
fmt.Printf(`
Upload file to qiniu Server.
Usage:
%s [-k key] filename
Options:
-k The filename used to save file on qiniu server.
`, os.Args[0])
}
func main() {
var key string
flag.StringVar(&key, "k", "", "The key of upload file")
flag.Parse()
filename := flag.Arg(0)
if filename == "" {
usage()
return
}
fileAbsPath, _ := filepath.Abs(filename)
if _, err := os.Stat(fileAbsPath); os.IsNotExist(err) {
fmt.Println("No such file to upload: ", filename)
return
}
if key == "" {
key = filepath.Base(fileAbsPath)
}
fmt.Println("Upload file with key: ", key)
resp, err := http.Get(UPTOKEN_URL + "?key=" + key)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
var uptoken Uptoken
json.Unmarshal(body, &uptoken)
zone := 0
uploader := kodocli.NewUploader(zone, nil)
ctx := context.Background()
err = uploader.PutFile(ctx, nil, uptoken.Value, key, fileAbsPath, nil)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Upload file success, You can download it from:")
fmt.Println(kodocli.MakeBaseUrl(DOMAIN, key))
}