7
7
8
8
import os
9
9
10
- 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
11
11
from ._version import VERSION
12
12
from ._blob_client import BlobClient
13
13
from ._container_client import ContainerClient
18
18
from ._shared_access_signature import generate_account_sas , generate_container_sas , generate_blob_sas
19
19
from ._shared .policies import ExponentialRetry , LinearRetry
20
20
from ._shared .response_handlers import PartialBatchErrorException
21
- from ._shared .models import (
21
+ from ._shared .models import (
22
22
LocationMode ,
23
23
ResourceTypes ,
24
24
AccountSasPermissions ,
25
25
StorageErrorCode ,
26
26
UserDelegationKey ,
27
27
Services
28
28
)
29
- from ._generated .models import (
30
- RehydratePriority ,
31
- )
29
+ from ._generated .models import RehydratePriority
32
30
from ._models import (
33
31
BlobType ,
34
32
BlockState ,
67
65
)
68
66
from ._list_blobs_helper import BlobPrefix
69
67
68
+ if TYPE_CHECKING :
69
+ from azure .core .credentials import AzureNamedKeyCredential , AzureSasCredential , TokenCredential
70
+
70
71
__version__ = VERSION
71
72
72
73
73
74
def upload_blob_to_url (
74
- blob_url , # type : str
75
- data , # type : Union[Iterable[AnyStr], IO[AnyStr]]
76
- credential = None , # type : Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
77
- ** kwargs ):
78
- # 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 ]:
79
80
"""Upload data to a given URL
80
81
81
82
The data will be uploaded as a block blob.
@@ -125,10 +126,10 @@ def upload_blob_to_url(
125
126
:rtype: dict(str, Any)
126
127
"""
127
128
with BlobClient .from_blob_url (blob_url , credential = credential ) as client :
128
- 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 )
129
130
130
131
131
- def _download_to_stream (client , handle , ** kwargs ) :
132
+ def _download_to_stream (client : BlobClient , handle : IO [ bytes ] , ** kwargs : Any ) -> None :
132
133
"""
133
134
Download data to specified open file-handle.
134
135
@@ -140,11 +141,11 @@ def _download_to_stream(client, handle, **kwargs):
140
141
141
142
142
143
def download_blob_from_url (
143
- blob_url , # type : str
144
- output , # type: str
145
- credential = None , # type : Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
146
- ** kwargs ):
147
- # 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 :
148
149
"""Download the contents of a blob to a local file or stream.
149
150
150
151
:param str blob_url:
@@ -194,7 +195,7 @@ def download_blob_from_url(
194
195
overwrite = kwargs .pop ('overwrite' , False )
195
196
with BlobClient .from_blob_url (blob_url , credential = credential ) as client :
196
197
if hasattr (output , 'write' ):
197
- _download_to_stream (client , output , ** kwargs )
198
+ _download_to_stream (client , cast ( IO [ bytes ], output ) , ** kwargs )
198
199
else :
199
200
if not overwrite and os .path .isfile (output ):
200
201
raise ValueError (f"The file '{ output } ' already exists." )
0 commit comments