Skip to content

Commit 5bc50ab

Browse files
Populate URLDecodedKey in S3Object (#335)
1 parent dda2bc7 commit 5bc50ab

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

events/s3.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package events
44

55
import (
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+
5773
type S3TestEvent struct {
5874
Service string `json:"Service"`
5975
Bucket string `json:"Bucket"`

events/s3_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff 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

3336
func TestS3TestEventMarshaling(t *testing.T) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

events/testdata/s3-event.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
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
},
@@ -27,9 +27,8 @@
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"

0 commit comments

Comments
 (0)