-
Notifications
You must be signed in to change notification settings - Fork 14
/
bcs.go
103 lines (98 loc) · 2.26 KB
/
bcs.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
package bcsgo
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"strings"
)
type BCS struct {
ak, sk string
httpClient *HttpClient
}
func NewBCS(ak, sk string) *BCS {
return &BCS{ak, sk, NewHttpClient()}
}
func (this *BCS) ListBuckets() ([]*Bucket, error) {
link := this.getUrl()
_, data, err := this.httpClient.Get(link)
if err != nil {
return nil, err
} else {
list := []*Bucket{}
err := json.Unmarshal(data, &list)
for i, _ := range list {
list[i].bcs = this
}
return list, err
}
}
func (this *BCS) Bucket(bucketName string) *Bucket {
return &Bucket{this, bucketName}
}
func (this *BCS) getUrl() string {
return this.restUrl(GET, "", "/")
}
func (this *BCS) restUrl(method, bucket, object string) string {
return this.urlWithSign(method, bucket, object, "", "", "")
}
func (this *BCS) restUrlExtra(method, bucket, object, time, ip, size string) string {
return this.urlWithSign(method, bucket, object, time, ip, size)
}
func (this *BCS) urlWithSign(method, bucket, object, time, ip, size string) string {
return fmt.Sprintf("%s?sign=%s", this.urlWithoutSign(bucket, object), this.sign(method, bucket, object, time, ip, size))
}
func (this *BCS) urlWithoutSign(bucket, object string) string {
return fmt.Sprintf("%s/%s%s", BCS_HOST, bucket, "/"+url.QueryEscape(object[1:]))
// return fmt.Sprintf("%s/%s%s", BCS_HOST, bucket, "/"+object[1:])
}
func (this *BCS) sign(m, b, o, t, i, s string) string {
flag := ""
ss := ""
flag += "M"
ss += "Method=" + m + "\n"
flag += "B"
ss += "Bucket=" + b + "\n"
flag += "O"
ss += "Object=" + o + "\n"
if t != "" {
flag += "T"
ss += "Time=" + t + "\n"
}
if i != "" {
flag += "I"
ss += "Ip=" + i + "\n"
}
if s != "" {
flag += "S"
ss += "Size=" + s + "\n"
}
ss = flag + "\n" + ss
h := func(sk, body string) string {
hash := hmac.New(sha1.New, []byte(sk))
hash.Write([]byte(body))
digest := hash.Sum(nil)
sign := base64.StdEncoding.EncodeToString(digest)
sign = strings.TrimSpace(sign)
sign = url.QueryEscape(sign)
return sign
}
sign := h(this.sk, ss)
final := fmt.Sprintf(
"%s:%s:%s",
flag,
this.ak,
sign)
if t != "" {
final += "&time=" + t
}
if i != "" {
final += "&ip=" + i
}
if s != "" {
final += "&size=" + s
}
return final
}