Skip to content

Commit aa4e58e

Browse files
jbonzobmoffatt
authored andcommitted
Add S3BatchJobEvent (#210)
* Add S3BatchJobEvent and related structs, tests, test json, and README * Update README_S3_Batch_Job.md Arn -> ARN * Update s3_batch_job.go S3BucketArn -> S3BucketARN
1 parent 5149d77 commit aa4e58e

5 files changed

+153
-0
lines changed

events/README_S3_Batch_Job.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Sample Function
2+
3+
The following is a sample class and Lambda function that receives Amazon S3 event record data as an input and writes some of the record data to CloudWatch Logs. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)
4+
5+
```go
6+
7+
import (
8+
"fmt"
9+
"context"
10+
"github.com/aws/aws-lambda-go/events"
11+
)
12+
13+
func handler(ctx context.Context, e events.S3BatchJobEvent) (response events.S3BatchJobResponse, err error) {
14+
fmt.Printf("InvocationSchemaVersion: %s\n", e.InvocationSchemaVersion)
15+
fmt.Printf("InvocationID: %s\n", e.InvocationID)
16+
fmt.Printf("Job.ID: %s\n", e.Job.ID)
17+
18+
for _, task := range e.Tasks {
19+
fmt.Printf("TaskID: %s\n", task.TaskID)
20+
fmt.Printf("S3Key: %s\n", task.S3Key)
21+
fmt.Printf("S3VersionID: %s\n", task.S3VersionID)
22+
fmt.Printf("S3BucketARN: %s\n", task.S3BucketARN)
23+
24+
}
25+
26+
fmt.Printf("InvocationSchemaVersion: %s\n", response.InvocationSchemaVersion)
27+
fmt.Printf("TreatMissingKeysAs: %s\n", response.TreatMissingKeysAs)
28+
fmt.Printf("InvocationID: %s\n", response.InvocationID)
29+
30+
for _, result := range response.Results {
31+
fmt.Printf("TaskID: %s\n", result.TaskID)
32+
fmt.Printf("ResultCode: %s\n", result.ResultCode)
33+
fmt.Printf("ResultString: %s\n", result.ResultString)
34+
}
35+
36+
return
37+
}
38+
39+
```

events/s3_batch_job.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
package events
4+
5+
type S3BatchJobEvent struct {
6+
InvocationSchemaVersion string `json:"invocationSchemaVersion"`
7+
InvocationID string `json:"invocationId"`
8+
Job S3BatchJob `json:"job"`
9+
Tasks []S3BatchJobTask `json:"tasks"`
10+
}
11+
12+
type S3BatchJob struct {
13+
ID string `json:"id"`
14+
}
15+
16+
type S3BatchJobTask struct {
17+
TaskID string `json:"taskId"`
18+
S3Key string `json:"s3Key"`
19+
S3VersionID string `json:"s3VersionId"`
20+
S3BucketARN string `json:"s3BucketArn"`
21+
}
22+
23+
type S3BatchJobResponse struct {
24+
InvocationSchemaVersion string `json:"invocationSchemaVersion"`
25+
TreatMissingKeysAs string `json:"treatMissingKeysAs"`
26+
InvocationID string `json:"invocationId"`
27+
Results []S3BatchJobResult `json:"results"`
28+
}
29+
30+
type S3BatchJobResult struct {
31+
TaskID string `json:"taskId"`
32+
ResultCode string `json:"resultCode"`
33+
ResultString string `json:"resultString"`
34+
}

events/s3_batch_job_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
package events
4+
5+
import (
6+
"encoding/json"
7+
"testing"
8+
9+
"github.com/aws/aws-lambda-go/events/test"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestS3BatchJobEventMarshaling(t *testing.T) {
14+
15+
// 1. read JSON from file
16+
inputJSON := test.ReadJSONFromFile(t, "./testdata/s3-batch-job-event-request.json")
17+
18+
// 2. de-serialize into Go object
19+
var inputEvent S3BatchJobEvent
20+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
21+
t.Errorf("could not unmarshal event. details: %v", err)
22+
}
23+
24+
// 3. serialize to JSON
25+
outputJSON, err := json.Marshal(inputEvent)
26+
if err != nil {
27+
t.Errorf("could not marshal event. details: %v", err)
28+
}
29+
30+
// 4. check result
31+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
32+
}
33+
34+
func TestS3BatchJobResponseMarshaling(t *testing.T) {
35+
36+
// 1. read JSON from file
37+
inputJSON := test.ReadJSONFromFile(t, "./testdata/s3-batch-job-event-response.json")
38+
39+
// 2. de-serialize into Go object
40+
var inputEvent S3BatchJobResponse
41+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
42+
t.Errorf("could not unmarshal event. details: %v", err)
43+
}
44+
45+
// 3. serialize to JSON
46+
outputJSON, err := json.Marshal(inputEvent)
47+
if err != nil {
48+
t.Errorf("could not marshal event. details: %v", err)
49+
}
50+
51+
// 4. check result
52+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
53+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"invocationSchemaVersion": "1.0",
3+
"invocationId": "YXNkbGZqYWRmaiBhc2RmdW9hZHNmZGpmaGFzbGtkaGZza2RmaAo",
4+
"job": {
5+
"id": "f3cc4f60-61f6-4a2b-8a21-d07600c373ce"
6+
},
7+
"tasks": [
8+
{
9+
"taskId": "dGFza2lkZ29lc2hlcmUK",
10+
"s3Key": "customerImage1.jpg",
11+
"s3VersionId": "1",
12+
"s3BucketArn": "arn:aws:s3:us-east-1:0123456788:awsexamplebucket"
13+
}
14+
]
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"invocationSchemaVersion": "1.0",
3+
"treatMissingKeysAs" : "PermanentFailure",
4+
"invocationId" : "YXNkbGZqYWRmaiBhc2RmdW9hZHNmZGpmaGFzbGtkaGZza2RmaAo",
5+
"results": [
6+
{
7+
"taskId": "dGFza2lkZ29lc2hlcmUK",
8+
"resultCode": "Succeeded",
9+
"resultString": "[\"Mary Major\", \"John Stiles\"]"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)