From ef7653335b2be1765b306f663b13b448dd44053f Mon Sep 17 00:00:00 2001 From: Miki Watanabe <105326591+MikiPWata@users.noreply.github.com> Date: Thu, 7 Mar 2024 04:12:43 -0500 Subject: [PATCH] S3: Add EventBridge Notification for DeleteObject (#7420) --- moto/s3/models.py | 14 +++++++++++ .../test_s3_eventbridge_integration.py | 23 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/moto/s3/models.py b/moto/s3/models.py index 5c71675fb..cf5643d03 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -2599,10 +2599,17 @@ class S3Backend(BaseBackend, CloudWatchMetricProvider): bucket = self.get_bucket(bucket_name) response_meta = {} + delete_key = bucket.keys.get(key_name) try: if not bucket.is_versioned: bucket.keys.pop(key_name) + notifications.send_event( + self.account_id, + notifications.S3NotificationEvent.OBJECT_REMOVED_DELETE_EVENT, + bucket, + delete_key, + ) else: if version_id is None: delete_marker = self._set_delete_marker(bucket_name, key_name) @@ -2645,6 +2652,13 @@ class S3Backend(BaseBackend, CloudWatchMetricProvider): if not bucket.keys.getlist(key_name): bucket.keys.pop(key_name) + notifications.send_event( + self.account_id, + notifications.S3NotificationEvent.OBJECT_REMOVED_DELETE_EVENT, + bucket, + delete_key, + ) + return True, response_meta except KeyError: return False, None diff --git a/tests/test_s3/test_s3_eventbridge_integration.py b/tests/test_s3/test_s3_eventbridge_integration.py index d84235020..dda1f2c04 100644 --- a/tests/test_s3/test_s3_eventbridge_integration.py +++ b/tests/test_s3/test_s3_eventbridge_integration.py @@ -142,3 +142,26 @@ def test_copy_object_notification(): assert event_message["detail"]["bucket"]["name"] == bucket_name assert event_message["detail"]["object"]["key"] == object_key assert event_message["detail"]["reason"] == "ObjectCreated" + + +@mock_aws +def test_delete_object_notification(): + resource_names = _seteup_bucket_notification_eventbridge() + bucket_name = resource_names["bucket_name"] + s3_client = boto3.client("s3", region_name=REGION_NAME) + + # Put Object + s3_client.put_object(Bucket=bucket_name, Key="keyname", Body="bodyofnewobject") + + # Delete Object + s3_client.delete_object(Bucket=bucket_name, Key="keyname") + + events = _get_send_events() + assert len(events) == 2 + event_message = json.loads(events[1]["message"]) + assert event_message["detail-type"] == "Object Deleted" + assert event_message["source"] == "aws.s3" + assert event_message["account"] == ACCOUNT_ID + assert event_message["region"] == REGION_NAME + assert event_message["detail"]["bucket"]["name"] == bucket_name + assert event_message["detail"]["reason"] == "ObjectRemoved"