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 7 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions storages/backends/azure_storage.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

file_that_exists = self.storage._open('file-that-exists')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to use open() as that is the public API. Also to use a with context handler.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just pushed commits to make these changes

self.assertIsInstance(file_that_exists, azure_storage.AzureStorageFile)

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