-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 ObjectCreated:Post
#7368
Merged
bblommers
merged 5 commits into
getmoto:master
from
tsugumi-sys:send-s3-eventbridge-notification-post-object
Feb 28, 2024
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import json | ||
from io import BytesIO | ||
from unittest import SkipTest | ||
from uuid import uuid4 | ||
|
||
import boto3 | ||
|
||
from moto import mock_aws | ||
from moto import mock_aws, settings | ||
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID | ||
from tests.test_s3.test_s3_multipart import reduced_min_part_size | ||
|
||
REGION_NAME = "us-east-1" | ||
REDUCED_PART_SIZE = 256 | ||
|
||
|
||
@mock_aws | ||
def test_pub_object_notification(): | ||
def test_put_object_notification_ObjectCreated_PUT(): | ||
if not settings.TEST_DECORATOR_MODE: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason behind skipping the tests in ServerMode? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, this part was added by mistake. |
||
raise SkipTest(("Doesn't quite work right with the Proxy or Server")) | ||
|
||
s3_res = boto3.resource("s3", region_name=REGION_NAME) | ||
s3_client = boto3.client("s3", region_name=REGION_NAME) | ||
events_client = boto3.client("events", region_name=REGION_NAME) | ||
|
@@ -57,3 +64,87 @@ def test_pub_object_notification(): | |
assert event_message["region"] == REGION_NAME | ||
assert event_message["detail"]["bucket"]["name"] == bucket_name | ||
assert event_message["detail"]["reason"] == "ObjectCreated" | ||
|
||
|
||
@mock_aws | ||
@reduced_min_part_size | ||
def test_put_object_notification_ObjectCreated_POST(): | ||
if not settings.TEST_DECORATOR_MODE: | ||
raise SkipTest(("Doesn't quite work right with the Proxy or Server")) | ||
|
||
s3_res = boto3.resource("s3", region_name=REGION_NAME) | ||
s3_client = boto3.client("s3", region_name=REGION_NAME) | ||
events_client = boto3.client("events", region_name=REGION_NAME) | ||
logs_client = boto3.client("logs", region_name=REGION_NAME) | ||
|
||
rule_name = "test-rule" | ||
events_client.put_rule( | ||
Name=rule_name, EventPattern=json.dumps({"account": [ACCOUNT_ID]}) | ||
) | ||
log_group_name = "/test-group" | ||
logs_client.create_log_group(logGroupName=log_group_name) | ||
events_client.put_targets( | ||
Rule=rule_name, | ||
Targets=[ | ||
{ | ||
"Id": "test", | ||
"Arn": f"arn:aws:logs:{REGION_NAME}:{ACCOUNT_ID}:log-group:{log_group_name}", | ||
} | ||
], | ||
) | ||
|
||
# Create S3 bucket | ||
bucket_name = str(uuid4()) | ||
s3_res.create_bucket(Bucket=bucket_name) | ||
|
||
# Put Notification | ||
s3_client.put_bucket_notification_configuration( | ||
Bucket=bucket_name, | ||
NotificationConfiguration={"EventBridgeConfiguration": {}}, | ||
) | ||
|
||
# Create multipart upload request (an object is uploaded via POST request). | ||
key_name = "the-key" | ||
part1 = b"0" * REDUCED_PART_SIZE | ||
part2 = b"1" | ||
multipart = s3_client.create_multipart_upload(Bucket=bucket_name, Key=key_name) | ||
up1 = s3_client.upload_part( | ||
Body=BytesIO(part1), | ||
PartNumber=4, | ||
Bucket=bucket_name, | ||
Key=key_name, | ||
UploadId=multipart["UploadId"], | ||
) | ||
up2 = s3_client.upload_part( | ||
Body=BytesIO(part2), | ||
PartNumber=2, | ||
Bucket=bucket_name, | ||
Key=key_name, | ||
UploadId=multipart["UploadId"], | ||
) | ||
|
||
s3_client.complete_multipart_upload( | ||
Bucket=bucket_name, | ||
Key=key_name, | ||
MultipartUpload={ | ||
"Parts": [ | ||
{"ETag": up1["ETag"], "PartNumber": 4}, | ||
{"ETag": up2["ETag"], "PartNumber": 2}, | ||
] | ||
}, | ||
UploadId=multipart["UploadId"], | ||
) | ||
|
||
events = sorted( | ||
logs_client.filter_log_events(logGroupName=log_group_name)["events"], | ||
key=lambda item: item["eventId"], | ||
) | ||
assert len(events) == 1 | ||
event_message = json.loads(events[0]["message"]) | ||
assert event_message["detail-type"] == "Object Created" | ||
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 | ||
# NOTE: We cannot know if an object is created via PUT or POST request from EventBridge message. | ||
assert event_message["detail"]["reason"] == "ObjectCreated" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would send a
POST
event immediately after theCompleteMultipartUpload
-action is finished. If I understand it correctly, AWS will only send a single event in this case. So we should avoid sending aPOST
event completely here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that's true, thanks!