-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3s.go
223 lines (187 loc) · 4.11 KB
/
s3s.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package s3s
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
const (
DEFAULT_THREAD_COUNT = 150
)
type FormatType int
const (
FormatTypeJSON FormatType = iota + 1
FormatTypeCSV
FormatTypeALBLogs
FormatTypeCFLogs
)
type Query struct {
FormatType FormatType
Query string
Since time.Time
Until time.Time
}
type Option struct {
IsDryRun bool
IsCountMode bool
}
type Client struct {
s3 *s3.Client
}
func New(ctx context.Context) (*Client, error) {
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
api := s3.NewFromConfig(cfg)
client := &Client{
s3: api,
}
return client, nil
}
type Result struct {
Count int
Bytes int64
}
func (c *Client) Run(ctx context.Context, prefixes []string, query *Query, option *Option) (*Result, error) {
result := &Result{}
switch query.FormatType {
case FormatTypeALBLogs:
albPrefixes, err := c.OptimizateALBPrefixes(ctx, prefixes, query)
if err != nil {
return nil, errors.WithStack(err)
}
if albPrefixes != nil {
prefixes = albPrefixes
}
case FormatTypeCFLogs:
cfPrefixes, err := c.OptimizateCFPrefixes(ctx, prefixes, query)
if err != nil {
return nil, errors.WithStack(err)
}
if cfPrefixes != nil {
prefixes = cfPrefixes
}
}
pathCH := make(chan s3Object, DEFAULT_THREAD_COUNT)
eg, egctx := errgroup.WithContext(ctx)
eg.Go(func() error {
if err := c.getBucketKeys(egctx, pathCH, prefixes, query); err != nil {
return errors.WithStack(err)
}
return nil
})
jsonCH := make(chan []byte, DEFAULT_THREAD_COUNT)
if !option.IsDryRun {
eg.Go(func() error {
if err := c.execS3Select(egctx, pathCH, jsonCH, query, option); err != nil {
return errors.WithStack(err)
}
return nil
})
} else {
eg.Go(func() error {
for c := range pathCH {
result.Bytes += c.Size
result.Count++
}
return nil
})
}
if !option.IsDryRun {
eg.Go(func() error {
if err := c.writeOutput(egctx, jsonCH); err != nil {
return errors.WithStack(err)
}
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, errors.WithStack(err)
}
return result, nil
}
func (c *Client) getBucketKeys(ctx context.Context, in chan<- s3Object, prefixes []string, info *Query) error {
defer close(in)
eg, egctx := errgroup.WithContext(ctx)
eg.SetLimit(DEFAULT_THREAD_COUNT)
for _, prefix := range prefixes {
prefix := prefix
eg.Go(func() error {
u, err := url.Parse(prefix)
if err != nil {
return errors.WithStack(err)
}
bucket := u.Hostname()
newPrefix := strings.TrimPrefix(u.Path, "/")
if c.GetS3Keys(egctx, in, bucket, newPrefix, info); err != nil {
return errors.WithStack(err)
}
return nil
})
}
if err := eg.Wait(); err != nil {
return errors.WithStack(err)
}
return nil
}
func (c *Client) execS3Select(ctx context.Context, out <-chan s3Object, in chan<- []byte, query *Query, option *Option) error {
defer close(in)
eg, egctx := errgroup.WithContext(ctx)
eg.SetLimit(DEFAULT_THREAD_COUNT)
LOOP:
for {
select {
case s3object, ok := <-out:
if !ok {
break LOOP
}
var input *s3SelectInput
switch query.FormatType {
case FormatTypeJSON:
input = &s3SelectInput{
Bucket: s3object.Bucket,
Key: s3object.Key,
Query: query.Query,
}
case FormatTypeCSV, FormatTypeALBLogs, FormatTypeCFLogs:
input = &s3SelectInput{
Bucket: s3object.Bucket,
Key: s3object.Key,
Query: query.Query,
}
}
input.FormatType = query.FormatType
eg.Go(func() error {
if err := c.s3Select(egctx, in, input, option); err != nil {
return errors.WithStack(err)
}
return nil
})
case <-ctx.Done():
return nil
}
}
if err := eg.Wait(); err != nil {
return errors.WithStack(err)
}
return nil
}
func (c *Client) writeOutput(ctx context.Context, out <-chan []byte) error {
for {
select {
case json, ok := <-out:
if !ok {
return nil
}
fmt.Println(string(json))
case <-ctx.Done():
return nil
}
}
}