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 3
3
package events
4
4
5
5
import (
6
+ "encoding/json"
7
+ "net/url"
6
8
"time"
7
9
)
8
10
@@ -54,6 +56,20 @@ type S3Object struct {
54
56
Sequencer string `json:"sequencer"`
55
57
}
56
58
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
+
57
73
type S3TestEvent struct {
58
74
Service string `json:"Service"`
59
75
Bucket string `json:"Bucket"`
Original file line number Diff line number Diff line change @@ -26,8 +26,11 @@ func TestS3EventMarshaling(t *testing.T) {
26
26
t .Errorf ("could not marshal event. details: %v" , err )
27
27
}
28
28
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 ))
31
34
}
32
35
33
36
func 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 12
12
"requestParameters" : {
13
13
"sourceIPAddress" : " 127.0.0.1"
14
14
},
15
- "responseElements" : {
15
+ "responseElements" : {
16
16
"x-amz-request-id" : " C3D13FE58DE4C810" ,
17
17
"x-amz-id-2" : " FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
18
18
},
27
27
"arn" : " arn:aws:s3:::mybucket"
28
28
},
29
29
"object" : {
30
- "key" : " HappyFace .jpg" ,
30
+ "key" : " Happy%20Face .jpg" ,
31
31
"size" : 1024 ,
32
- "urlDecodedKey" : " HappyFace.jpg" ,
33
32
"versionId" : " version" ,
34
33
"eTag" : " d41d8cd98f00b204e9800998ecf8427e" ,
35
34
"sequencer" : " Happy Sequencer"
You can’t perform that action at this time.
0 commit comments