2016-02-09 03:39:09 +00:00
|
|
|
package s3
|
2014-07-25 06:03:41 +00:00
|
|
|
|
|
|
|
|
import (
|
2016-02-03 05:04:22 +00:00
|
|
|
"io"
|
2014-07-25 06:03:41 +00:00
|
|
|
|
2015-08-17 00:52:10 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2016-02-03 05:04:22 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2016-05-01 17:42:28 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2014-07-25 06:03:41 +00:00
|
|
|
)
|
|
|
|
|
|
2016-02-09 03:39:09 +00:00
|
|
|
// Emitter stores data in S3 bucket.
|
2014-12-10 23:38:19 +00:00
|
|
|
//
|
|
|
|
|
// The use of this struct requires the configuration of an S3 bucket/endpoint. When the buffer is full, this
|
2014-07-25 06:03:41 +00:00
|
|
|
// struct's Emit method adds the contents of the buffer to S3 as one file. The filename is generated
|
|
|
|
|
// from the first and last sequence numbers of the records contained in that file separated by a
|
|
|
|
|
// dash. This struct requires the configuration of an S3 bucket and endpoint.
|
2016-02-09 03:39:09 +00:00
|
|
|
type Emitter struct {
|
2016-02-03 05:04:22 +00:00
|
|
|
Bucket string
|
2016-05-01 17:42:28 +00:00
|
|
|
Region string
|
2014-07-25 06:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-10 23:38:19 +00:00
|
|
|
// Emit is invoked when the buffer is full. This method emits the set of filtered records.
|
2016-02-09 03:39:09 +00:00
|
|
|
func (e Emitter) Emit(s3Key string, b io.ReadSeeker) error {
|
2016-05-01 17:42:28 +00:00
|
|
|
svc := s3.New(
|
|
|
|
|
session.New(aws.NewConfig().WithMaxRetries(10)),
|
|
|
|
|
&aws.Config{
|
|
|
|
|
Region: aws.String(e.Region),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
params := &s3.PutObjectInput{
|
2016-02-03 05:04:22 +00:00
|
|
|
Body: b,
|
|
|
|
|
Bucket: aws.String(e.Bucket),
|
2015-08-17 00:52:10 +00:00
|
|
|
ContentType: aws.String("text/plain"),
|
2016-02-03 05:04:22 +00:00
|
|
|
Key: aws.String(s3Key),
|
2015-08-17 00:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-01 17:42:28 +00:00
|
|
|
_, err := svc.PutObject(params)
|
2014-07-25 06:03:41 +00:00
|
|
|
|
|
|
|
|
if err != nil {
|
2016-02-09 03:39:09 +00:00
|
|
|
return err
|
2015-08-16 05:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 03:39:09 +00:00
|
|
|
return nil
|
2014-07-25 06:03:41 +00:00
|
|
|
}
|