-
Notifications
You must be signed in to change notification settings - Fork 12
/
driver.go
61 lines (49 loc) · 1.19 KB
/
driver.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
package s3
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/eleven26/goss/core"
"github.com/spf13/viper"
)
type Driver struct {
Viper *viper.Viper
}
func NewDriver(opts ...core.Option) core.Driver {
driver := &Driver{}
for _, option := range opts {
option(driver)
}
return driver
}
func (d *Driver) Storage() (core.Storage, error) {
conf := getConfig(d.Viper)
if conf.Bucket == "" || conf.AccessKey == "" || conf.SecretKey == "" || conf.Endpoint == "" || conf.Region == "" {
return nil, core.ErrorConfigEmpty
}
creds := credentials.NewStaticCredentials(conf.AccessKey, conf.SecretKey, "")
_, err := creds.Get()
if err != nil {
return nil, err
}
awsConfig := &aws.Config{
Region: aws.String(conf.Region),
Endpoint: aws.String(conf.Endpoint),
DisableSSL: aws.Bool(true),
Credentials: creds,
}
sess, err := session.NewSession(awsConfig)
if err != nil {
return nil, err
}
svc := s3.New(sess)
store := Store{
s3: svc,
config: *conf,
}
return core.NewStorage(&store), nil
}
func (d Driver) Name() string {
return "s3"
}