From 98f33740e117d20656a4ab2557f30a0d0e481cc0 Mon Sep 17 00:00:00 2001 From: koshigoe Date: Wed, 16 Oct 2019 18:13:59 +0900 Subject: [PATCH] fix(s3): check whether key is None or not to avoid exception. ``` AttributeError: 'NoneType' object has no attribute 'multipart' ``` --- moto/s3/models.py | 2 +- tests/test_s3/test_s3.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/moto/s3/models.py b/moto/s3/models.py index ef49d7f95..39c4b1edd 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -1107,7 +1107,7 @@ class S3Backend(BaseBackend): key = key_version break - if part_number and key.multipart: + if part_number and key and key.multipart: key = key.multipart.parts[part_number] if isinstance(key, FakeKey): diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 292093893..0d8f3385d 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -1534,6 +1534,18 @@ def test_boto3_get_object(): e.exception.response['Error']['Code'].should.equal('NoSuchKey') +@mock_s3 +def test_boto3_get_missing_object_with_part_number(): + s3 = boto3.resource('s3', region_name='us-east-1') + s3.create_bucket(Bucket="blah") + + with assert_raises(ClientError) as e: + s3.Object('blah', 'hello.txt').meta.client.head_object( + Bucket='blah', Key='hello.txt', PartNumber=123) + + e.exception.response['Error']['Code'].should.equal('404') + + @mock_s3 def test_boto3_head_object_with_versioning(): s3 = boto3.resource('s3', region_name='us-east-1')