Skip to content

Commit 0cb976d

Browse files
Merge pull request #200 from Aiven-Open/ilpo-azure-empty
Fix Azure empty object download
2 parents 1d0b29e + 10ba2eb commit 0cb976d

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

rohmu/object_storage/azure.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,11 @@ def _stream_blob(
362362
if progress_callback:
363363
progress_callback(start_range, file_size)
364364
except azure.core.exceptions.ResourceNotFoundError as ex:
365-
if ex.status_code == 416: # Empty file
366-
return
367365
raise FileNotFoundFromStorageError(key) from ex
366+
except azure.core.exceptions.HttpResponseError as ex:
367+
if ex.status_code == 416 and blob.get_blob_properties().size == 0: # Empty file
368+
return
369+
raise
368370

369371
def get_contents_to_fileobj(
370372
self,

test/object_storage/test_azure.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import BytesIO
44
from pytest_mock import MockerFixture
55
from rohmu.common.strenum import StrEnum
6-
from rohmu.errors import InvalidByteRangeError
6+
from rohmu.errors import FileNotFoundFromStorageError, InvalidByteRangeError
77
from rohmu.object_storage.azure import AzureTransfer
88
from rohmu.object_storage.config import AzureObjectStorageConfig
99
from tempfile import NamedTemporaryFile
@@ -108,6 +108,52 @@ def test_get_contents_to_fileobj_raises_error_on_invalid_byte_range(mock_get_blo
108108
)
109109

110110

111+
def test_get_contents_to_fileobj_not_found(mock_get_blob_client: MagicMock) -> None:
112+
notifier = MagicMock()
113+
transfer = AzureTransfer(
114+
bucket_name="test_bucket",
115+
account_name="test_account",
116+
account_key="test_key2",
117+
notifier=notifier,
118+
)
119+
120+
download_blob = MagicMock(side_effect=azure.core.exceptions.ResourceNotFoundError)
121+
mock_get_blob_client.return_value = MagicMock(download_blob=download_blob)
122+
with pytest.raises(FileNotFoundFromStorageError):
123+
transfer.get_contents_to_fileobj(
124+
key="testkey",
125+
fileobj_to_store_to=BytesIO(),
126+
)
127+
128+
129+
def test_get_contents_to_fileobj_empty_object(mock_get_blob_client: MagicMock) -> None:
130+
notifier = MagicMock()
131+
transfer = AzureTransfer(
132+
bucket_name="test_bucket",
133+
account_name="test_account",
134+
account_key="test_key2",
135+
notifier=notifier,
136+
)
137+
transfer._metadata_for_key = MagicMock(return_value={}) # type: ignore[method-assign]
138+
139+
def download_blob(*args: Any, **kwargs: Any) -> Any:
140+
raise azure.core.exceptions.HttpResponseError(
141+
message="The range specified is invalid for the current size of the resource.",
142+
response=MagicMock(reason="Range Not Satisfiable", status_code=416),
143+
)
144+
145+
def get_blob_properties(*args: Any, **kwargs: Any) -> Any:
146+
return MagicMock(size=0)
147+
148+
mock_get_blob_client.return_value = MagicMock(download_blob=download_blob, get_blob_properties=get_blob_properties)
149+
fileobj = BytesIO()
150+
transfer.get_contents_to_fileobj(
151+
key="testkey",
152+
fileobj_to_store_to=fileobj,
153+
)
154+
assert fileobj.getvalue() == b""
155+
156+
111157
def test_minimal_config() -> None:
112158
config = AzureObjectStorageConfig(account_name="test", bucket_name=None, account_key=None, sas_token=None)
113159
assert config.account_name == "test"

0 commit comments

Comments
 (0)