S3 - Error when restoring non arhive object (#4670)

This commit is contained in:
interimadd 2021-12-07 22:00:42 +09:00 committed by GitHub
parent 9ca50c3474
commit b4175994e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -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

View File

@ -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")