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 object storage class change #8046

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
18 changes: 17 additions & 1 deletion moto/s3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,25 @@ def set_metadata(self, metadata: Any, replace: bool = False) -> None:
self._metadata = {} # type: ignore
self._metadata.update(metadata)

def set_storage_class(self, storage: Optional[str]) -> None:

@property
def storage_class_status(self) -> Optional[str]:
return self._storage_class


@storage_class_status.setter
def storage_class_status(self, storage: Optional[str]) -> None:
if storage is not None and storage not in STORAGE_CLASS:
raise InvalidStorageClass(storage=storage)
if self._storage_class != storage:
s3_backend = s3_backends[self.account_id][self.partition]
bucket = s3_backend.get_bucket(self.bucket_name) # type: ignore
notifications.send_event(
self.account_id,
notifications.S3NotificationEvent.LIFECYCLE_TRANSITION_EVENT,
bucket,
key=self,
)
self._storage_class = storage

def set_expiry(self, expiry: Optional[datetime.datetime]) -> None:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_s3/test_s3_eventbridge_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,32 @@ def test_delete_object_tagging_notification():
assert event_message["region"] == REGION_NAME
assert event_message["detail"]["bucket"]["name"] == bucket_name
assert event_message["detail"]["reason"] == "ObjectTagging"


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

s3_client.put_object(Bucket=bucket_name, Key="keyname", Body="bodyofnewobject")

# Change the storage class
new_class = "GLACIER"
copy_source = {
"Bucket": bucket_name,
"Key": "keyname"
}

s3_client.copy_object(copy_source, bucket_name, "keyname_copy", ExtraArgs={'StorageClass': new_class})

events = _get_send_events()
assert len(events) == 3
event_message = json.loads(events[2]["message"])
print(event_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
assert event_message["detail"]["reason"] == "ObjectCreated"
Loading