-
Notifications
You must be signed in to change notification settings - Fork 1
/
alioss.go
69 lines (58 loc) · 1.65 KB
/
alioss.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
package oss
import (
"errors"
"io"
"github.com/denverdino/aliyungo/oss"
"github.com/extrame/goblet"
)
type Alioss struct {
Region string `goblet:"region,beijing"`
AccessKeyId string `goblet:"access_id"`
AccessKeySecret string `goblet:"access_secret"`
}
var ossclient *oss.Client
func (r *Alioss) Init(server *goblet.Server) error {
ossclient = newSaver(r.Region, r.AccessKeyId, r.AccessKeySecret)
return nil
}
func (s *Alioss) AddCfgAndInit(server *goblet.Server) error {
server.AddConfig("alioss", &s)
return s.Init(server)
}
func newSaver(region, accessKeyId, accessKeySecret string) (l *oss.Client) {
reg := oss.Beijing
switch region {
case "beijing":
reg = oss.Beijing
case "qingdao":
reg = oss.Qingdao
case "shanghai":
reg = oss.Shanghai
case "shenzheng":
reg = oss.Shenzhen
case "hangzhou":
reg = oss.Hangzhou
case "hongkong":
reg = oss.Hongkong
}
return oss.NewOSSClient(reg, false, accessKeyId, accessKeySecret, false)
}
func InitSaver(region, accessKeyId, accessKeySecret string) {
ossclient = newSaver(region, accessKeyId, accessKeySecret)
}
func PutFile(bucket, path string, file io.Reader, size int64) error {
if ossclient == nil {
errors.New("oss init failed,please check your config file or call InitSaver munal!")
}
bkt := ossclient.Bucket(bucket)
err := bkt.PutReader(path, file, size, "application/octet-stream", oss.Private, oss.Options{})
return err
}
func GetFile(bucket, path string) (io.ReadCloser, error) {
if ossclient == nil {
errors.New("oss init failed,please check your config file or call InitSaver munal!")
}
bkt := ossclient.Bucket(bucket)
res, err := bkt.GetReader(path)
return res, err
}