From b06e77b604f54ba425ff22b3ddc47ce9472331d1 Mon Sep 17 00:00:00 2001 From: Tibor Djurica Potpara Date: Tue, 16 Mar 2021 22:24:41 +0000 Subject: [PATCH] Make put-object-acl return 404 if the key does not exist (#3777) Co-authored-by: Tibor Djurica Potpara --- moto/s3/responses.py | 7 +++++-- tests/test_s3/test_s3.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index a5fa6b849..0a8c53a82 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -1305,8 +1305,11 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): if "acl" in query: key = self.backend.get_object(bucket_name, key_name) # TODO: Support the XML-based ACL format - key.set_acl(acl) - return 200, response_headers, "" + if key is not None: + key.set_acl(acl) + return 200, response_headers, "" + else: + raise MissingKey(key_name) if "tagging" in query: if "versionId" in query: diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index b83091315..b35290f46 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -979,6 +979,17 @@ def test_acl_switching(): ), grants +@mock_s3 +def test_acl_switching_nonexistent_key(): + s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME) + s3.create_bucket(Bucket="mybucket") + + with pytest.raises(ClientError) as e: + s3.put_object_acl(Bucket="mybucket", Key="nonexistent", ACL="private") + + e.value.response["Error"]["Code"].should.equal("NoSuchKey") + + @mock_s3_deprecated def test_bucket_acl_setting(): conn = boto.connect_s3()