Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure: raise FileNotFoundError when requested blob is missing from storage #1358

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions storages/backends/azure_storage.py
Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_azure.py
Expand Up @@ -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

with self.storage.open('file-that-exists') as file_that_exists:
self.assertIsInstance(file_that_exists, azure_storage.AzureStorageFile)

self.assertRaises(FileNotFoundError, self.storage.open, 'file-that-does-not-exist')