Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions aws_lambda_powertools/utilities/parser/models/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,59 @@
)


class S3EventRecordLifecycleTransitionEventData(BaseModel):
destinationStorageClass: str = Field(
description="The storage class the object transitioned to. For Lifecycle Transition events.",
examples=[
"INTELLIGENT_TIERING",
"STANDARD_IA",
"GLACIER",
],
)


class S3EventRecordLifecycleEventData(BaseModel):
transitionEventData: S3EventRecordLifecycleTransitionEventData = Field(
description="Event data produced only for 's3:LifecycleTransition' events.",
examples=[
{
"transitionEventData": {
"destinationStorageClass": "INTELLIGENT_TIERING",
},
},
],
)


class S3EventRecordReplicationEventData(BaseModel):
replicationRuleId: str | None = Field(
default=None,
description="The ID of the replication rule. For Replication events.",
examples=["replication-rule-1"],
)
destinationBucket: str | None = Field(
default=None,
description="The destination bucket ARN for the replication. For Replication events.",
examples=["arn:aws:s3:::destination-bucket"],
)
s3Operation: str | None = Field(
default=None,
description="The S3 operation that was replicated. For Replication events.",
examples=["OBJECT_PUT"],
)
requestTime: str | None = Field(
default=None,
description="The time, in ISO-8601 format, when the replication was requested. For Replication events.",
examples=["2019-09-03T19:37:27.192Z"],

Check failure on line 96 in aws_lambda_powertools/utilities/parser/models/s3.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "2019-09-03T19:37:27.192Z" 3 times.

See more on https://sonarcloud.io/project/issues?id=aws-powertools_powertools-lambda-python&issues=AaHfiy26L2JGjhmu4S_F&open=AaHfiy26L2JGjhmu4S_F&pullRequest=8819
)
failureReason: str | None = Field(
default=None,
description="The reason the replication failed, present only for failed replication events. "
"For Replication events.",
examples=["AssumeRoleNotPermitted"],
)


class S3Identity(BaseModel):
principalId: str = Field(
description="Amazon identifier of the user, role, account or services who caused the event.",
Expand Down Expand Up @@ -483,6 +536,30 @@
},
],
)
lifecycleEventData: S3EventRecordLifecycleEventData | None = Field(
default=None,
description="The Lifecycle event data object.",
examples=[
{
"transitionEventData": {
"destinationStorageClass": "INTELLIGENT_TIERING",
},
},
],
)
replicationEventData: S3EventRecordReplicationEventData | None = Field(
default=None,
description="The Replication event data object.",
examples=[
{
"replicationRuleId": "replication-rule-1",
"destinationBucket": "arn:aws:s3:::destination-bucket",
"s3Operation": "OBJECT_PUT",
"requestTime": "2019-09-03T19:37:27.192Z",
"failureReason": "AssumeRoleNotPermitted",
},
],
)

@model_validator(mode="before")
def validate_s3_object(cls, values):
Expand Down
46 changes: 46 additions & 0 deletions tests/events/s3EventReplication.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"Records": [
{
"awsRegion": "us-east-1",
"eventName": "Replication:OperationFailedReplication",
"eventSource": "aws:s3",
"eventTime": "2019-09-03T19:37:27.192Z",
"eventVersion": "2.1",
"replicationEventData": {
"destinationBucket": "arn:aws:s3:::destination-bucket",
"failureReason": "AssumeRoleNotPermitted",
"replicationRuleId": "replication-rule-1",
"requestTime": "2019-09-03T19:37:27.192Z",
"s3Operation": "OBJECT_PUT"
},
"requestParameters": {
"sourceIPAddress": "s3.amazonaws.com"
},
"responseElements": {
"x-amz-id-2": "vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo=",
"x-amz-request-id": "D82B88E5F771F645"
},
"s3": {
"bucket": {
"arn": "arn:aws:s3:::lambda-artifacts-deafc19498e3f2df",
"name": "lambda-artifacts-deafc19498e3f2df",
"ownerIdentity": {
"principalId": "A3I5XTEXAMAI3E"
}
},
"configurationId": "828aa6fc-f7b5-4305-8584-487c791949c1",
"object": {
"eTag": "abcdef1232423423",
"key": "/path/to/file.parquet",
"sequencer": "0C0F6F405D6ED209E1",
"size": 12345,
"versionId": "SomeThingThere"
},
"s3SchemaVersion": "1.0"
},
"userIdentity": {
"principalId": "s3.amazonaws.com"
}
}
]
}
31 changes: 31 additions & 0 deletions tests/unit/parser/_pydantic/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ def test_s3_trigger_event_lifecycle_transition():
assert convert_time == 1567539447192
assert record.eventName == raw_record["eventName"]
assert record.glacierEventData is None
assert record.lifecycleEventData is not None
assert (
record.lifecycleEventData.transitionEventData.destinationStorageClass
== raw_record["lifecycleEventData"]["transitionEventData"]["destinationStorageClass"]
)

user_identity = record.userIdentity
assert user_identity.principalId == raw_record["userIdentity"]["principalId"]
Expand All @@ -198,3 +203,29 @@ def test_s3_trigger_event_lifecycle_transition():
assert bucket.name == raw_bucket["name"]
assert bucket.ownerIdentity.principalId == raw_bucket["ownerIdentity"]["principalId"]
assert bucket.arn == raw_bucket["arn"]


def test_s3_trigger_event_replication():
raw_event = load_event("s3EventReplication.json")
parsed_event: S3Model = S3Model(**raw_event)

records = list(parsed_event.Records)
assert len(records) == 1

record: S3RecordModel = records[0]
raw_record = raw_event["Records"][0]
assert record.eventVersion == raw_record["eventVersion"]
assert record.eventSource == raw_record["eventSource"]
assert record.awsRegion == raw_record["awsRegion"]
assert record.eventName == raw_record["eventName"]
assert record.glacierEventData is None
assert record.lifecycleEventData is None

replication = record.replicationEventData
assert replication is not None
raw_replication = raw_record["replicationEventData"]
assert replication.replicationRuleId == raw_replication["replicationRuleId"]
assert replication.destinationBucket == raw_replication["destinationBucket"]
assert replication.s3Operation == raw_replication["s3Operation"]
assert replication.requestTime == raw_replication["requestTime"]
assert replication.failureReason == raw_replication["failureReason"]