-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
144 lines (129 loc) · 3.83 KB
/
main.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
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
cziAWS "github.com/chanzuckerberg/go-misc/aws"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func processRecord(
ctx context.Context,
awsClient *cziAWS.Client,
sourceBucket string,
key string,
destinationBucket string,
destinationKMSKeyID string,
destinationPrefix string) error {
getObject := &s3.GetObjectInput{
Bucket: aws.String(sourceBucket),
Key: aws.String(key),
}
downloadData := aws.NewWriteAtBuffer([]byte{})
_, err := awsClient.S3.Downloader.DownloadWithContext(ctx, downloadData, getObject)
if err != nil {
return errors.Wrapf(err, "Could not get %s/%s", sourceBucket, key)
}
gzipReader, err := gzip.NewReader(bytes.NewBuffer(downloadData.Bytes()))
if err != nil {
return errors.Wrap(err, "Could not create gzip reader")
}
defer gzipReader.Close()
data, err := ioutil.ReadAll(gzipReader)
if err != nil {
return errors.Wrap(err, "Could not read all data")
}
parsed := map[string]interface{}{}
err = json.Unmarshal(data, &parsed)
if err != nil {
return errors.Wrap(err, "Error unmarshalling data")
}
outputData := bytes.NewBuffer(nil)
outputGzipWriter := gzip.NewWriter(outputData)
defer outputGzipWriter.Close()
// TODO(el): This will ignore the digest files, do we care about that?
records, ok := parsed["Records"]
if !ok {
logrus.Infof("Malformed event, skipping. 'Records' key not present")
return nil
}
recordList, ok := records.([]interface{})
if !ok {
logrus.Infof("Malformed event, skipping. Records not a list")
return nil
}
line := []byte{} //nolint
for _, record := range recordList {
line, err = json.Marshal(record)
if err != nil {
return errors.Wrap(err, "Error marshalling sub-record")
}
_, err = outputGzipWriter.Write(line)
if err != nil {
return errors.Wrap(err, "Error writing line")
}
_, err = outputGzipWriter.Write([]byte{byte('\n')})
if err != nil {
return errors.Wrap(err, "Error writing newline")
}
}
// Explicitly close here since we're done with the data
// Ok to call close twice (with defer)
err = outputGzipWriter.Close()
if err != nil {
return errors.Wrap(err, "Could not finalize gzip archive")
}
destinationKey := path.Join(destinationPrefix, key)
outputBytes := outputData.Bytes()
putObjectInput := &s3.PutObjectInput{
Bucket: aws.String(destinationBucket),
Key: aws.String(destinationKey),
ACL: aws.String("private"),
ContentLength: aws.Int64(int64(len(outputBytes))),
ContentType: aws.String(http.DetectContentType(outputBytes)),
Body: bytes.NewReader(outputBytes),
ServerSideEncryption: aws.String("aws:kms"),
SSEKMSKeyId: aws.String(destinationKMSKeyID),
}
_, err = awsClient.S3.Svc.PutObjectWithContext(ctx, putObjectInput)
return errors.Wrapf(err, "Error uploading to %s", destinationKey)
}
func handler(ctx context.Context, s3Event events.S3Event) (err error) {
destinationBucket := os.Getenv("DESTINATION_BUCKET")
destinationKMSKeyID := os.Getenv("DESTINATION_KMS_KEY_ID")
destinationPrefix := os.Getenv("DESTINATION_PREFIX")
sess, err := session.NewSession(&aws.Config{})
if err != nil {
return errors.Wrap(err, "Could not create aws session")
}
client := cziAWS.New(sess).WithS3(nil)
for _, event := range s3Event.Records {
err = processRecord(
ctx,
client,
event.S3.Bucket.Name,
event.S3.Object.Key,
destinationBucket,
destinationKMSKeyID,
destinationPrefix,
)
if err != nil {
return err
}
}
return nil
}
func main() {
logrus.Info("Processing started")
lambda.Start(handler)
}