Skip to content

Commit fd838e7

Browse files
committed
Add S3BatchJobEvent
1 parent af0b813 commit fd838e7

5 files changed

+152
-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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
)
11+
12+
func TestS3BatchJobEventMarshaling(t *testing.T) {
13+
14+
// 1. read JSON from file
15+
inputJSON := test.ReadJSONFromFile(t, "./testdata/s3-batch-job-event-request.json")
16+
17+
// 2. de-serialize into Go object
18+
var inputEvent S3BatchJobEvent
19+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
20+
t.Errorf("could not unmarshal event. details: %v", err)
21+
}
22+
23+
// 3. serialize to JSON
24+
outputJSON, err := json.Marshal(inputEvent)
25+
if err != nil {
26+
t.Errorf("could not marshal event. details: %v", err)
27+
}
28+
29+
// 4. check result
30+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
31+
}
32+
33+
func TestS3BatchJobResponseMarshaling(t *testing.T) {
34+
35+
// 1. read JSON from file
36+
inputJSON := test.ReadJSONFromFile(t, "./testdata/s3-batch-job-event-response.json")
37+
38+
// 2. de-serialize into Go object
39+
var inputEvent S3BatchJobResponse
40+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
41+
t.Errorf("could not unmarshal event. details: %v", err)
42+
}
43+
44+
// 3. serialize to JSON
45+
outputJSON, err := json.Marshal(inputEvent)
46+
if err != nil {
47+
t.Errorf("could not marshal event. details: %v", err)
48+
}
49+
50+
// 4. check result
51+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
52+
}
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)