forked from cosmo0920/fluent-bit-go-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
134 lines (114 loc) · 3.25 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
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 s3Config struct {
credentials *credentials.Credentials
bucket *string
s3prefix *string
region *string
compress format
endpoint string
logLevel log.Level
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) {
var creds *credentials.Credentials
if credential != "" {
creds = credentials.NewSharedCredentials(credential, "default")
if _, err := creds.Get(); err != nil {
fmt.Println("[SharedCredentials] ERROR:", err)
} else {
return creds, nil
}
} else if !(accessKeyID == "" && secretKey == "") {
creds = credentials.NewStaticCredentials(accessKeyID, secretKey, "")
if _, err := creds.Get(); err != nil {
fmt.Println("[StaticCredentials] ERROR:", err)
} else {
return creds, nil
}
} else {
creds = credentials.NewEnvCredentials()
if _, err := creds.Get(); err != nil {
fmt.Println("[EnvCredentials] ERROR:", err)
} else {
return creds, nil
}
}
return nil, fmt.Errorf("Failed to create credentials")
}
func getS3Config(accessID, secretKey, credential, s3prefix, bucket, region, compress, endpoint, autoCreateBucket, logLevel, 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)
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 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
}