Skip to content

Commit c3e399a

Browse files
jabberakou
andauthored
GH-45705: [Python] Add support for SAS token in AzureFileSystem (#45706)
### Rationale for this change Python bindings could use the ability to use SAS tokens just like C++ ### What changes are included in this PR? AzureFileSystem now includes a sas_token parameter ### Are these changes tested? Yes. ### Are there any user-facing changes? Additional parameter to AzureFileSystem * GitHub Issue: #45705 Lead-authored-by: Mike Barry <github@barry.io> Co-authored-by: Mike <jabbera@users.noreply.github.com> Co-authored-by: Sutou Kouhei <kou@cozmixng.org> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 08089df commit c3e399a

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

python/pyarrow/_azurefs.pyx

+17-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ cdef class AzureFileSystem(FileSystem):
4747
Azure Blob Storage account name. This is the globally unique identifier for the
4848
storage account.
4949
account_key : str, default None
50-
Account key of the storage account. Pass None to use default credential.
50+
Account key of the storage account. If sas_token and account_key are None the
51+
default credential will be used. The parameters account_key and sas_token are
52+
mutually exclusive.
5153
blob_storage_authority : str, default None
5254
hostname[:port] of the Blob Service. Defaults to `.blob.core.windows.net`. Useful
5355
for connecting to a local emulator, like Azurite.
@@ -60,6 +62,10 @@ cdef class AzureFileSystem(FileSystem):
6062
dfs_storage_scheme : str, default None
6163
Either `http` or `https`. Defaults to `https`. Useful for connecting to a local
6264
emulator, like Azurite.
65+
sas_token : str, default None
66+
SAS token for the storage account, used as an alternative to account_key. If sas_token
67+
and account_key are None the default credential will be used. The parameters
68+
account_key and sas_token are mutually exclusive.
6369
6470
Examples
6571
--------
@@ -79,10 +85,11 @@ cdef class AzureFileSystem(FileSystem):
7985
cdef:
8086
CAzureFileSystem* azurefs
8187
c_string account_key
88+
c_string sas_token
8289

8390
def __init__(self, account_name, *, account_key=None, blob_storage_authority=None,
8491
dfs_storage_authority=None, blob_storage_scheme=None,
85-
dfs_storage_scheme=None):
92+
dfs_storage_scheme=None, sas_token=None):
8693
cdef:
8794
CAzureOptions options
8895
shared_ptr[CAzureFileSystem] wrapped
@@ -97,9 +104,15 @@ cdef class AzureFileSystem(FileSystem):
97104
if dfs_storage_scheme:
98105
options.dfs_storage_scheme = tobytes(dfs_storage_scheme)
99106

107+
if account_key and sas_token:
108+
raise ValueError("Cannot specify both account_key and sas_token.")
109+
100110
if account_key:
101111
options.ConfigureAccountKeyCredential(tobytes(account_key))
102112
self.account_key = tobytes(account_key)
113+
elif sas_token:
114+
options.ConfigureSASCredential(tobytes(sas_token))
115+
self.sas_token = tobytes(sas_token)
103116
else:
104117
options.ConfigureDefaultCredential()
105118

@@ -127,5 +140,6 @@ cdef class AzureFileSystem(FileSystem):
127140
blob_storage_authority=frombytes(opts.blob_storage_authority),
128141
dfs_storage_authority=frombytes(opts.dfs_storage_authority),
129142
blob_storage_scheme=frombytes(opts.blob_storage_scheme),
130-
dfs_storage_scheme=frombytes(opts.dfs_storage_scheme)
143+
dfs_storage_scheme=frombytes(opts.dfs_storage_scheme),
144+
sas_token=frombytes(self.sas_token)
131145
),))

python/pyarrow/includes/libarrow_fs.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ cdef extern from "arrow/filesystem/api.h" namespace "arrow::fs" nogil:
252252
c_bool Equals(const CAzureOptions& other)
253253
CStatus ConfigureDefaultCredential()
254254
CStatus ConfigureAccountKeyCredential(c_string account_key)
255+
CStatus ConfigureSASCredential(c_string sas_token)
255256

256257
cdef cppclass CAzureFileSystem "arrow::fs::AzureFileSystem":
257258
@staticmethod

python/pyarrow/tests/test_fs.py

+10
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,16 @@ def test_azurefs_options(pickle_module):
14631463
assert pickle_module.loads(pickle_module.dumps(fs3)) == fs3
14641464
assert fs3 != fs2
14651465

1466+
fs4 = AzureFileSystem(account_name='fake-account-name',
1467+
sas_token='fakesastoken')
1468+
assert isinstance(fs4, AzureFileSystem)
1469+
assert pickle_module.loads(pickle_module.dumps(fs4)) == fs4
1470+
assert fs4 != fs3
1471+
1472+
with pytest.raises(ValueError):
1473+
AzureFileSystem(account_name='fake-account-name', account_key='fakeaccount',
1474+
sas_token='fakesastoken')
1475+
14661476
with pytest.raises(TypeError):
14671477
AzureFileSystem()
14681478

0 commit comments

Comments
 (0)