-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupyun.go
58 lines (47 loc) · 1.29 KB
/
upyun.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
// by liudan,modify by daiping @ 2016-06-28
package common
import (
"errors"
"io"
"net/http"
"strings"
"third/upyun"
)
type UpyunParams struct {
UpBucket string
UpUsername string
UpPassword string
}
var _upyun *upyun.UpYun
func (this *UpyunParams) InitUpyun() {
if _upyun == nil {
_upyun = upyun.NewUpYun(this.UpBucket, this.UpUsername, this.UpPassword)
}
return
}
func (this *UpyunParams) UploadFile(domain_prefix, key string, value io.Reader, headers map[string]string) (string, http.Header, error) {
if _upyun == nil {
return "", nil, errors.New("_upyun not init")
}
header, err := _upyun.Put(key, value, true, headers)
if err != nil {
return "", nil, err
} else {
return this.FileUrl(domain_prefix, key), header, nil
}
}
func (this *UpyunParams) UploadFileFromUrl(domain_prefix, key, addr string, headers map[string]string) (string, http.Header, error) {
if _upyun == nil {
return "", nil, errors.New("_upyun not init")
}
_, data, err := SendRequest("GET", addr, nil, nil, nil)
if err != nil {
return "", nil, err
}
r := strings.NewReader(data)
return this.UploadFile(domain_prefix, key, r, headers)
}
func (this *UpyunParams) FileUrl(domain_prefix, key string) string {
//like http://img3.codoon.com/aaaaaaaaa
return "http://" + domain_prefix + ".codoon.com/" + key
}