From b4175994e6647bbce9460cd0525863febd112d9b Mon Sep 17 00:00:00 2001 From: interimadd Date: Tue, 7 Dec 2021 22:00:42 +0900 Subject: [PATCH] S3 - Error when restoring non arhive object (#4670) --- moto/s3/responses.py | 2 ++ tests/test_s3/test_s3.py | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index f0d410ad8..eb21e6710 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -2085,6 +2085,8 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): es = minidom.parseString(body).getElementsByTagName("Days") days = es[0].childNodes[0].wholeText key = self.backend.get_object(bucket_name, key_name) + if key.storage_class not in ["GLACIER", "DEEP_ARCHIVE"]: + raise InvalidObjectState(storage_class=key.storage_class) r = 202 if key.expiry_date is not None: r = 200 diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 9ae367f48..733e0cffa 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -1749,6 +1749,7 @@ def test_restore_key(): bucket = conn.create_bucket("foobar") key = Key(bucket) key.key = "the-key" + key.storage_class = "GLACIER" key.set_contents_from_string("some value") list(bucket)[0].ongoing_restore.should.be.none key.restore(1) @@ -1770,7 +1771,7 @@ def test_restore_key_boto3(): bucket = s3.Bucket("foobar") bucket.create() - key = bucket.put_object(Key="the-key", Body=b"somedata") + key = bucket.put_object(Key="the-key", Body=b"somedata", StorageClass="GLACIER") key.restore.should.be.none key.restore_object(RestoreRequest={"Days": 1}) if settings.TEST_SERVER_MODE: @@ -1790,6 +1791,24 @@ def test_restore_key_boto3(): ) +@mock_s3 +def test_cannot_restore_standard_class_object_boto3(): + s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) + bucket = s3.Bucket("foobar") + bucket.create() + + key = bucket.put_object(Key="the-key", Body=b"somedata") + with pytest.raises(Exception) as err: + key.restore_object(RestoreRequest={"Days": 1}) + + err = err.value.response["Error"] + err["Code"].should.equal("InvalidObjectState") + err["StorageClass"].should.equal("STANDARD") + err["Message"].should.equal( + "The operation is not valid for the object's storage class" + ) + + @freeze_time("2012-01-01 12:00:00") @mock_s3_deprecated def test_restore_key_headers(): @@ -1797,6 +1816,7 @@ def test_restore_key_headers(): bucket = conn.create_bucket("foobar") key = Key(bucket) key.key = "the-key" + key.storage_class = "GLACIER" key.set_contents_from_string("some value") key.restore(1, headers={"foo": "bar"}) key = bucket.get_key("the-key")