diff --git a/mkdocs/docs/configuration.md b/mkdocs/docs/configuration.md index d72a4ead27..10fce23216 100644 --- a/mkdocs/docs/configuration.md +++ b/mkdocs/docs/configuration.md @@ -214,6 +214,7 @@ export AWS_RESPONSE_CHECKSUM_VALIDATION=when_required | Key | Example | Description | |------------------------------|---------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | adls.connection-string | AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqF...;BlobEndpoint= | A [connection string](https://learn.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string). This could be used to use FileIO with any adls-compatible object storage service that has a different endpoint (like [azurite](https://github.com/azure/azurite)). | +| `adls.connection-string.` | | The endpoint for the given host, matching Java. The key is the location host, for example `account.dfs.core.windows.net`. | | adls.account-name | devstoreaccount1 | The account that you want to connect to | | adls.account-key | Eby8vdM02xNOcqF... | The key to authentication against the account. | | adls.sas-token | NuHOuuzdQN7VRM%2FOpOeqBlawRCA845IY05h9eu1Yte4%3D | The shared access signature | diff --git a/pyiceberg/io/fsspec.py b/pyiceberg/io/fsspec.py index 87607d377b..a678c58650 100644 --- a/pyiceberg/io/fsspec.py +++ b/pyiceberg/io/fsspec.py @@ -301,6 +301,11 @@ def _adls(properties: Properties, hostname: str | None = None) -> AbstractFileSy if hostname and account_name is None: account_name = hostname.split(".")[0] + account_host = properties.get(ADLS_ACCOUNT_HOST) + if hostname and (endpoint := properties.get(f"{ADLS_CONNECTION_STRING}.{hostname}")): + # Java's adls.connection-string. holds the endpoint for that host, not an Azure connection string + account_host = urlparse(endpoint).netloc or endpoint + class StaticTokenCredential(AsyncTokenCredential): _DEFAULT_EXPIRY_SECONDS = 3600 @@ -328,7 +333,7 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken: tenant_id=properties.get(ADLS_TENANT_ID), client_id=properties.get(ADLS_CLIENT_ID), client_secret=properties.get(ADLS_CLIENT_SECRET), - account_host=properties.get(ADLS_ACCOUNT_HOST), + account_host=account_host, anon=properties.get(ADLS_ANON), ) diff --git a/tests/io/test_fsspec.py b/tests/io/test_fsspec.py index 9c76e535fe..386722c8bb 100644 --- a/tests/io/test_fsspec.py +++ b/tests/io/test_fsspec.py @@ -706,6 +706,50 @@ def test_adls_account_name_extracted_from_uri_hostname() -> None: ) +def test_fsspec_adls_accepts_java_connection_string_endpoint() -> None: + session_properties: Properties = { + "adls.connection-string.connectionaccount.dfs.core.windows.net": ("https://connectionaccount.dfs.core.windows.net"), + } + + with mock.patch("adlfs.AzureBlobFileSystem") as mock_adlfs: + FsspecFileIO(properties=session_properties).new_input( + location="abfss://container@connectionaccount.dfs.core.windows.net/file.parquet" + ) + + client_kwargs = mock_adlfs.call_args.kwargs + assert client_kwargs["account_name"] == "connectionaccount" + assert client_kwargs["account_host"] == "connectionaccount.dfs.core.windows.net" + assert client_kwargs["connection_string"] is None + + +def test_fsspec_adls_connection_string_endpoint_without_scheme() -> None: + session_properties: Properties = { + "adls.connection-string.connectionaccount.dfs.core.windows.net": "connectionaccount.dfs.core.windows.net", + } + + with mock.patch("adlfs.AzureBlobFileSystem") as mock_adlfs: + FsspecFileIO(properties=session_properties).new_input( + location="abfss://container@connectionaccount.dfs.core.windows.net/file.parquet" + ) + + assert mock_adlfs.call_args.kwargs["account_host"] == "connectionaccount.dfs.core.windows.net" + + +def test_fsspec_adls_connection_string_must_match_the_host() -> None: + session_properties: Properties = { + "adls.connection-string.connectionaccount": "https://connectionaccount.dfs.core.windows.net", + } + + with mock.patch("adlfs.AzureBlobFileSystem") as mock_adlfs: + FsspecFileIO(properties=session_properties).new_input( + location="abfss://container@connectionaccount.dfs.core.windows.net/file.parquet" + ) + + client_kwargs = mock_adlfs.call_args.kwargs + assert client_kwargs["account_host"] is None + assert client_kwargs["connection_string"] is None + + def test_adls_account_name_resolved_per_location() -> None: """Two locations in different accounts must each get a filesystem for their own account.""" session_properties: Properties = {"adls.tenant-id": "test-tenant-id"}