33# Licensed under the MIT License. See License.txt in the project root for
44# license information.
55# --------------------------------------------------------------------------
6+ # pylint: disable=docstring-keyword-should-match-keyword-only
7+
68import os
79
8- from typing import Union , Iterable , AnyStr , IO , Any , Dict # pylint: disable=unused-import
10+ from typing import Any , AnyStr , cast , Dict , IO , Iterable , Optional , Union , TYPE_CHECKING
911from ._version import VERSION
1012from ._blob_client import BlobClient
1113from ._container_client import ContainerClient
1618from ._shared_access_signature import generate_account_sas , generate_container_sas , generate_blob_sas
1719from ._shared .policies import ExponentialRetry , LinearRetry
1820from ._shared .response_handlers import PartialBatchErrorException
19- from ._shared .models import (
21+ from ._shared .models import (
2022 LocationMode ,
2123 ResourceTypes ,
2224 AccountSasPermissions ,
2325 StorageErrorCode ,
24- UserDelegationKey
25- )
26- from ._generated .models import (
27- RehydratePriority
26+ UserDelegationKey ,
27+ Services
2828)
29+ from ._generated .models import RehydratePriority
2930from ._models import (
3031 BlobType ,
3132 BlockState ,
3233 StandardBlobTier ,
3334 PremiumPageBlobTier ,
35+ BlobImmutabilityPolicyMode ,
3436 SequenceNumberAction ,
3537 PublicAccess ,
3638 BlobAnalyticsLogging ,
5456 BlobQueryError ,
5557 DelimitedJsonDialect ,
5658 DelimitedTextDialect ,
59+ QuickQueryDialect ,
5760 ArrowDialect ,
5861 ArrowType ,
5962 ObjectReplicationPolicy ,
60- ObjectReplicationRule
63+ ObjectReplicationRule ,
64+ ImmutabilityPolicy ,
6165)
6266from ._list_blobs_helper import BlobPrefix
6367
68+ if TYPE_CHECKING :
69+ from azure .core .credentials import AzureNamedKeyCredential , AzureSasCredential , TokenCredential
70+
6471__version__ = VERSION
6572
6673
6774def upload_blob_to_url (
68- blob_url , # type : str
69- data , # type : Union[Iterable[AnyStr], IO[AnyStr]]
70- credential = None , # type: Any
71- ** kwargs ):
72- # type: (... ) -> Dict[str, Any]
75+ blob_url : str ,
76+ data : Union [Iterable [AnyStr ], IO [AnyStr ]],
77+ credential : Optional [ Union [ str , Dict [ str , str ], "AzureNamedKeyCredential" , "AzureSasCredential" , "TokenCredential" ]] = None , # pylint: disable=line-too-long
78+ ** kwargs : Any
79+ ) -> Dict [str , Any ]:
7380 """Upload data to a given URL
7481
7582 The data will be uploaded as a block blob.
@@ -82,10 +89,17 @@ def upload_blob_to_url(
8289 :param credential:
8390 The credentials with which to authenticate. This is optional if the
8491 blob URL already has a SAS token. The value can be a SAS token string,
85- an instance of a AzureSasCredential from azure.core.credentials, an account
86- shared access key, or an instance of a TokenCredentials class from azure.identity.
92+ an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
93+ an account shared access key, or an instance of a TokenCredentials class from azure.identity.
8794 If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential
8895 - except in the case of AzureSasCredential, where the conflicting SAS tokens will raise a ValueError.
96+ If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
97+ should be the storage account key.
98+ :type credential:
99+ ~azure.core.credentials.AzureNamedKeyCredential or
100+ ~azure.core.credentials.AzureSasCredential or
101+ ~azure.core.credentials.TokenCredential or
102+ str or dict[str, str] or None
89103 :keyword bool overwrite:
90104 Whether the blob to be uploaded should overwrite the current data.
91105 If True, upload_blob_to_url will overwrite any existing data. If set to False, the
@@ -112,21 +126,26 @@ def upload_blob_to_url(
112126 :rtype: dict(str, Any)
113127 """
114128 with BlobClient .from_blob_url (blob_url , credential = credential ) as client :
115- return client .upload_blob (data = data , blob_type = BlobType .BlockBlob , ** kwargs )
129+ return cast ( BlobClient , client ) .upload_blob (data = data , blob_type = BlobType .BLOCKBLOB , ** kwargs )
116130
117131
118- def _download_to_stream (client , handle , ** kwargs ):
119- """Download data to specified open file-handle."""
132+ def _download_to_stream (client : BlobClient , handle : IO [bytes ], ** kwargs : Any ) -> None :
133+ """
134+ Download data to specified open file-handle.
135+
136+ :param BlobClient client: The BlobClient to download with.
137+ :param Stream handle: A Stream to download the data into.
138+ """
120139 stream = client .download_blob (** kwargs )
121140 stream .readinto (handle )
122141
123142
124143def download_blob_from_url (
125- blob_url , # type : str
126- output , # type: str
127- credential = None , # type: Any
128- ** kwargs ):
129- # type: (... ) -> None
144+ blob_url : str ,
145+ output : Union [ str , IO [ bytes ]],
146+ credential : Optional [ Union [ str , Dict [ str , str ], "AzureNamedKeyCredential" , "AzureSasCredential" , "TokenCredential" ]] = None , # pylint: disable=line-too-long
147+ ** kwargs : Any
148+ ) -> None :
130149 """Download the contents of a blob to a local file or stream.
131150
132151 :param str blob_url:
@@ -138,10 +157,17 @@ def download_blob_from_url(
138157 :param credential:
139158 The credentials with which to authenticate. This is optional if the
140159 blob URL already has a SAS token or the blob is public. The value can be a SAS token string,
141- an instance of a AzureSasCredential from azure.core.credentials,
160+ an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials,
142161 an account shared access key, or an instance of a TokenCredentials class from azure.identity.
143162 If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential
144163 - except in the case of AzureSasCredential, where the conflicting SAS tokens will raise a ValueError.
164+ If using an instance of AzureNamedKeyCredential, "name" should be the storage account name, and "key"
165+ should be the storage account key.
166+ :type credential:
167+ ~azure.core.credentials.AzureNamedKeyCredential or
168+ ~azure.core.credentials.AzureSasCredential or
169+ ~azure.core.credentials.TokenCredential or
170+ str or dict[str, str] or None
145171 :keyword bool overwrite:
146172 Whether the local file should be overwritten if it already exists. The default value is
147173 `False` - in which case a ValueError will be raised if the file already exists. If set to
@@ -169,10 +195,10 @@ def download_blob_from_url(
169195 overwrite = kwargs .pop ('overwrite' , False )
170196 with BlobClient .from_blob_url (blob_url , credential = credential ) as client :
171197 if hasattr (output , 'write' ):
172- _download_to_stream (client , output , ** kwargs )
198+ _download_to_stream (client , cast ( IO [ bytes ], output ) , ** kwargs )
173199 else :
174200 if not overwrite and os .path .isfile (output ):
175- raise ValueError ("The file '{}' already exists." . format ( output ) )
201+ raise ValueError (f "The file '{ output } ' already exists." )
176202 with open (output , 'wb' ) as file_handle :
177203 _download_to_stream (client , file_handle , ** kwargs )
178204
@@ -194,6 +220,8 @@ def download_blob_from_url(
194220 'StandardBlobTier' ,
195221 'PremiumPageBlobTier' ,
196222 'SequenceNumberAction' ,
223+ 'BlobImmutabilityPolicyMode' ,
224+ 'ImmutabilityPolicy' ,
197225 'PublicAccess' ,
198226 'BlobAnalyticsLogging' ,
199227 'Metrics' ,
@@ -210,6 +238,7 @@ def download_blob_from_url(
210238 'BlobBlock' ,
211239 'PageRange' ,
212240 'AccessPolicy' ,
241+ 'QuickQueryDialect' ,
213242 'ContainerSasPermissions' ,
214243 'BlobSasPermissions' ,
215244 'ResourceTypes' ,
@@ -229,5 +258,6 @@ def download_blob_from_url(
229258 'ArrowType' ,
230259 'BlobQueryReader' ,
231260 'ObjectReplicationPolicy' ,
232- 'ObjectReplicationRule'
261+ 'ObjectReplicationRule' ,
262+ 'Services' ,
233263]
0 commit comments