-
Notifications
You must be signed in to change notification settings - Fork 13
/
s3.go
149 lines (125 loc) · 3.42 KB
/
s3.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"github.com/aws/aws-sdk-go/aws"
)
import "github.com/aws/aws-sdk-go/aws/credentials"
import log "github.com/sirupsen/logrus"
import (
"fmt"
"strconv"
"strings"
"time"
)
type format int
const (
plainTextFormat format = iota
gzipFormat
)
type algorithm int
const (
noSuffixAlgorithm algorithm = iota
sha256SuffixAlgorithm
)
type s3Config struct {
credentials *credentials.Credentials
bucket *string
s3prefix *string
suffixAlgorithm algorithm
region *string
compress format
endpoint string
logLevel log.Level
timeFormat string
location *time.Location
autoCreateBucket bool
}
type S3Credential interface {
GetCredentials(accessID, secretkey, credentials string) (*credentials.Credentials, error)
}
type s3PluginConfig struct{}
var s3Creds S3Credential = &s3PluginConfig{}
func (c *s3PluginConfig) GetCredentials(accessKeyID, secretKey, credential string) (*credentials.Credentials, error) {
if credential != "" {
creds := credentials.NewSharedCredentials(credential, "default")
if _, err := creds.Get(); err != nil {
return nil, fmt.Errorf("[SharedCredentials] ERROR: %s", err)
}
return creds, nil
}
if !(accessKeyID == "" && secretKey == "") {
creds := credentials.NewStaticCredentials(accessKeyID, secretKey, "")
if _, err := creds.Get(); err != nil {
return nil, fmt.Errorf("[StaticCredentials] ERROR: %s", err)
}
return creds, nil
}
return nil, nil
}
func getS3Config(accessID, secretKey, credential, s3prefix, suffixAlgorithm, bucket, region, compress, endpoint, autoCreateBucket, logLevel, timeFormat, timeZone string) (*s3Config, error) {
conf := &s3Config{}
creds, err := s3Creds.GetCredentials(accessID, secretKey, credential)
if err != nil {
return nil, fmt.Errorf("Failed to create credentials")
}
conf.credentials = creds
if bucket == "" {
return nil, fmt.Errorf("Cannot specify empty string to bucket name")
}
conf.bucket = aws.String(bucket)
if s3prefix == "" {
return nil, fmt.Errorf("Cannot specify empty string to s3prefix")
}
conf.s3prefix = aws.String(s3prefix)
switch suffixAlgorithm {
case "sha256":
conf.suffixAlgorithm = sha256SuffixAlgorithm
default:
conf.suffixAlgorithm = noSuffixAlgorithm
}
if region == "" {
return nil, fmt.Errorf("Cannot specify empty string to region")
}
conf.region = aws.String(region)
switch compress {
case "gzip":
conf.compress = gzipFormat
default:
conf.compress = plainTextFormat
}
if endpoint != "" {
if strings.HasSuffix(endpoint, "amazonaws.com") {
return nil, fmt.Errorf("Endpoint is not supported for AWS S3. This parameter is intended for S3 compatible services. Use Region instead.")
}
conf.endpoint = endpoint
}
isAutoCreateBucket, err := strconv.ParseBool(autoCreateBucket)
if err != nil {
conf.autoCreateBucket = false
} else {
conf.autoCreateBucket = isAutoCreateBucket
}
if logLevel == "" {
logLevel = "info"
}
var level log.Level
if level, err = log.ParseLevel(logLevel); err != nil {
return nil, fmt.Errorf("invalid log level: %v", logLevel)
}
conf.logLevel = level
if timeFormat != "" {
conf.timeFormat = timeFormat
} else {
conf.timeFormat = "20060102/15"
}
if timeZone != "" {
loc, err := time.LoadLocation(timeZone)
if err != nil {
return nil, fmt.Errorf("invalid timeZone: %v", err)
} else {
conf.location = loc
}
} else {
conf.location = time.Local
}
return conf, nil
}