Skip to content

Commit 20c235e

Browse files
mcasadosanathkr
authored andcommitted
fix: download a swagger file from given S3 location (aws#444)
1 parent 4afb0cb commit 20c235e

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

samcli/commands/local/lib/swagger/reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def _download_from_s3(bucket, key, version=None):
213213

214214
with tempfile.TemporaryFile() as fp:
215215
try:
216-
s3.download_file(
216+
s3.download_fileobj(
217217
bucket, key, fp,
218218
ExtraArgs=extra_args)
219219

tests/unit/commands/local/lib/swagger/test_reader.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,18 @@ def test_must_download_file_from_s3(self, tempfilemock, botomock):
259259
actual = SamSwaggerReader._download_from_s3(self.bucket, self.key, self.version)
260260
self.assertEquals(actual, expected)
261261

262-
s3_mock.download_file.assert_called_with(self.bucket, self.key, fp_mock,
263-
ExtraArgs={"VersionId": self.version})
262+
s3_mock.download_fileobj.assert_called_with(self.bucket, self.key, fp_mock,
263+
ExtraArgs={"VersionId": self.version})
264264

265265
fp_mock.seek.assert_called_with(0) # make sure we seek the file before reading
266266
fp_mock.read.assert_called_with()
267267

268+
def test_must_fail_on_download_from_s3(self):
269+
with self.assertRaises(Exception) as cm:
270+
SamSwaggerReader._download_from_s3(self.bucket, self.key)
271+
self.assertIn(cm.exception.__class__,
272+
(botocore.exceptions.NoCredentialsError, botocore.exceptions.ClientError))
273+
268274
@patch('samcli.commands.local.lib.swagger.reader.boto3')
269275
@patch('samcli.commands.local.lib.swagger.reader.tempfile')
270276
def test_must_work_without_object_version_id(self, tempfilemock, botomock):
@@ -281,8 +287,8 @@ def test_must_work_without_object_version_id(self, tempfilemock, botomock):
281287
actual = SamSwaggerReader._download_from_s3(self.bucket, self.key)
282288
self.assertEquals(actual, expected)
283289

284-
s3_mock.download_file.assert_called_with(self.bucket, self.key, fp_mock,
285-
ExtraArgs={})
290+
s3_mock.download_fileobj.assert_called_with(self.bucket, self.key, fp_mock,
291+
ExtraArgs={})
286292

287293
@patch('samcli.commands.local.lib.swagger.reader.boto3')
288294
@patch('samcli.commands.local.lib.swagger.reader.tempfile')
@@ -293,8 +299,8 @@ def test_must_log_on_download_exception(self, tempfilemock, botomock):
293299

294300
fp_mock = Mock()
295301
tempfilemock.TemporaryFile.return_value.__enter__.return_value = fp_mock # mocking context manager
296-
s3_mock.download_file.side_effect = botocore.exceptions.ClientError({"Error": {}},
297-
"download_file")
302+
s3_mock.download_fileobj.side_effect = botocore.exceptions.ClientError({"Error": {}},
303+
"download_file")
298304

299305
with self.assertRaises(botocore.exceptions.ClientError):
300306
SamSwaggerReader._download_from_s3(self.bucket, self.key)

0 commit comments

Comments
 (0)