NoSuchKey error in S3 is actually '404'

Fixes #571 and #953
This commit is contained in:
Kate Heddleston 2017-05-17 13:03:33 -07:00 committed by Jack Danger Canty
parent b54ef0961f
commit a21413f4ea
3 changed files with 8 additions and 5 deletions

View File

@ -11,7 +11,7 @@ import six
from bisect import insort
from moto.core import BaseBackend, BaseModel
from moto.core.utils import iso_8601_datetime_with_milliseconds, rfc_1123_datetime
from .exceptions import BucketAlreadyExists, MissingBucket, MissingKey, InvalidPart, EntityTooSmall
from .exceptions import BucketAlreadyExists, MissingBucket, InvalidPart, EntityTooSmall
from .utils import clean_key_name, _VersionedKeyStore
UPLOAD_ID_BYTES = 43
@ -473,7 +473,7 @@ class S3Backend(BaseBackend):
if isinstance(key, FakeKey):
return key
else:
raise MissingKey(key_name=key_name)
return None
def initiate_multipart(self, bucket_name, key_name, metadata):
bucket = self.get_bucket(bucket_name)

View File

@ -12,7 +12,7 @@ from moto.core.responses import _TemplateEnvironmentMixin
from moto.s3bucket_path.utils import bucket_name_from_url as bucketpath_bucket_name_from_url, parse_key_name as bucketpath_parse_key_name, is_delete_keys as bucketpath_is_delete_keys
from .exceptions import BucketAlreadyExists, S3ClientError, InvalidPartOrder
from .exceptions import BucketAlreadyExists, S3ClientError, MissingKey, InvalidPartOrder
from .models import s3_backend, get_canned_acl, FakeGrantee, FakeGrant, FakeAcl, FakeKey
from .utils import bucket_name_from_url, metadata_from_headers
from xml.dom import minidom
@ -508,6 +508,8 @@ class ResponseObject(_TemplateEnvironmentMixin):
version_id = query.get('versionId', [None])[0]
key = self.backend.get_key(
bucket_name, key_name, version_id=version_id)
if key is None:
raise MissingKey(key_name)
if 'acl' in query:
template = self.response_template(S3_OBJECT_ACL_RESPONSE)
return 200, response_headers, template.render(obj=key)

View File

@ -1223,9 +1223,10 @@ def test_boto3_head_object():
s3.Object('blah', 'hello.txt').meta.client.head_object(
Bucket='blah', Key='hello.txt')
with assert_raises(ClientError):
with assert_raises(ClientError) as e:
s3.Object('blah', 'hello2.txt').meta.client.head_object(
Bucket='blah', Key='hello_bad.txt')
e.exception.response['Error']['Code'].should.equal('404')
@mock_s3
@ -1353,7 +1354,7 @@ def test_boto3_delete_markers():
Bucket=bucket_name,
Key=key
)
e.response['Error']['Code'].should.equal('NoSuchKey')
e.response['Error']['Code'].should.equal('404')
s3.delete_object(
Bucket=bucket_name,