From 0f2b326eeea2772cb7eef0792373ae98f12e5d04 Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Tue, 18 Jun 2024 18:23:12 -0700 Subject: [PATCH] support signed urls via connection string alone --- adlfs/spec.py | 13 +++++++++++-- adlfs/tests/test_spec.py | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/adlfs/spec.py b/adlfs/spec.py index d6c52af4..f2bee65a 100644 --- a/adlfs/spec.py +++ b/adlfs/spec.py @@ -22,6 +22,7 @@ ResourceExistsError, ResourceNotFoundError, ) +from azure.core.utils import parse_connection_string from azure.storage.blob import ( BlobBlock, BlobProperties, @@ -1541,11 +1542,19 @@ async def _url( """ container_name, blob, version_id = self.split_path(path) + if self.connection_string: + args_dict = parse_connection_string(self.connection_string) + account_name = args_dict.get("accountname") + account_key = args_dict.get("accountkey") + else: + account_name = self.account_name + account_key = self.account_key + sas_token = generate_blob_sas( - account_name=self.account_name, + account_name=account_name, container_name=container_name, blob_name=blob, - account_key=self.account_key, + account_key=account_key, permission=BlobSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(seconds=expires), version_id=version_id, diff --git a/adlfs/tests/test_spec.py b/adlfs/tests/test_spec.py index a40df93f..3ccb1de7 100644 --- a/adlfs/tests/test_spec.py +++ b/adlfs/tests/test_spec.py @@ -1629,6 +1629,26 @@ async def test_url_versioned(storage, mocker): ) +async def test_url_with_conn_str(mocker): + fs = AzureBlobFileSystem(connection_string=CONN_STR) + generate_blob_sas = mocker.patch("adlfs.spec.generate_blob_sas") + + await fs._url("data/root/a/file.txt") + generate_blob_sas.assert_called_once_with( + account_name=ACCOUNT_NAME, + container_name="data", + blob_name="root/a/file.txt", + account_key=KEY, + permission=mocker.ANY, + expiry=mocker.ANY, + version_id=None, + content_disposition=None, + content_encoding=None, + content_language=None, + content_type=None, + ) + + def test_cp_file(storage): fs = AzureBlobFileSystem( account_name=storage.account_name, connection_string=CONN_STR