Skip to content

Commit 0c8d0c4

Browse files
pjbullM0dEx
andauthored
487 live tests (#491)
* fix(az): catch HttpResponseError in _check_hns (#487) * fix(az): catch HttpResponseError in _check_hns * chore: update HISTORY.md * format * cast fallback * review comment --------- Co-authored-by: Jakub Kubík <[email protected]>
1 parent 7f1a5dc commit 0c8d0c4

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fixed `CloudPath(...) / other` to correctly attempt to fall back on `other`'s `__rtruediv__` implementation, in order to support classes that explicitly support the `/` with a `CloudPath` instance. Previously, this would always raise a `TypeError` if `other` were not a `str` or `PurePosixPath`. (PR [#479](https://github.com/drivendataorg/cloudpathlib/pull/479))
6+
- Fixed an uncaught exception on Azure Gen2 storage accounts with HNS enabled when used with `DefaultAzureCredential`. (Issue [#486](https://github.com/drivendataorg/cloudpathlib/issues/486))
67

78
## v0.20.0 (2024-10-18)
89

cloudpathlib/azure/azblobclient.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
from datetime import datetime, timedelta
22
import mimetypes
33
import os
4+
from http import HTTPStatus
45
from pathlib import Path
56
from typing import Any, Callable, Dict, Iterable, Optional, Tuple, Union
67

8+
try:
9+
from typing import cast
10+
except ImportError:
11+
from typing_extensions import cast
712

813
from ..client import Client, register_client_class
914
from ..cloudpath import implementation_registry
@@ -13,7 +18,7 @@
1318

1419

1520
try:
16-
from azure.core.exceptions import ResourceNotFoundError
21+
from azure.core.exceptions import HttpResponseError, ResourceNotFoundError
1722
from azure.core.credentials import AzureNamedKeyCredential
1823

1924
from azure.storage.blob import (
@@ -202,20 +207,29 @@ def _check_hns(self, cloud_path: AzureBlobPath) -> Optional[bool]:
202207
try:
203208
account_info = self.service_client.get_account_information() # type: ignore
204209
self._hns_enabled = account_info.get("is_hns_enabled", False) # type: ignore
210+
211+
# get_account_information() not supported with this credential; we have to fallback to
212+
# checking if the root directory exists and is a has 'metadata': {'hdi_isfolder': 'true'}
205213
except ResourceNotFoundError:
206-
# get_account_information() not supported with this credential; we have to fallback to
207-
# checking if the root directory exists and is a has 'metadata': {'hdi_isfolder': 'true'}
208-
root_dir = self.service_client.get_blob_client(
209-
container=cloud_path.container, blob="/"
210-
)
211-
self._hns_enabled = (
212-
root_dir.exists()
213-
and root_dir.get_blob_properties().metadata.get("hdi_isfolder", False)
214-
== "true"
215-
)
214+
return self._check_hns_root_metadata(cloud_path)
215+
except HttpResponseError as error:
216+
if error.status_code == HTTPStatus.FORBIDDEN:
217+
return self._check_hns_root_metadata(cloud_path)
218+
else:
219+
raise
216220

217221
return self._hns_enabled
218222

223+
def _check_hns_root_metadata(self, cloud_path: AzureBlobPath) -> bool:
224+
root_dir = self.service_client.get_blob_client(container=cloud_path.container, blob="/")
225+
226+
self._hns_enabled = (
227+
root_dir.exists()
228+
and root_dir.get_blob_properties().metadata.get("hdi_isfolder", False) == "true"
229+
)
230+
231+
return cast(bool, self._hns_enabled)
232+
219233
def _get_metadata(
220234
self, cloud_path: AzureBlobPath
221235
) -> Union["BlobProperties", "FileProperties", Dict[str, Any]]:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ classifiers = [
3030
]
3131
requires-python = ">=3.8"
3232
dependencies = [
33-
"typing_extensions>4 ; python_version < '3.11'",
33+
"typing-extensions>4 ; python_version < '3.11'",
3434
]
3535

3636
[project.optional-dependencies]

0 commit comments

Comments
 (0)