2014-07-25 06:03:41 +00:00
|
|
|
package connector
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2016-02-03 05:04:22 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2015-08-17 00:52:10 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
|
"gopkg.in/matryer/try.v1"
|
2014-07-25 06:03:41 +00:00
|
|
|
)
|
|
|
|
|
|
2016-02-03 05:04:22 +00:00
|
|
|
// S3Emitter 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.
|
|
|
|
|
type S3Emitter struct {
|
2016-02-03 05:04:22 +00:00
|
|
|
Bucket 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-03 05:04:22 +00:00
|
|
|
func (e S3Emitter) Emit(s3Key string, b io.ReadSeeker) {
|
|
|
|
|
svc := s3.New(session.New())
|
2015-08-17 00:52:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := try.Do(func(attempt int) (bool, error) {
|
|
|
|
|
var err error
|
|
|
|
|
_, err = svc.PutObject(params)
|
|
|
|
|
return attempt < 5, err
|
|
|
|
|
})
|
2014-07-25 06:03:41 +00:00
|
|
|
|
|
|
|
|
if err != nil {
|
2015-08-17 00:52:10 +00:00
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
2016-02-03 05:04:22 +00:00
|
|
|
logger.Log("error", "s3.PutObject", "code", awsErr.Code())
|
2015-08-17 00:52:10 +00:00
|
|
|
}
|
2015-08-16 05:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-03 05:04:22 +00:00
|
|
|
logger.Log("info", "S3Emitter", "msg", "success", "key", s3Key)
|
2014-07-25 06:03:41 +00:00
|
|
|
}
|