Skip to content

Commit 7ef82bf

Browse files
committed
[S3] Respect chunk_size in download_object_as_stream
download_object_as_stream and download_object_range_as_stream passed the hardcoded module-level CHUNK_SIZE to response.iter_content instead of the chunk_size supplied by the caller, so a user-specified chunk size was silently ignored. Forward chunk_size (falling back to CHUNK_SIZE) as the sibling drivers already do. Closes #1798
1 parent fe948d0 commit 7ef82bf

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

CHANGES.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ Compute
3434
(#2147)
3535
[Miguel Caballer - @micafer]
3636

37+
Storage
38+
~~~~~~~
39+
40+
- [S3] Fix ``chunk_size`` argument being ignored by
41+
``download_object_as_stream`` and ``download_object_range_as_stream``. The
42+
requested chunk size is now passed through to the underlying iterator
43+
instead of the hardcoded default.
44+
(GITHUB-1798)
45+
[Sanjay Santhanam - @Sanjays2402]
46+
3747
Changes in Apache Libcloud 3.9.1
3848
--------------------------------
3949

libcloud/storage/drivers/s3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def download_object_as_stream(self, obj, chunk_size=None):
521521
callback=read_in_chunks,
522522
response=response,
523523
callback_kwargs={
524-
"iterator": response.iter_content(CHUNK_SIZE),
524+
"iterator": response.iter_content(chunk_size or CHUNK_SIZE),
525525
"chunk_size": chunk_size,
526526
},
527527
success_status_code=httplib.OK,
@@ -573,7 +573,7 @@ def download_object_range_as_stream(self, obj, start_bytes, end_bytes=None, chun
573573
callback=read_in_chunks,
574574
response=response,
575575
callback_kwargs={
576-
"iterator": response.iter_content(CHUNK_SIZE),
576+
"iterator": response.iter_content(chunk_size or CHUNK_SIZE),
577577
"chunk_size": chunk_size,
578578
},
579579
success_status_code=httplib.PARTIAL_CONTENT,

libcloud/test/storage/test_s3.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,30 @@ def mock_get_object(
861861
finally:
862862
self.driver_type._get_object = old_func
863863

864+
def test_download_object_as_stream_uses_chunk_size(self):
865+
# Regression test: the chunk_size passed by the caller must be
866+
# forwarded to iter_content instead of the hardcoded CHUNK_SIZE.
867+
container = Container(name="foo_bar_container", extra={}, driver=self.driver)
868+
obj = Object(
869+
name="foo_bar_object",
870+
size=1000,
871+
hash=None,
872+
extra={},
873+
container=container,
874+
meta_data=None,
875+
driver=self.driver_type,
876+
)
877+
878+
requested_chunk_size = CHUNK_SIZE * 2
879+
mock_response = Mock(name="mock response")
880+
mock_response.iter_content.return_value = iter([b"a"])
881+
882+
with mock.patch.object(self.driver.connection, "request", return_value=mock_response):
883+
with mock.patch.object(self.driver, "_get_object", side_effect=lambda **kw: kw):
884+
self.driver.download_object_as_stream(obj=obj, chunk_size=requested_chunk_size)
885+
886+
mock_response.iter_content.assert_called_once_with(requested_chunk_size)
887+
864888
def test_upload_object_invalid_ex_storage_class(self):
865889
# Invalid hash is detected on the amazon side and BAD_REQUEST is
866890
# returned

0 commit comments

Comments
 (0)