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: Sending notification to EventBridge LIFECYCLE_TRANSITION_EVENT #7448

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions moto/s3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2783,6 +2783,16 @@ def copy_object(
new_key,
)

if src_key.storage_class != new_key.storage_class:
notifications.send_event(
self.account_id,
notifications.S3NotificationEvent.LIFECYCLE_TRANSITION_EVENT,
bucket,
new_key,
)

# Send notifications if object storage class is changed.

def put_bucket_acl(self, bucket_name: str, acl: Optional[FakeAcl]) -> None:
bucket = self.get_bucket(bucket_name)
bucket.set_acl(acl)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_s3/test_s3_eventbridge_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,38 @@ def test_delete_object_notification():
assert event_message["region"] == REGION_NAME
assert event_message["detail"]["bucket"]["name"] == bucket_name
assert event_message["detail"]["reason"] == "ObjectRemoved"


@mock_aws
def test_storage_class_changed_notification_eventbridge_copy_object():
resource_names = _seteup_bucket_notification_eventbridge()
bucket_name = resource_names["bucket_name"]
s3_client = boto3.client("s3", region_name=REGION_NAME)

# Copy Object with different storage class
src_object_key = "srcobjectkey"
s3_client.put_object(
Bucket=bucket_name,
Key=src_object_key,
Body="bodyofnewobject",
StorageClass="STANDARD",
)
new_object_key = "newobjectkey"
s3_client.copy_object(
Bucket=bucket_name,
CopySource=f"{bucket_name}/{src_object_key}",
Key=new_object_key,
StorageClass="REDUCED_REDUNDANCY",
)

events = _get_send_events()
assert (
len(events) == 3
) # [PutObject event, CopyObject event, LifecycleTransition event]
event_message = json.loads(events[-1]["message"])
assert event_message["detail-type"] == "Object Storage Class Changed"
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"] == "LifecycleTransition"