/
aliyun.go
79 lines (59 loc) · 1.59 KB
/
aliyun.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
package aliyun
import (
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/qor/qor/media_library"
"github.com/qor/qor/media_library/aliyun/config"
"github.com/sunfmin/ali-oss"
)
type OSS struct {
media_library.Base
}
var aliossClient *alioss.Client
func init() {
aliossClient = alioss.NewClient(config.AliOSSAccessKey, config.AliOSSAccessSecret)
}
func getBucket(option *media_library.Option) string {
if bucket := option.Get("bucket"); bucket != "" {
return bucket
}
return config.AliOSSBucket
}
func getEndpoint(option *media_library.Option) string {
if endpoint := option.Get("endpoint"); endpoint != "" {
return endpoint
}
return getBucket(option) + "." + config.AliOSSRegion
}
func (s OSS) GetURLTemplate(option *media_library.Option) (path string) {
if path = option.Get("URL"); path == "" {
path = "/{{class}}/{{primary_key}}/{{column}}/{{filename_with_hash}}"
}
path = "//" + getEndpoint(option) + path
return
}
func (s OSS) Store(url string, option *media_library.Option, reader io.Reader) (err error) {
path := strings.Replace(url, "//"+getEndpoint(option), "", -1)
bucket := alioss.NewBucket(config.AliOSSBucket, alioss.BucketRegion(config.AliOSSRegion), aliossClient)
err = bucket.Put(path, reader, nil)
if err != nil {
return
}
return
}
func (s OSS) Retrieve(url string) (*os.File, error) {
response, err := http.Get("http:" + url)
if err != nil {
return nil, err
}
defer response.Body.Close()
if file, err := ioutil.TempFile("/tmp", "OSS"); err == nil {
_, err := io.Copy(file, response.Body)
return file, err
} else {
return nil, err
}
}