forked from ywdarklord/Go-Example
/
main.go
155 lines (129 loc) · 4.12 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
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
149
150
151
152
153
154
package main
import (
"log"
"fmt"
"net/http"
"encoding/base64"
"encoding/json"
"github.com/qiniu/api/rs"
. "github.com/qiniu/api/conf"
)
const (
BUCKET = "APPLY YOUR BUCKET NAME HERE" // change to own space name
DOMAIN = "APPLY YOUR DOMAIN HERE" // For example: myspace.qiniudn.com
)
// --------------------------------------------------------------------------------
func init() {
ACCESS_KEY = "" // Apply Access key here
SECRET_KEY = "" // Apply Secret key here
if ACCESS_KEY == "" || SECRET_KEY == "" {
panic("require ACCESS_KEY & SECRET_KEY")
}
}
// --------------------------------------------------------------------------------
// HTML code that will be shown on the webpage
//Simple upload without assigning key for the image you want to upload
var uploadFormFmt = `
<html>
<body>
<form method="post" action="http://up.qiniu.com/" enctype="multipart/form-data">
<input name="token" type="hidden" value="%s">
Image to upload: <input name="file" type="file"/>
<input type="submit" value="Upload">
</form>
</body>
</html>
`
//Assign a key for the image you want to upload
var uploadWithKeyFormFmt = `
<html>
<body>
<form method="post" action="http://up.qiniu.com/" enctype="multipart/form-data">
<input name="token" type="hidden" value="%s">
Image key in qiniu cloud storage: <input name="key" value="foo bar.jpg"><br>
Image to upload: <input name="file" type="file"/>
<input type="submit" value="Upload">
</form>
</body>
</html>
`
//Assign both key and custom field
var uploadWithkeyAndCustomFieldFmt = `
<html>
<body>
<form method="post" action="http://up.qiniu.com/" enctype="multipart/form-data">
<input name="token" type="hidden" value="%s">
<input name="x:custom_field_name" value="x:custom_field_name">
Image key in qiniu cloud storage: <input name="key" value="foo bar.jpg"><br>
Image to upload: <input name="file" type="file"/>
<input type="submit" value="Upload">
</form>
</body>
</html>
`
var returnPageFmt = `
<html>
<body>
<p>%s
<p>ImageDownloadUrl: %s
<p><a href="/upload">Back to upload</a>
<p><a href="/upload2">Back to uploadWithKey</a>
<p><a href="/upload3">Back to uploadWithkeyAndCustomField</a>
<p><img src="%s">
</body>
</html>
`
type UploadRet struct {
Key string `json:"key"`
}
func handleReturn(w http.ResponseWriter, req *http.Request) {
ret := req.FormValue("upload_ret")
b, err := base64.URLEncoding.DecodeString(ret)
if err != nil {
w.WriteHeader(404)
return
}
var uploadRet UploadRet
err = json.Unmarshal(b, &uploadRet)
if err != nil {
w.WriteHeader(404)
return
}
policy := rs.GetPolicy{Scope: "*/" + uploadRet.Key}
img := policy.MakeRequest(rs.MakeBaseUrl(DOMAIN, uploadRet.Key))
returnPage := fmt.Sprintf(returnPageFmt, string(b), img, img)
w.Write([]byte(returnPage))
}
func handleUpload(w http.ResponseWriter, req *http.Request) {
policy := rs.PutPolicy{Scope: BUCKET, ReturnUrl: "http://localhost:8765/uploaded"}
token := policy.Token()
log.Println("token:", token)
uploadForm := fmt.Sprintf(uploadFormFmt, token)
w.Write([]byte(uploadForm))
}
func handleUploadWithKey(w http.ResponseWriter, req *http.Request) {
policy := rs.PutPolicy{Scope: BUCKET, ReturnUrl: "http://localhost:8765/uploaded"}
token := policy.Token()
log.Println("token:", token)
uploadForm := fmt.Sprintf(uploadWithKeyFormFmt, token)
w.Write([]byte(uploadForm))
}
func handleUploadWithKeyAndCustomField(w http.ResponseWriter, req *http.Request) {
policy := rs.PutPolicy{Scope: BUCKET, ReturnUrl: "http://localhost:8765/uploaded"}
token := policy.Token()
log.Println("token:", token)
uploadForm := fmt.Sprintf(uploadWithkeyAndCustomFieldFmt, token)
w.Write([]byte(uploadForm))
}
func handleDefault(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/upload", 302)
}
func main() {
http.HandleFunc("/", handleDefault)
http.HandleFunc("/upload", handleUpload)
http.HandleFunc("/upload2", handleUploadWithKey)
http.HandleFunc("/upload3", handleUploadWithKeyAndCustomField)
http.HandleFunc("/uploaded", handleReturn)
log.Fatal(http.ListenAndServe(":8765", nil))
}
// --------------------------------------------------------------------------------