diff --git a/storages/backends/azure_storage.py b/storages/backends/azure_storage.py index 0ae14802..24d392d2 100644 --- a/storages/backends/azure_storage.py +++ b/storages/backends/azure_storage.py @@ -255,6 +255,8 @@ def _get_valid_path(self, name): return _get_valid_path(self._normalize_name(clean_name(name))) def _open(self, name, mode="rb"): + if not self.exists(name): + raise FileNotFoundError('File does not exist: %s' % name) return AzureStorageFile(name, mode, self) def get_available_name(self, name, max_length=_AZURE_NAME_MAX_LEN): diff --git a/tests/test_azure.py b/tests/test_azure.py index 9ae2e01c..d3907e0f 100644 --- a/tests/test_azure.py +++ b/tests/test_azure.py @@ -502,3 +502,13 @@ def test_blobserviceclient_no_custom_domain(self): "https://foo_name.blob.core.windows.net", credential={"account_name": "foo_name", "account_key": "foo_key"}, ) + + def test_missing_file_handling(self): + client_mock = mock.MagicMock() + client_mock.exists.side_effect = [True, False] + self.storage._client.get_blob_client.return_value = client_mock + + file_that_exists = self.storage._open('file-that-exists') + self.assertIsInstance(file_that_exists, azure_storage.AzureStorageFile) + + self.assertRaises(FileNotFoundError, self.storage._open, 'file-that-does-not-exist')