Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3: Add EventBridge Notification for DeleteObject #7420

Merged
merged 4 commits into from
Mar 7, 2024
Merged
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
14 changes: 14 additions & 0 deletions moto/s3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2599,10 +2599,17 @@ def delete_object(
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)
Expand Down Expand Up @@ -2645,6 +2652,13 @@ def delete_object(

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
Expand Down
23 changes: 23 additions & 0 deletions tests/test_s3/test_s3_eventbridge_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"