/
unioss.go
54 lines (46 loc) · 1.08 KB
/
unioss.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
package unioss
import (
"errors"
"io"
)
const (
ALIYUN = "aliyun"
TENCENT = "tencent"
QINIU = "qiniu"
)
var currentStorage Storage
type Storage interface {
GetObjectToFile(objectKey, downloadedFileName string) error
DeleteObject(objectKey string) error
PutObject(objectKey string, reader io.Reader) error
PutObjectFromFile(objectKey, filePath string) error
IsExists(objectKey string) (bool, error)
GetObjectUrl(objectKey string) string
}
func NewStorage(ossName string, config Config) error {
if config.KeyID == "" || config.KeySecret == "" || config.Bucket == "" {
return errors.New("configuration not correct")
}
var err error
switch ossName {
case ALIYUN:
currentStorage, err = newAliyun(config)
case TENCENT:
currentStorage, err = newTencent(config)
case QINIU:
currentStorage, err = newQiniu(config)
default:
return errors.New("driver not exists")
}
if err != nil {
currentStorage = nil
return err
}
return nil
}
func GetStorage() (Storage, error) {
if currentStorage != nil {
return currentStorage, nil
}
return nil, errors.New("driver not exists")
}