File tree Expand file tree Collapse file tree 4 files changed +63
-5
lines changed Expand file tree Collapse file tree 4 files changed +63
-5
lines changed Original file line number Diff line number Diff line change 33package events
44
55import (
6+ "encoding/json"
7+ "net/url"
68 "time"
79)
810
@@ -54,6 +56,20 @@ type S3Object struct {
5456 Sequencer string `json:"sequencer"`
5557}
5658
59+ func (o * S3Object ) UnmarshalJSON (data []byte ) error {
60+ type rawS3Object S3Object
61+ if err := json .Unmarshal (data , (* rawS3Object )(o )); err != nil {
62+ return err
63+ }
64+ key , err := url .QueryUnescape (o .Key )
65+ if err != nil {
66+ return err
67+ }
68+ o .URLDecodedKey = key
69+
70+ return nil
71+ }
72+
5773type S3TestEvent struct {
5874 Service string `json:"Service"`
5975 Bucket string `json:"Bucket"`
Original file line number Diff line number Diff line change @@ -26,8 +26,11 @@ func TestS3EventMarshaling(t *testing.T) {
2626 t .Errorf ("could not marshal event. details: %v" , err )
2727 }
2828
29- // 4. check result
30- assert .JSONEq (t , string (inputJSON ), string (outputJSON ))
29+ // 4. read expected output JSON from file
30+ exepectedOutputJSON := test .ReadJSONFromFile (t , "./testdata/s3-event-with-decoded.json" )
31+
32+ // 5. check result
33+ assert .JSONEq (t , string (exepectedOutputJSON ), string (outputJSON ))
3134}
3235
3336func TestS3TestEventMarshaling (t * testing.T ) {
Original file line number Diff line number Diff line change 1+ {
2+ "Records" : [
3+ {
4+ "eventVersion" : " 2.0" ,
5+ "eventSource" : " aws:s3" ,
6+ "awsRegion" : " us-east-1" ,
7+ "eventTime" : " 1970-01-01T00:00:00.123Z" ,
8+ "eventName" : " ObjectCreated:Put" ,
9+ "userIdentity" : {
10+ "principalId" : " EXAMPLE"
11+ },
12+ "requestParameters" : {
13+ "sourceIPAddress" : " 127.0.0.1"
14+ },
15+ "responseElements" : {
16+ "x-amz-request-id" : " C3D13FE58DE4C810" ,
17+ "x-amz-id-2" : " FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
18+ },
19+ "s3" : {
20+ "s3SchemaVersion" : " 1.0" ,
21+ "configurationId" : " testConfigRule" ,
22+ "bucket" : {
23+ "name" : " sourcebucket" ,
24+ "ownerIdentity" : {
25+ "principalId" : " EXAMPLE"
26+ },
27+ "arn" : " arn:aws:s3:::mybucket"
28+ },
29+ "object" : {
30+ "key" : " Happy%20Face.jpg" ,
31+ "urlDecodedKey" : " Happy Face.jpg" ,
32+ "size" : 1024 ,
33+ "versionId" : " version" ,
34+ "eTag" : " d41d8cd98f00b204e9800998ecf8427e" ,
35+ "sequencer" : " Happy Sequencer"
36+ }
37+ }
38+ }
39+ ]
40+ }
Original file line number Diff line number Diff line change 1212 "requestParameters" : {
1313 "sourceIPAddress" : " 127.0.0.1"
1414 },
15- "responseElements" : {
15+ "responseElements" : {
1616 "x-amz-request-id" : " C3D13FE58DE4C810" ,
1717 "x-amz-id-2" : " FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
1818 },
2727 "arn" : " arn:aws:s3:::mybucket"
2828 },
2929 "object" : {
30- "key" : " HappyFace .jpg" ,
30+ "key" : " Happy%20Face .jpg" ,
3131 "size" : 1024 ,
32- "urlDecodedKey" : " HappyFace.jpg" ,
3332 "versionId" : " version" ,
3433 "eTag" : " d41d8cd98f00b204e9800998ecf8427e" ,
3534 "sequencer" : " Happy Sequencer"
You can’t perform that action at this time.
0 commit comments