/
provider.go
100 lines (83 loc) · 2.13 KB
/
provider.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
package provider
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
var urlEncode = url.QueryEscape
var corsAllowHeaders = []string{"content-type", "content-disposition", "x-amz-acl"}
const (
defaultUploadExp = time.Hour
defaultDownloadExp = time.Hour * 24
)
// Object is the basic operation unit
type Object struct {
Key string // remote file path
ETag string // file md5
FilePath string // local file path
Type string // local file type, added or changed
}
type Provider interface {
SetupCORS() error
Head(object string) (*Object, error)
List(prefix string) ([]Object, error)
Move(object, newObject string) error
SignedPutURL(key, filetype string, filesize int64, public bool) (url string, headers http.Header, err error)
SignedGetURL(key, filename string) (url string, err error)
PublicURL(key string) (url string)
ObjectDelete(key string) error
ObjectsDelete(keys []string) error
}
type Config struct {
Provider string
Bucket string
Endpoint string
Region string
CustomHost string
AccessKey string
AccessSecret string
}
func (c *Config) Clone() *Config {
clone := *c
return &clone
}
func (c *Config) WithCustomHost(s string) *Config {
c.CustomHost = s
return c
}
type Constructor func(provider *Config) (Provider, error)
var supportProviders = map[string]Constructor{
"COS": NewCOSProvider,
"KODO": NewKODOProvider,
"MINIO": NewMINIOProvider,
"NOS": NewNOSProvider,
"OBS": NewOBSProvider,
"OSS": NewOSSProvider,
"S3": NewS3Provider,
"US3": NewUS3Provider,
"USS": NewUSSProvider,
//"od": NewODProvider,
//"gd": NewGDProvider,
}
func New(conf *Config) (Provider, error) {
if conf.Region == "" {
conf.Region = "auto"
}
if conf.CustomHost != "" && !strings.Contains(conf.CustomHost, "://") {
conf.CustomHost = "http://" + conf.CustomHost
}
constructor, ok := supportProviders[strings.ToUpper(conf.Provider)]
if !ok {
return nil, fmt.Errorf("provider %s not found", conf.Provider)
}
return constructor(conf)
}
func GetProviders() []string {
keys := make([]string, 0)
for k := range supportProviders {
keys = append(keys, k)
}
return keys
}