|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | +import os |
| 7 | + |
| 8 | +from typing import Union, Iterable, AnyStr, IO, Any, Dict # pylint: disable=unused-import |
| 9 | +from ._version import VERSION |
| 10 | +from ._blob_client import BlobClient |
| 11 | +from ._container_client import ContainerClient |
| 12 | +from ._blob_service_client import BlobServiceClient |
| 13 | +from ._lease import BlobLeaseClient |
| 14 | +from ._download import StorageStreamDownloader |
| 15 | +from ._quick_query_helper import BlobQueryReader |
| 16 | +from ._shared_access_signature import generate_account_sas, generate_container_sas, generate_blob_sas |
| 17 | +from ._shared.policies import ExponentialRetry, LinearRetry |
| 18 | +from ._shared.response_handlers import PartialBatchErrorException |
| 19 | +from ._shared.models import( |
| 20 | + LocationMode, |
| 21 | + ResourceTypes, |
| 22 | + AccountSasPermissions, |
| 23 | + StorageErrorCode, |
| 24 | + UserDelegationKey |
| 25 | +) |
| 26 | +from ._generated.models import ( |
| 27 | + RehydratePriority, |
| 28 | +) |
| 29 | +from ._models import ( |
| 30 | + BlobType, |
| 31 | + BlockState, |
| 32 | + StandardBlobTier, |
| 33 | + PremiumPageBlobTier, |
| 34 | + BlobImmutabilityPolicyMode, |
| 35 | + SequenceNumberAction, |
| 36 | + PublicAccess, |
| 37 | + BlobAnalyticsLogging, |
| 38 | + Metrics, |
| 39 | + RetentionPolicy, |
| 40 | + StaticWebsite, |
| 41 | + CorsRule, |
| 42 | + ContainerProperties, |
| 43 | + BlobProperties, |
| 44 | + FilteredBlob, |
| 45 | + LeaseProperties, |
| 46 | + ContentSettings, |
| 47 | + CopyProperties, |
| 48 | + BlobBlock, |
| 49 | + PageRange, |
| 50 | + AccessPolicy, |
| 51 | + ContainerSasPermissions, |
| 52 | + BlobSasPermissions, |
| 53 | + CustomerProvidedEncryptionKey, |
| 54 | + ContainerEncryptionScope, |
| 55 | + BlobQueryError, |
| 56 | + DelimitedJsonDialect, |
| 57 | + DelimitedTextDialect, |
| 58 | + QuickQueryDialect, |
| 59 | + ArrowDialect, |
| 60 | + ArrowType, |
| 61 | + ObjectReplicationPolicy, |
| 62 | + ObjectReplicationRule, |
| 63 | + ImmutabilityPolicy |
| 64 | +) |
| 65 | +from ._list_blobs_helper import BlobPrefix |
| 66 | + |
| 67 | +__version__ = VERSION |
| 68 | + |
| 69 | + |
| 70 | +def upload_blob_to_url( |
| 71 | + blob_url, # type: str |
| 72 | + data, # type: Union[Iterable[AnyStr], IO[AnyStr]] |
| 73 | + credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long |
| 74 | + **kwargs): |
| 75 | + # type: (...) -> Dict[str, Any] |
| 76 | + """Upload data to a given URL |
| 77 | +
|
| 78 | + The data will be uploaded as a block blob. |
| 79 | +
|
| 80 | + :param str blob_url: |
| 81 | + The full URI to the blob. This can also include a SAS token. |
| 82 | + :param data: |
| 83 | + The data to upload. This can be bytes, text, an iterable or a file-like object. |
| 84 | + :type data: bytes or str or Iterable |
| 85 | + :param credential: |
| 86 | + The credentials with which to authenticate. This is optional if the |
| 87 | + blob URL already has a SAS token. The value can be a SAS token string, |
| 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. |
| 90 | + If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential |
| 91 | + - 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. |
| 94 | + :keyword bool overwrite: |
| 95 | + Whether the blob to be uploaded should overwrite the current data. |
| 96 | + If True, upload_blob_to_url will overwrite any existing data. If set to False, the |
| 97 | + operation will fail with a ResourceExistsError. |
| 98 | + :keyword int max_concurrency: |
| 99 | + The number of parallel connections with which to download. |
| 100 | + :keyword int length: |
| 101 | + Number of bytes to read from the stream. This is optional, but |
| 102 | + should be supplied for optimal performance. |
| 103 | + :keyword dict(str,str) metadata: |
| 104 | + Name-value pairs associated with the blob as metadata. |
| 105 | + :keyword bool validate_content: |
| 106 | + If true, calculates an MD5 hash for each chunk of the blob. The storage |
| 107 | + service checks the hash of the content that has arrived with the hash |
| 108 | + that was sent. This is primarily valuable for detecting bitflips on |
| 109 | + the wire if using http instead of https as https (the default) will |
| 110 | + already validate. Note that this MD5 hash is not stored with the |
| 111 | + blob. Also note that if enabled, the memory-efficient upload algorithm |
| 112 | + will not be used, because computing the MD5 hash requires buffering |
| 113 | + entire blocks, and doing so defeats the purpose of the memory-efficient algorithm. |
| 114 | + :keyword str encoding: |
| 115 | + Encoding to use if text is supplied as input. Defaults to UTF-8. |
| 116 | + :returns: Blob-updated property dict (Etag and last modified) |
| 117 | + :rtype: dict(str, Any) |
| 118 | + """ |
| 119 | + with BlobClient.from_blob_url(blob_url, credential=credential) as client: |
| 120 | + return client.upload_blob(data=data, blob_type=BlobType.BlockBlob, **kwargs) |
| 121 | + |
| 122 | + |
| 123 | +def _download_to_stream(client, handle, **kwargs): |
| 124 | + """Download data to specified open file-handle.""" |
| 125 | + stream = client.download_blob(**kwargs) |
| 126 | + stream.readinto(handle) |
| 127 | + |
| 128 | + |
| 129 | +def download_blob_from_url( |
| 130 | + blob_url, # type: str |
| 131 | + output, # type: str |
| 132 | + credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long |
| 133 | + **kwargs): |
| 134 | + # type: (...) -> None |
| 135 | + """Download the contents of a blob to a local file or stream. |
| 136 | +
|
| 137 | + :param str blob_url: |
| 138 | + The full URI to the blob. This can also include a SAS token. |
| 139 | + :param output: |
| 140 | + Where the data should be downloaded to. This could be either a file path to write to, |
| 141 | + or an open IO handle to write to. |
| 142 | + :type output: str or writable stream. |
| 143 | + :param credential: |
| 144 | + The credentials with which to authenticate. This is optional if the |
| 145 | + blob URL already has a SAS token or the blob is public. The value can be a SAS token string, |
| 146 | + an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials, |
| 147 | + an account shared access key, or an instance of a TokenCredentials class from azure.identity. |
| 148 | + If the resource URI already contains a SAS token, this will be ignored in favor of an explicit credential |
| 149 | + - 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. |
| 152 | + :keyword bool overwrite: |
| 153 | + Whether the local file should be overwritten if it already exists. The default value is |
| 154 | + `False` - in which case a ValueError will be raised if the file already exists. If set to |
| 155 | + `True`, an attempt will be made to write to the existing file. If a stream handle is passed |
| 156 | + in, this value is ignored. |
| 157 | + :keyword int max_concurrency: |
| 158 | + The number of parallel connections with which to download. |
| 159 | + :keyword int offset: |
| 160 | + Start of byte range to use for downloading a section of the blob. |
| 161 | + Must be set if length is provided. |
| 162 | + :keyword int length: |
| 163 | + Number of bytes to read from the stream. This is optional, but |
| 164 | + should be supplied for optimal performance. |
| 165 | + :keyword bool validate_content: |
| 166 | + If true, calculates an MD5 hash for each chunk of the blob. The storage |
| 167 | + service checks the hash of the content that has arrived with the hash |
| 168 | + that was sent. This is primarily valuable for detecting bitflips on |
| 169 | + the wire if using http instead of https as https (the default) will |
| 170 | + already validate. Note that this MD5 hash is not stored with the |
| 171 | + blob. Also note that if enabled, the memory-efficient upload algorithm |
| 172 | + will not be used, because computing the MD5 hash requires buffering |
| 173 | + entire blocks, and doing so defeats the purpose of the memory-efficient algorithm. |
| 174 | + :rtype: None |
| 175 | + """ |
| 176 | + overwrite = kwargs.pop('overwrite', False) |
| 177 | + with BlobClient.from_blob_url(blob_url, credential=credential) as client: |
| 178 | + if hasattr(output, 'write'): |
| 179 | + _download_to_stream(client, output, **kwargs) |
| 180 | + else: |
| 181 | + if not overwrite and os.path.isfile(output): |
| 182 | + raise ValueError("The file '{}' already exists.".format(output)) |
| 183 | + with open(output, 'wb') as file_handle: |
| 184 | + _download_to_stream(client, file_handle, **kwargs) |
| 185 | + |
| 186 | + |
| 187 | +__all__ = [ |
| 188 | + 'upload_blob_to_url', |
| 189 | + 'download_blob_from_url', |
| 190 | + 'BlobServiceClient', |
| 191 | + 'ContainerClient', |
| 192 | + 'BlobClient', |
| 193 | + 'BlobType', |
| 194 | + 'BlobLeaseClient', |
| 195 | + 'StorageErrorCode', |
| 196 | + 'UserDelegationKey', |
| 197 | + 'ExponentialRetry', |
| 198 | + 'LinearRetry', |
| 199 | + 'LocationMode', |
| 200 | + 'BlockState', |
| 201 | + 'StandardBlobTier', |
| 202 | + 'PremiumPageBlobTier', |
| 203 | + 'SequenceNumberAction', |
| 204 | + 'BlobImmutabilityPolicyMode', |
| 205 | + 'ImmutabilityPolicy', |
| 206 | + 'PublicAccess', |
| 207 | + 'BlobAnalyticsLogging', |
| 208 | + 'Metrics', |
| 209 | + 'RetentionPolicy', |
| 210 | + 'StaticWebsite', |
| 211 | + 'CorsRule', |
| 212 | + 'ContainerProperties', |
| 213 | + 'BlobProperties', |
| 214 | + 'BlobPrefix', |
| 215 | + 'FilteredBlob', |
| 216 | + 'LeaseProperties', |
| 217 | + 'ContentSettings', |
| 218 | + 'CopyProperties', |
| 219 | + 'BlobBlock', |
| 220 | + 'PageRange', |
| 221 | + 'AccessPolicy', |
| 222 | + 'QuickQueryDialect', |
| 223 | + 'ContainerSasPermissions', |
| 224 | + 'BlobSasPermissions', |
| 225 | + 'ResourceTypes', |
| 226 | + 'AccountSasPermissions', |
| 227 | + 'StorageStreamDownloader', |
| 228 | + 'CustomerProvidedEncryptionKey', |
| 229 | + 'RehydratePriority', |
| 230 | + 'generate_account_sas', |
| 231 | + 'generate_container_sas', |
| 232 | + 'generate_blob_sas', |
| 233 | + 'PartialBatchErrorException', |
| 234 | + 'ContainerEncryptionScope', |
| 235 | + 'BlobQueryError', |
| 236 | + 'DelimitedJsonDialect', |
| 237 | + 'DelimitedTextDialect', |
| 238 | + 'ArrowDialect', |
| 239 | + 'ArrowType', |
| 240 | + 'BlobQueryReader', |
| 241 | + 'ObjectReplicationPolicy', |
| 242 | + 'ObjectReplicationRule' |
| 243 | +] |
0 commit comments