forked from benthosdev/benthos
/
output_s3.go
437 lines (389 loc) · 14.4 KB
/
output_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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package aws
import (
"bytes"
"context"
"fmt"
"net/url"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/benthosdev/benthos/v4/internal/bloblang/query"
"github.com/benthosdev/benthos/v4/internal/component"
"github.com/benthosdev/benthos/v4/internal/component/output"
"github.com/benthosdev/benthos/v4/internal/impl/aws/config"
"github.com/benthosdev/benthos/v4/public/service"
)
const (
// S3 Output Fields
s3oFieldBucket = "bucket"
s3oFieldForcePathStyleURLs = "force_path_style_urls"
s3oFieldPath = "path"
s3oFieldTags = "tags"
s3oFieldContentType = "content_type"
s3oFieldContentEncoding = "content_encoding"
s3oFieldCacheControl = "cache_control"
s3oFieldContentDisposition = "content_disposition"
s3oFieldContentLanguage = "content_language"
s3oFieldWebsiteRedirectLocation = "website_redirect_location"
s3oFieldMetadata = "metadata"
s3oFieldStorageClass = "storage_class"
s3oFieldTimeout = "timeout"
s3oFieldKMSKeyID = "kms_key_id"
s3oFieldServerSideEncryption = "server_side_encryption"
s3oFieldBatching = "batching"
)
type s3TagPair struct {
key string
value *service.InterpolatedString
}
type s3oConfig struct {
Bucket string
Path *service.InterpolatedString
Tags []s3TagPair
ContentType *service.InterpolatedString
ContentEncoding *service.InterpolatedString
CacheControl *service.InterpolatedString
ContentDisposition *service.InterpolatedString
ContentLanguage *service.InterpolatedString
WebsiteRedirectLocation *service.InterpolatedString
Metadata *service.MetadataExcludeFilter
StorageClass *service.InterpolatedString
Timeout time.Duration
KMSKeyID string
ServerSideEncryption string
session *session.Session
}
func s3oConfigFromParsed(pConf *service.ParsedConfig) (conf s3oConfig, err error) {
if conf.Bucket, err = pConf.FieldString(s3oFieldBucket); err != nil {
return
}
var forcePathStyleURLs bool
if forcePathStyleURLs, err = pConf.FieldBool(s3oFieldForcePathStyleURLs); err != nil {
return
}
if conf.Path, err = pConf.FieldInterpolatedString(s3oFieldPath); err != nil {
return
}
var tagMap map[string]*service.InterpolatedString
if tagMap, err = pConf.FieldInterpolatedStringMap(s3oFieldTags); err != nil {
return
}
conf.Tags = make([]s3TagPair, 0, len(tagMap))
for k, v := range tagMap {
conf.Tags = append(conf.Tags, s3TagPair{key: k, value: v})
}
sort.Slice(conf.Tags, func(i, j int) bool {
return conf.Tags[i].key < conf.Tags[j].key
})
if conf.ContentType, err = pConf.FieldInterpolatedString(s3oFieldContentType); err != nil {
return
}
if conf.ContentEncoding, err = pConf.FieldInterpolatedString(s3oFieldContentEncoding); err != nil {
return
}
if conf.CacheControl, err = pConf.FieldInterpolatedString(s3oFieldCacheControl); err != nil {
return
}
if conf.ContentDisposition, err = pConf.FieldInterpolatedString(s3oFieldContentDisposition); err != nil {
return
}
if conf.ContentLanguage, err = pConf.FieldInterpolatedString(s3oFieldContentLanguage); err != nil {
return
}
if conf.WebsiteRedirectLocation, err = pConf.FieldInterpolatedString(s3oFieldWebsiteRedirectLocation); err != nil {
return
}
if conf.Metadata, err = pConf.FieldMetadataExcludeFilter(s3oFieldMetadata); err != nil {
return
}
if conf.StorageClass, err = pConf.FieldInterpolatedString(s3oFieldStorageClass); err != nil {
return
}
if conf.Timeout, err = pConf.FieldDuration(s3oFieldTimeout); err != nil {
return
}
if conf.KMSKeyID, err = pConf.FieldString(s3oFieldKMSKeyID); err != nil {
return
}
if conf.ServerSideEncryption, err = pConf.FieldString(s3oFieldServerSideEncryption); err != nil {
return
}
if conf.session, err = GetSession(pConf, func(c *aws.Config) {
c.S3ForcePathStyle = aws.Bool(forcePathStyleURLs)
}); err != nil {
return
}
return
}
func s3oOutputSpec() *service.ConfigSpec {
return service.NewConfigSpec().
Stable().
Version("3.36.0").
Categories("Services", "AWS").
Summary(`Sends message parts as objects to an Amazon S3 bucket. Each object is uploaded with the path specified with the `+"`path`"+` field.`).
Description(output.Description(true, false, `
In order to have a different path for each object you should use function interpolations described [here](/docs/configuration/interpolation#bloblang-queries), which are calculated per message of a batch.
### Metadata
Metadata fields on messages will be sent as headers, in order to mutate these values (or remove them) check out the [metadata docs](/docs/configuration/metadata).
### Tags
The tags field allows you to specify key/value pairs to attach to objects as tags, where the values support [interpolation functions](/docs/configuration/interpolation#bloblang-queries):
`+"```yaml"+`
output:
aws_s3:
bucket: TODO
path: ${!count("files")}-${!timestamp_unix_nano()}.tar.gz
tags:
Key1: Value1
Timestamp: ${!meta("Timestamp")}
`+"```"+`
### Credentials
By default Benthos will use a shared credentials file when connecting to AWS services. It's also possible to set them explicitly at the component level, allowing you to transfer data across accounts. You can find out more [in this document](/docs/guides/cloud/aws).
### Batching
It's common to want to upload messages to S3 as batched archives, the easiest way to do this is to batch your messages at the output level and join the batch of messages with an `+"[`archive`](/docs/components/processors/archive)"+` and/or `+"[`compress`](/docs/components/processors/compress)"+` processor.
For example, if we wished to upload messages as a .tar.gz archive of documents we could achieve that with the following config:
`+"```yaml"+`
output:
aws_s3:
bucket: TODO
path: ${!count("files")}-${!timestamp_unix_nano()}.tar.gz
batching:
count: 100
period: 10s
processors:
- archive:
format: tar
- compress:
algorithm: gzip
`+"```"+`
Alternatively, if we wished to upload JSON documents as a single large document containing an array of objects we can do that with:
`+"```yaml"+`
output:
aws_s3:
bucket: TODO
path: ${!count("files")}-${!timestamp_unix_nano()}.json
batching:
count: 100
processors:
- archive:
format: json_array
`+"```"+``)).
Fields(
service.NewStringField(s3oFieldBucket).
Description("The bucket to upload messages to."),
service.NewInterpolatedStringField(s3oFieldPath).
Description("The path of each message to upload.").
Default(`${!count("files")}-${!timestamp_unix_nano()}.txt`).
Example(`${!count("files")}-${!timestamp_unix_nano()}.txt`).
Example(`${!meta("kafka_key")}.json`).
Example(`${!json("doc.namespace")}/${!json("doc.id")}.json`),
service.NewInterpolatedStringMapField(s3oFieldTags).
Description("Key/value pairs to store with the object as tags.").
Default(map[string]string{}).
Example(map[string]string{
"Key1": "Value1",
"Timestamp": `${!meta("Timestamp")}`,
}),
service.NewInterpolatedStringField(s3oFieldContentType).
Description("The content type to set for each object.").
Default("application/octet-stream"),
service.NewInterpolatedStringField(s3oFieldContentEncoding).
Description("An optional content encoding to set for each object.").
Default("").
Advanced(),
service.NewInterpolatedStringField(s3oFieldCacheControl).
Description("The cache control to set for each object.").
Default("").
Advanced(),
service.NewInterpolatedStringField(s3oFieldContentDisposition).
Description("The content disposition to set for each object.").
Default("").
Advanced(),
service.NewInterpolatedStringField(s3oFieldContentLanguage).
Description("The content language to set for each object.").
Default("").
Advanced(),
service.NewInterpolatedStringField(s3oFieldWebsiteRedirectLocation).
Description("The website redirect location to set for each object.").
Default("").
Advanced(),
service.NewMetadataExcludeFilterField(s3oFieldMetadata).
Description("Specify criteria for which metadata values are attached to objects as headers."),
service.NewInterpolatedStringEnumField(s3oFieldStorageClass,
"STANDARD", "REDUCED_REDUNDANCY", "GLACIER", "STANDARD_IA", "ONEZONE_IA", "INTELLIGENT_TIERING", "DEEP_ARCHIVE",
).
Description("The storage class to set for each object.").
Default("STANDARD").
Advanced(),
service.NewStringField(s3oFieldKMSKeyID).
Description("An optional server side encryption key.").
Default("").
Advanced(),
service.NewStringField(s3oFieldServerSideEncryption).
Description("An optional server side encryption algorithm.").
Version("3.63.0").
Default("").
Advanced(),
service.NewBoolField(s3oFieldForcePathStyleURLs).
Description("Forces the client API to use path style URLs, which helps when connecting to custom endpoints.").
Advanced().
Default(false),
service.NewOutputMaxInFlightField(),
service.NewDurationField(s3oFieldTimeout).
Description("The maximum period to wait on an upload before abandoning it and reattempting.").
Advanced().
Default("5s"),
service.NewBatchPolicyField(s3oFieldBatching),
).
Fields(config.SessionFields()...)
}
func init() {
err := service.RegisterBatchOutput("aws_s3", s3oOutputSpec(),
func(conf *service.ParsedConfig, mgr *service.Resources) (out service.BatchOutput, batchPolicy service.BatchPolicy, maxInFlight int, err error) {
if maxInFlight, err = conf.FieldMaxInFlight(); err != nil {
return
}
if batchPolicy, err = conf.FieldBatchPolicy(koFieldBatching); err != nil {
return
}
var wConf s3oConfig
if wConf, err = s3oConfigFromParsed(conf); err != nil {
return
}
out, err = newAmazonS3Writer(wConf, mgr)
return
})
if err != nil {
panic(err)
}
}
type amazonS3Writer struct {
conf s3oConfig
uploader *s3manager.Uploader
log *service.Logger
}
func newAmazonS3Writer(conf s3oConfig, mgr *service.Resources) (*amazonS3Writer, error) {
a := &amazonS3Writer{
conf: conf,
log: mgr.Logger(),
}
return a, nil
}
func (a *amazonS3Writer) Connect(ctx context.Context) error {
if a.uploader != nil {
return nil
}
a.uploader = s3manager.NewUploader(a.conf.session)
a.log.Infof("Uploading message parts as objects to Amazon S3 bucket: %v\n", a.conf.Bucket)
return nil
}
func (a *amazonS3Writer) WriteBatch(wctx context.Context, msg service.MessageBatch) error {
if a.uploader == nil {
return component.ErrNotConnected
}
ctx, cancel := context.WithTimeout(wctx, a.conf.Timeout)
defer cancel()
return msg.WalkWithBatchedErrors(func(i int, m *service.Message) error {
metadata := map[string]*string{}
_ = a.conf.Metadata.WalkMut(m, func(k string, v any) error {
metadata[k] = aws.String(query.IToString(v))
return nil
})
var contentEncoding *string
ce, err := msg.TryInterpolatedString(i, a.conf.ContentEncoding)
if err != nil {
return fmt.Errorf("content encoding interpolation: %w", err)
}
if len(ce) > 0 {
contentEncoding = aws.String(ce)
}
var cacheControl *string
if ce, err = msg.TryInterpolatedString(i, a.conf.CacheControl); err != nil {
return fmt.Errorf("cache control interpolation: %w", err)
}
if len(ce) > 0 {
cacheControl = aws.String(ce)
}
var contentDisposition *string
if ce, err = msg.TryInterpolatedString(i, a.conf.ContentDisposition); err != nil {
return fmt.Errorf("content disposition interpolation: %w", err)
}
if len(ce) > 0 {
contentDisposition = aws.String(ce)
}
var contentLanguage *string
if ce, err = msg.TryInterpolatedString(i, a.conf.ContentLanguage); err != nil {
return fmt.Errorf("content language interpolation: %w", err)
}
if len(ce) > 0 {
contentLanguage = aws.String(ce)
}
var websiteRedirectLocation *string
if ce, err = msg.TryInterpolatedString(i, a.conf.WebsiteRedirectLocation); err != nil {
return fmt.Errorf("website redirect location interpolation: %w", err)
}
if len(ce) > 0 {
websiteRedirectLocation = aws.String(ce)
}
key, err := msg.TryInterpolatedString(i, a.conf.Path)
if err != nil {
return fmt.Errorf("key interpolation: %w", err)
}
contentType, err := msg.TryInterpolatedString(i, a.conf.ContentType)
if err != nil {
return fmt.Errorf("content type interpolation: %w", err)
}
storageClass, err := msg.TryInterpolatedString(i, a.conf.StorageClass)
if err != nil {
return fmt.Errorf("storage class interpolation: %w", err)
}
mBytes, err := m.AsBytes()
if err != nil {
return err
}
uploadInput := &s3manager.UploadInput{
Bucket: &a.conf.Bucket,
Key: aws.String(key),
Body: bytes.NewReader(mBytes),
ContentType: aws.String(contentType),
ContentEncoding: contentEncoding,
CacheControl: cacheControl,
ContentDisposition: contentDisposition,
ContentLanguage: contentLanguage,
WebsiteRedirectLocation: websiteRedirectLocation,
StorageClass: aws.String(storageClass),
Metadata: metadata,
}
// Prepare tags, escaping keys and values to ensure they're valid query string parameters.
if len(a.conf.Tags) > 0 {
tags := make([]string, len(a.conf.Tags))
for j, pair := range a.conf.Tags {
tagStr, err := msg.TryInterpolatedString(i, pair.value)
if err != nil {
return fmt.Errorf("tag %v interpolation: %w", pair.key, err)
}
tags[j] = url.QueryEscape(pair.key) + "=" + url.QueryEscape(tagStr)
}
uploadInput.Tagging = aws.String(strings.Join(tags, "&"))
}
if a.conf.KMSKeyID != "" {
uploadInput.ServerSideEncryption = aws.String("aws:kms")
uploadInput.SSEKMSKeyId = &a.conf.KMSKeyID
}
// NOTE: This overrides the ServerSideEncryption set above. We need this to preserve
// backwards compatibility, where it is allowed to only set kms_key_id in the config and
// the ServerSideEncryption value of "aws:kms" is implied.
if a.conf.ServerSideEncryption != "" {
uploadInput.ServerSideEncryption = &a.conf.ServerSideEncryption
}
if _, err := a.uploader.UploadWithContext(ctx, uploadInput); err != nil {
return err
}
return nil
})
}
func (a *amazonS3Writer) Close(context.Context) error {
return nil
}