Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ Compute
Storage
~~~~~~~

- [Local Storage] Delete the partially written file when ``download_object_range``
fails and ``delete_on_failure`` is set.
(GITHUB-2223)
[Rahul - @raamrutsagarb25-tech]

- [S3] Fix ``get_object_cdn_url`` computing invalid signatures when using
a non-standard port
(#2222)
Expand Down
16 changes: 13 additions & 3 deletions libcloud/storage/drivers/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,19 @@ def download_object_range(
obj=obj, start_bytes=start_bytes, end_bytes=end_bytes
)

with open(file_path, "wb") as fp:
for chunk in iterator:
fp.write(chunk)
try:
with open(file_path, "wb") as fp:
for chunk in iterator:
fp.write(chunk)
except Exception:
if delete_on_failure and os.path.exists(file_path):
try:
os.remove(file_path)
except OSError:
# Don't let a failure to clean up the partially
# written file mask the original download exception.
pass
raise

return True

Expand Down
57 changes: 57 additions & 0 deletions libcloud/test/storage/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,63 @@ def test_download_object_range_success(self):
self.remove_tmp_file(tmppath)
os.unlink(destination_path)

def test_download_object_range_deletes_file_on_failure(self):
container = self.driver.create_container("range-failure")
obj = self.driver.upload_object_via_stream(iter([b"payload"]), container, "object")

destination_path = os.path.join(self.key, "download")

error = OSError("stream failed")

def failing_iterator(*args, **kwargs):
yield b"partial data"
raise error

with mock.patch.object(
self.driver, "download_object_range_as_stream", side_effect=failing_iterator
):
with self.assertRaises(OSError) as raised:
self.driver.download_object_range(
obj=obj,
destination_path=destination_path,
start_bytes=0,
delete_on_failure=True,
)

self.assertIs(raised.exception, error)
self.assertFalse(os.path.exists(destination_path))

def test_download_object_range_preserves_partial_file_when_delete_on_failure_is_false(self):
container = self.driver.create_container("range-failure-preserve")
obj = self.driver.upload_object_via_stream(iter([b"payload"]), container, "object")

destination_path = os.path.join(self.key, "download")

error = OSError("stream failed")

def failing_iterator(*args, **kwargs):
yield b"partial data"
raise error

with mock.patch.object(
self.driver, "download_object_range_as_stream", side_effect=failing_iterator
):
with self.assertRaises(OSError) as raised:
self.driver.download_object_range(
obj=obj,
destination_path=destination_path,
start_bytes=0,
delete_on_failure=False,
)

self.assertIs(raised.exception, error)
self.assertTrue(os.path.exists(destination_path))

with open(destination_path, "rb") as fp:
self.assertEqual(fp.read(), b"partial data")

os.unlink(destination_path)

def test_download_object_range_as_stream_success(self):
content = b"0123456789123456789"
tmppath = self.make_tmp_file(content=content)
Expand Down
Loading