forked from WasabiAiR/stow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
78 lines (72 loc) · 1.69 KB
/
config.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
package azure
import (
"errors"
"net/url"
az "github.com/Azure/azure-sdk-for-go/storage"
"github.com/graymeta/stow"
)
// ConfigAccount and ConfigKey are the supported configuration items for
// Azure blob storage.
const (
ConfigAccount = "account"
ConfigKey = "key"
)
// Kind is the kind of Location this package provides.
const Kind = "azure"
func init() {
validatefn := func(config stow.Config) error {
_, ok := config.Config(ConfigAccount)
if !ok {
return errors.New("missing account id")
}
_, ok = config.Config(ConfigKey)
if !ok {
return errors.New("missing auth key")
}
return nil
}
makefn := func(config stow.Config) (stow.Location, error) {
_, ok := config.Config(ConfigAccount)
if !ok {
return nil, errors.New("missing account id")
}
_, ok = config.Config(ConfigKey)
if !ok {
return nil, errors.New("missing auth key")
}
l := &location{
config: config,
}
var err error
l.client, err = newBlobStorageClient(l.config)
if err != nil {
return nil, err
}
// test the connection
_, _, err = l.Containers("", stow.CursorStart, 1)
if err != nil {
return nil, err
}
return l, nil
}
kindfn := func(u *url.URL) bool {
return u.Scheme == Kind
}
stow.Register(Kind, makefn, kindfn, validatefn)
}
func newBlobStorageClient(cfg stow.Config) (*az.BlobStorageClient, error) {
acc, ok := cfg.Config(ConfigAccount)
if !ok {
return nil, errors.New("missing account id")
}
key, ok := cfg.Config(ConfigKey)
if !ok {
return nil, errors.New("missing auth key")
}
basicClient, err := az.NewBasicClient(acc, key)
if err != nil {
return nil, errors.New("bad credentials")
}
client := basicClient.GetBlobService()
return &client, err
}