Skip to content

Commit 1191ffb

Browse files
committed
feat: add replication event data to S3EventRecord and corresponding test cases
1 parent d4fbc0b commit 1191ffb

3 files changed

Lines changed: 90 additions & 9 deletions

File tree

events/s3.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ type S3Event struct {
1515

1616
// S3EventRecord which wrap record data
1717
type S3EventRecord struct {
18-
EventVersion string `json:"eventVersion"`
19-
EventSource string `json:"eventSource"`
20-
AWSRegion string `json:"awsRegion"`
21-
EventTime time.Time `json:"eventTime"`
22-
EventName string `json:"eventName"`
23-
PrincipalID S3UserIdentity `json:"userIdentity"`
24-
RequestParameters S3RequestParameters `json:"requestParameters"`
25-
ResponseElements map[string]string `json:"responseElements"`
26-
S3 S3Entity `json:"s3"`
18+
EventVersion string `json:"eventVersion"`
19+
EventSource string `json:"eventSource"`
20+
AWSRegion string `json:"awsRegion"`
21+
EventTime time.Time `json:"eventTime"`
22+
EventName string `json:"eventName"`
23+
PrincipalID S3UserIdentity `json:"userIdentity"`
24+
RequestParameters S3RequestParameters `json:"requestParameters"`
25+
ResponseElements map[string]string `json:"responseElements"`
26+
S3 S3Entity `json:"s3"`
27+
ReplicationEventData *S3ReplicationEventData `json:"replicationEventData,omitempty"`
2728
}
2829

2930
type S3UserIdentity struct {
@@ -70,6 +71,14 @@ func (o *S3Object) UnmarshalJSON(data []byte) error {
7071
return nil
7172
}
7273

74+
type S3ReplicationEventData struct {
75+
ReplicationRuleID string `json:"replicationRuleId"`
76+
DestinationBucket string `json:"destinationBucket"`
77+
S3Operation string `json:"s3Operation"`
78+
RequestTime time.Time `json:"requestTime"`
79+
FailureReason string `json:"failureReason"`
80+
}
81+
7382
type S3TestEvent struct {
7483
Service string `json:"Service"`
7584
Bucket string `json:"Bucket"`

events/s3_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,28 @@ func TestS3TestEventMarshaling(t *testing.T) {
5656
func TestS3MarshalingMalformedJSON(t *testing.T) {
5757
test.TestMalformedJson(t, S3Event{})
5858
}
59+
60+
func TestS3ReplicationEventMarshaling(t *testing.T) {
61+
// 1. read JSON from file
62+
inputJSON := test.ReadJSONFromFile(t, "./testdata/s3-replication-event.json")
63+
64+
// 2. de-serialize into Go object
65+
var inputEvent S3Event
66+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
67+
t.Errorf("could not unmarshal event. details: %v", err)
68+
}
69+
70+
// 3. verify replicationEventData is correctly parsed
71+
if inputEvent.Records[0].ReplicationEventData == nil {
72+
t.Error("replicationEventData should not be nil for replication events")
73+
}
74+
75+
// 4. serialize to JSON
76+
outputJSON, err := json.Marshal(inputEvent)
77+
if err != nil {
78+
t.Errorf("could not marshal event. details: %v", err)
79+
}
80+
81+
// 5. check result
82+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
83+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"Records": [
3+
{
4+
"eventVersion": "2.2",
5+
"eventSource": "aws:s3",
6+
"awsRegion": "us-east-1",
7+
"eventTime": "2024-09-05T21:04:32.527Z",
8+
"eventName": "s3:Replication:OperationFailedReplication",
9+
"userIdentity": {
10+
"principalId": "s3.amazonaws.com"
11+
},
12+
"requestParameters": {
13+
"sourceIPAddress": "s3.amazonaws.com"
14+
},
15+
"responseElements": {
16+
"x-amz-request-id": "123bf045-2b4b-4ca8-a211-c34a63c59426",
17+
"x-amz-id-2": "12VAWNDIHnwJsRhTccqQTeAPoXQmRt22KkewMV8G3XZihAuf9CLDdmkApgZzudaIe2KlLfDqGS0="
18+
},
19+
"s3": {
20+
"s3SchemaVersion": "1.0",
21+
"configurationId": "ReplicationEventName",
22+
"bucket": {
23+
"name": "amzn-s3-demo-bucket1",
24+
"ownerIdentity": {
25+
"principalId": "111122223333"
26+
},
27+
"arn": "arn:aws:s3:::amzn-s3-demo-bucket1"
28+
},
29+
"object": {
30+
"key": "replication%2Dtest.png",
31+
"urlDecodedKey": "replication-test.png",
32+
"size": 520080,
33+
"versionId": "abcdeH0Xp66ep__QDjR76LK7Gc9X4wKO",
34+
"eTag": "e12345ca7e88a38428305d3ff7fcb99f",
35+
"sequencer": "0066DA1CBF104C0D51"
36+
}
37+
},
38+
"replicationEventData": {
39+
"replicationRuleId": "notification-test-replication-rule",
40+
"destinationBucket": "arn:aws:s3:::amzn-s3-demo-bucket2",
41+
"s3Operation": "OBJECT_PUT",
42+
"requestTime": "2024-09-05T21:03:59.168Z",
43+
"failureReason": "AssumeRoleNotPermitted"
44+
}
45+
}
46+
]
47+
}

0 commit comments

Comments
 (0)