-
Notifications
You must be signed in to change notification settings - Fork 120
/
main.go
53 lines (49 loc) · 1.3 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
package alioss
import (
"bytes"
"errors"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"gopkg.in/go-playground/validator.v9"
)
//OSS config
type OssConfig struct {
Endpoint string `validate:"required"`
AccessKeyId string `validate:"required"`
AccessKeySecret string `validate:"required"`
BucketName string `validate:"required"`
PrivateServerDomain string `validate:"required"`
}
type OssHelper struct {
client *oss.Client
config OssConfig
}
func NewOssHelper(c OssConfig, options ...oss.ClientOption) (*OssHelper, error) {
validate := validator.New()
err := validate.Struct(c)
if err != nil {
return nil, errors.New("alioss uploader config not set")
}
client, e := oss.New(c.Endpoint, c.AccessKeyId, c.AccessKeySecret, options...)
if e != nil {
return nil, e
}
return &OssHelper{
client: client,
config: c,
}, nil
}
func (h OssHelper) Upload(objectKey string, b []byte, options ...oss.Option) (url string, err error) {
// 获取存储空间。
bucket, err := h.client.Bucket(h.config.BucketName)
if err != nil {
return "", err
}
// 上传文件。
reader := bytes.NewReader(b)
err = bucket.PutObject(objectKey, reader, options...)
if err != nil {
return "", err
}
return fmt.Sprintf("https://%s/%s", h.config.PrivateServerDomain, objectKey), nil
}