Skip to content

Commit

Permalink
fixes #384 (#415)
Browse files Browse the repository at this point in the history
* fixes #384
  • Loading branch information
aersam authored Jun 3, 2023
1 parent 71c0684 commit cdd9513
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
23 changes: 19 additions & 4 deletions adlfs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ class AzureBlobFileSystem(AsyncFileSystem):
version_aware : bool (False)
Whether to support blob versioning. If enable this will require the
user to have the necessary permissions for dealing with versioned blobs.
assume_container_exists: Optional[bool] (None)
Set this to true to not check for existance of containers at all, assuming they exist.
None (default) means to warn in case of a failure when checking for existance of a container
False throws if retrieving container properties fails, which might happen if your
authentication is only valid at the storage container level, and not the
storage account level.
Pass on to fsspec:
skip_instance_cache: to control reuse of instances
Expand Down Expand Up @@ -221,6 +226,7 @@ def __init__(
default_fill_cache: bool = True,
default_cache_type: str = "bytes",
version_aware: bool = False,
assume_container_exists: Optional[bool] = None,
**kwargs,
):
super_kwargs = {
Expand All @@ -245,6 +251,7 @@ def __init__(
self.location_mode = location_mode
self.credential = credential
self.request_session = request_session
self.assume_container_exists = assume_container_exists
if socket_timeout is not _SOCKET_TIMEOUT_DEFAULT:
warnings.warn(
"socket_timeout is deprecated and has no effect.", FutureWarning
Expand Down Expand Up @@ -1100,6 +1107,8 @@ async def _async_walk(self, path: str, maxdepth=None, **kwargs):
yield path, dirs, files

async def _container_exists(self, container_name):
if self.assume_container_exists:
return True
try:
async with self.service_client.get_container_client(
container_name
Expand All @@ -1108,9 +1117,15 @@ async def _container_exists(self, container_name):
except ResourceNotFoundError:
return False
except Exception as e:
raise ValueError(
f"Failed to fetch container properties for {container_name} for {e}"
) from e
if self.assume_container_exists is None:
warnings.warn(
f"Failed to fetch container properties for {container_name}. Assume it exists already",
)
return True
else:
raise ValueError(
f"Failed to fetch container properties for {container_name} for {e}"
) from e
else:
return True

Expand Down
17 changes: 17 additions & 0 deletions adlfs/tests/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ def test_mkdir(storage):
fs = AzureBlobFileSystem(
account_name=storage.account_name,
connection_string=CONN_STR,
assume_container_exists=False,
)

# Verify mkdir will create a new container when create_parents is True
Expand Down Expand Up @@ -810,6 +811,22 @@ def test_mkdir(storage):
with pytest.raises(PermissionError):
fs.mkdir("new-container/dir", create_parents=False)

fs = AzureBlobFileSystem(
account_name=storage.account_name,
connection_string=CONN_STR,
assume_container_exists=None,
)

with pytest.warns():
fs.mkdir("bad_container_name")

fs = AzureBlobFileSystem(
account_name=storage.account_name,
connection_string=CONN_STR,
assume_container_exists=True,
)
fs.mkdir("bad_container_name") # should not throw as we assume it exists


def test_makedir(storage):
fs = AzureBlobFileSystem(
Expand Down

0 comments on commit cdd9513

Please sign in to comment.