Skip to content

Commit

Permalink
Merge pull request #1 from torchbox-forks/fix/azure-missing-blob-hand…
Browse files Browse the repository at this point in the history
…ling

Azure: raise FileNotFoundError when requested blob is missing from storage
  • Loading branch information
alxbridge authored Mar 1, 2024
2 parents 70fa8d9 + aaaed8b commit 69a3d97
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
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')
self.assertIsInstance(file_that_exists, azure_storage.AzureStorageFile)

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

0 comments on commit 69a3d97

Please sign in to comment.