Skip to content

Commit 1844ef9

Browse files
[Storage] Handling AzureNamedKeyCredential in Python SDK (Azure#24967)
1 parent 8dad21e commit 1844ef9

File tree

57 files changed

+1209
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1209
-188
lines changed

sdk/storage/azure-storage-blob-changefeed/azure/storage/blob/changefeed/_change_feed_client.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ class ChangeFeedClient(object): # pylint: disable=too-many-public-methods
2525
:param credential:
2626
The credentials with which to authenticate. This is optional if the
2727
account URL already has a SAS token. The value can be a SAS token string,
28-
an instance of a AzureSasCredential from azure.core.credentials, an account
29-
shared access key, or an instance of a TokenCredentials class from azure.identity.
28+
an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
29+
an account shared access key, or an instance of a TokenCredentials class from azure.identity.
3030
If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential
3131
- except in the case of AzureSasCredential, where the conflicting SAS tokens will raise a ValueError.
32+
If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
33+
should be the storage account key.
3234
:keyword str secondary_hostname:
3335
The hostname of the secondary endpoint.
3436
:keyword int max_single_get_size:
@@ -51,7 +53,7 @@ class ChangeFeedClient(object): # pylint: disable=too-many-public-methods
5153
"""
5254
def __init__(
5355
self, account_url, # type: str
54-
credential=None, # type: Optional[Any]
56+
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
5557
**kwargs # type: Any
5658
):
5759
# type: (...) -> None
@@ -60,7 +62,7 @@ def __init__(
6062
@classmethod
6163
def from_connection_string(
6264
cls, conn_str, # type: str
63-
credential=None, # type: Optional[Any]
65+
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
6466
**kwargs # type: Any
6567
): # type: (...) -> ChangeFeedClient
6668
"""Create ChangeFeedClient from a Connection String.
@@ -71,9 +73,11 @@ def from_connection_string(
7173
The credentials with which to authenticate. This is optional if the
7274
account URL already has a SAS token, or the connection string already has shared
7375
access key values. The value can be a SAS token string,
74-
an instance of a AzureSasCredential from azure.core.credentials, an account shared access
75-
key, or an instance of a TokenCredentials class from azure.identity.
76+
an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
77+
an account shared access key, or an instance of a TokenCredentials class from azure.identity.
7678
Credentials provided here will take precedence over those in the connection string.
79+
If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
80+
should be the storage account key.
7781
:returns: A change feed client.
7882
:rtype: ~azure.storage.blob.changefeed.ChangeFeedClient
7983

sdk/storage/azure-storage-blob/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
This version and all future versions will require Python 3.7+. Python 3.6 is no longer supported.
66

77
### Features Added
8+
- Added support for `AzureNamedKeyCredential` as a valid `credential` type.
89

910
### Bugs Fixed
1011
- Adjusted type hints for `upload_blob` and `StorageStreamDownloader.readall`.

sdk/storage/azure-storage-blob/azure/storage/blob/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
def upload_blob_to_url(
7171
blob_url, # type: str
7272
data, # type: Union[Iterable[AnyStr], IO[AnyStr]]
73-
credential=None, # type: Any
73+
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
7474
**kwargs):
7575
# type: (...) -> Dict[str, Any]
7676
"""Upload data to a given URL
@@ -85,10 +85,12 @@ def upload_blob_to_url(
8585
:param credential:
8686
The credentials with which to authenticate. This is optional if the
8787
blob URL already has a SAS token. The value can be a SAS token string,
88-
an instance of a AzureSasCredential from azure.core.credentials, an account
89-
shared access key, or an instance of a TokenCredentials class from azure.identity.
88+
an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
89+
an account shared access key, or an instance of a TokenCredentials class from azure.identity.
9090
If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential
9191
- except in the case of AzureSasCredential, where the conflicting SAS tokens will raise a ValueError.
92+
If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
93+
should be the storage account key.
9294
:keyword bool overwrite:
9395
Whether the blob to be uploaded should overwrite the current data.
9496
If True, upload_blob_to_url will overwrite any existing data. If set to False, the
@@ -127,7 +129,7 @@ def _download_to_stream(client, handle, **kwargs):
127129
def download_blob_from_url(
128130
blob_url, # type: str
129131
output, # type: str
130-
credential=None, # type: Any
132+
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
131133
**kwargs):
132134
# type: (...) -> None
133135
"""Download the contents of a blob to a local file or stream.
@@ -141,10 +143,12 @@ def download_blob_from_url(
141143
:param credential:
142144
The credentials with which to authenticate. This is optional if the
143145
blob URL already has a SAS token or the blob is public. The value can be a SAS token string,
144-
an instance of a AzureSasCredential from azure.core.credentials,
146+
an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
145147
an account shared access key, or an instance of a TokenCredentials class from azure.identity.
146148
If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential
147149
- except in the case of AzureSasCredential, where the conflicting SAS tokens will raise a ValueError.
150+
If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
151+
should be the storage account key.
148152
:keyword bool overwrite:
149153
Whether the local file should be overwritten if it already exists. The default value is
150154
`False` - in which case a ValueError will be raised if the file already exists. If set to

sdk/storage/azure-storage-blob/azure/storage/blob/_blob_client.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ class BlobClient(StorageAccountHostsMixin, StorageEncryptionMixin): # pylint: d
104104
:param credential:
105105
The credentials with which to authenticate. This is optional if the
106106
account URL already has a SAS token. The value can be a SAS token string,
107-
an instance of a AzureSasCredential from azure.core.credentials, an account
108-
shared access key, or an instance of a TokenCredentials class from azure.identity.
107+
an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
108+
an account shared access key, or an instance of a TokenCredentials class from azure.identity.
109109
If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential
110110
- except in the case of AzureSasCredential, where the conflicting SAS tokens will raise a ValueError.
111+
If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
112+
should be the storage account key.
111113
:keyword str api_version:
112114
The Storage API version to use for requests. Default value is the most recent service version that is
113115
compatible with the current SDK. Setting to an older version may result in reduced feature compatibility.
@@ -151,7 +153,7 @@ def __init__(
151153
container_name, # type: str
152154
blob_name, # type: str
153155
snapshot=None, # type: Optional[Union[str, Dict[str, Any]]]
154-
credential=None, # type: Optional[Any]
156+
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
155157
**kwargs # type: Any
156158
):
157159
# type: (...) -> None
@@ -210,8 +212,13 @@ def _encode_source_url(self, source_url):
210212
return '?'.join(result)
211213

212214
@classmethod
213-
def from_blob_url(cls, blob_url, credential=None, snapshot=None, **kwargs):
214-
# type: (Type[ClassType], str, Optional[Any], Optional[Union[str, Dict[str, Any]]], Any) -> ClassType
215+
def from_blob_url(
216+
cls, # type: Type[ClassType]
217+
blob_url, # type: str
218+
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
219+
snapshot=None, # type: Optional[Union[str, Dict[str, Any]]]
220+
**kwargs # type: Any
221+
): # type: (...) -> ClassType
215222
"""Create BlobClient from a blob url. This doesn't support customized blob url with '/' in blob name.
216223
217224
:param str blob_url:
@@ -222,10 +229,12 @@ def from_blob_url(cls, blob_url, credential=None, snapshot=None, **kwargs):
222229
The credentials with which to authenticate. This is optional if the
223230
account URL already has a SAS token, or the connection string already has shared
224231
access key values. The value can be a SAS token string,
225-
an instance of a AzureSasCredential from azure.core.credentials, an account shared access
226-
key, or an instance of a TokenCredentials class from azure.identity.
232+
an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
233+
an account shared access key, or an instance of a TokenCredentials class from azure.identity.
227234
If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential
228235
- except in the case of AzureSasCredential, where the conflicting SAS tokens will raise a ValueError.
236+
If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
237+
should be the storage account key.
229238
:param str snapshot:
230239
The optional blob snapshot on which to operate. This can be the snapshot ID string
231240
or the response returned from :func:`create_snapshot`. If specified, this will override
@@ -291,7 +300,7 @@ def from_connection_string(
291300
container_name, # type: str
292301
blob_name, # type: str
293302
snapshot=None, # type: Optional[str]
294-
credential=None, # type: Optional[Any]
303+
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
295304
**kwargs # type: Any
296305
): # type: (...) -> ClassType
297306
"""Create BlobClient from a Connection String.
@@ -309,9 +318,11 @@ def from_connection_string(
309318
The credentials with which to authenticate. This is optional if the
310319
account URL already has a SAS token, or the connection string already has shared
311320
access key values. The value can be a SAS token string,
312-
an instance of a AzureSasCredential from azure.core.credentials, an account shared access
313-
key, or an instance of a TokenCredentials class from azure.identity.
321+
an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
322+
an account shared access key, or an instance of a TokenCredentials class from azure.identity.
314323
Credentials provided here will take precedence over those in the connection string.
324+
If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
325+
should be the storage account key.
315326
:returns: A Blob client.
316327
:rtype: ~azure.storage.blob.BlobClient
317328

sdk/storage/azure-storage-blob/azure/storage/blob/_blob_service_client.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ class BlobServiceClient(StorageAccountHostsMixin, StorageEncryptionMixin):
7373
:param credential:
7474
The credentials with which to authenticate. This is optional if the
7575
account URL already has a SAS token. The value can be a SAS token string,
76-
an instance of a AzureSasCredential from azure.core.credentials, an account
77-
shared access key, or an instance of a TokenCredentials class from azure.identity.
76+
an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
77+
an account shared access key, or an instance of a TokenCredentials class from azure.identity.
7878
If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential
7979
- except in the case of AzureSasCredential, where the conflicting SAS tokens will raise a ValueError.
80+
If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
81+
should be the storage account key.
8082
:keyword str api_version:
8183
The Storage API version to use for requests. Default value is the most recent service version that is
8284
compatible with the current SDK. Setting to an older version may result in reduced feature compatibility.
@@ -118,7 +120,7 @@ class BlobServiceClient(StorageAccountHostsMixin, StorageEncryptionMixin):
118120

119121
def __init__(
120122
self, account_url, # type: str
121-
credential=None, # type: Optional[Any]
123+
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
122124
**kwargs # type: Any
123125
):
124126
# type: (...) -> None
@@ -148,7 +150,7 @@ def _format_url(self, hostname):
148150
def from_connection_string(
149151
cls, # type: Type[ClassType]
150152
conn_str, # type: str
151-
credential=None, # type: Optional[Any]
153+
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
152154
**kwargs # type: Any
153155
): # type: (...) -> ClassType
154156
"""Create BlobServiceClient from a Connection String.
@@ -159,9 +161,11 @@ def from_connection_string(
159161
The credentials with which to authenticate. This is optional if the
160162
account URL already has a SAS token, or the connection string already has shared
161163
access key values. The value can be a SAS token string,
162-
an instance of a AzureSasCredential from azure.core.credentials, an account shared access
163-
key, or an instance of a TokenCredentials class from azure.identity.
164+
an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
165+
an account shared access key, or an instance of a TokenCredentials class from azure.identity.
164166
Credentials provided here will take precedence over those in the connection string.
167+
If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
168+
should be the storage account key.
165169
:returns: A Blob service client.
166170
:rtype: ~azure.storage.blob.BlobServiceClient
167171

0 commit comments

Comments
 (0)