2014-07-30 02:15:44 +00:00
|
|
|
package emitters
|
2014-07-30 01:45:58 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/crowdmob/goamz/aws"
|
|
|
|
|
"github.com/crowdmob/goamz/s3"
|
2014-07-30 02:15:44 +00:00
|
|
|
"github.com/harlow/go-etl/buffers"
|
2014-07-30 01:45:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type S3Emitter struct {
|
|
|
|
|
S3Bucket string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e S3Emitter) s3FileName(firstSeq string, lastSeq string) string {
|
|
|
|
|
date := time.Now().UTC().Format("2006-01-02")
|
|
|
|
|
return fmt.Sprintf("/%v/%v-%v.txt", date, firstSeq, lastSeq)
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 02:15:44 +00:00
|
|
|
func (e S3Emitter) Emit(buffer buffers.Buffer) {
|
2014-07-30 01:45:58 +00:00
|
|
|
auth, _ := aws.EnvAuth()
|
|
|
|
|
s := s3.New(auth, aws.USEast)
|
|
|
|
|
b := s.Bucket(e.S3Bucket)
|
|
|
|
|
f := e.s3FileName(buffer.FirstSequenceNumber(), buffer.LastSequenceNumber())
|
|
|
|
|
r := b.Put(f, buffer.Data(), "text/plain", s3.Private, s3.Options{})
|
|
|
|
|
fmt.Printf("Successfully emitted %v records to S3 in s3://%v/%v", buffer.NumMessagesInBuffer(), b, f)
|
|
|
|
|
fmt.Println(r)
|
|
|
|
|
}
|