forked from segmentio/analytics-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths3clientconfig.go
64 lines (53 loc) · 1.38 KB
/
s3clientconfig.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
package analytics
import (
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/session"
)
// MB is the number of bytes in one megabyte.
const MB = 1024 * 1024
// Given a config object as argument the function will set all zero-values to
// their defaults and return the modified object.
func makeS3ClientConfig(c S3ClientConfig) (S3ClientConfig, error) {
c.Config = makeConfig(c.Config)
if c.S3.Stage != strings.ToLower(c.S3.Stage) {
return c, fmt.Errorf("Stage should be lowercased")
}
if c.S3.Stage == "" {
c.S3.Stage = "dev"
}
if c.S3.MaxBatchBytes == 0 {
c.S3.MaxBatchBytes = 20 * MB
}
if c.S3.Bucket == "" {
c.S3.Bucket = "fh-analytics-" + c.S3.Stage
}
if c.S3.Stream != strings.ToLower(c.S3.Stream) {
return c, fmt.Errorf("Stream should be lowercased")
}
if c.S3.Stream == "" {
c.S3.Stream = "haring"
}
if c.S3.KeyConstructor == nil {
c.S3.KeyConstructor = func(now func() Time, uid func() string) string {
ts := time.Time(now())
return fmt.Sprintf(
"analytics/%s/bulk/%s/json/%d/%02d/%02d/%02d/%d-%s.json.gz",
strings.ToUpper(c.S3.Stage),
c.S3.Stream,
ts.Year(), ts.Month(), ts.Day(), ts.Hour(),
ts.Unix(),
uid(),
)
}
}
if c.S3.Session == nil {
var err error
c.S3.Session, err = session.NewSession()
if err != nil {
return c, fmt.Errorf("analytics: creating s3 client session failed: %w", err)
}
}
return c, nil
}