Download Contents from Azure Blob¶
Downloading files made easy.
Package Content¶
-
class
azblobexplorer.
AzureBlobDownload
(account_name: str, account_key: str, container_name: str)[source]¶ Bases:
azblobexplorer.base.BlobBase
Download a file or folder.
-
download_file
(blob_name: str, download_to: str = None, timeout: int = 10)[source]¶ Download a file to a location.
- Parameters
blob_name (str) – Give a blob path with file name.
download_to (str) – Give a local absolute path to download.
timeout (int) –
Request timeout in seconds
New in version 2.0.
- Raises
OSError – If the directory for
download_to
does not exists
>>> from azblobexplorer import AzureBlobDownload >>> az = AzureBlobDownload('account name', 'account key', 'container name') >>> az.download_file('some/name/file.txt')
-
download_folder
(blob_folder_name: str, download_to: str = None, timeout: int = 10)[source]¶ Download a blob folder.
- Parameters
blob_folder_name (str) – Give a folder name.
download_to (str) – Give a local path to download.
timeout (int) –
Request timeout in seconds
New in version 2.0.
- Raises
NoBlobsFound – If the blob folder is empty or is not found.
OSError – If the directory for
download_to
does not exists
>>> from azblobexplorer import AzureBlobDownload >>> az = AzureBlobDownload('account name', 'account key', 'container name') >>> az.download_folder('some/folder/name')
-
generate_url
(blob_name: str, read: bool = True, add: bool = False, create: bool = False, write: bool = False, delete: bool = False, sas: bool = False, access_time: int = 1) → str¶ Generate’s blob URL. It can also generate Shared Access Signature (SAS) if
sas=True
.- Parameters
write (bool) –
Write access
New in version 2.0.
create (bool) –
Create access
New in version 2.0.
add (bool) –
Add access
New in version 2.0.
read (bool) –
Read access
New in version 2.0.
delete (bool) –
Delete access
New in version 2.0.
access_time (int) – Time till the URL is valid
blob_name (str) – Name of the blob, this could be a path
sas (bool) – Set
True
to generate SAS key
- Returns
Blob URL
Example without ``sas``
>>> import os >>> from azblobexplorer import AzureBlobDownload >>> az = AzureBlobDownload('account name', 'account key', 'container name') >>> az.generate_url("filename.txt") https://containername.blob.core.windows.net/blobname/filename.txt
Example with ``upload_to`` and ``sas``
>>> import os >>> from azblobexplorer import AzureBlobDownload >>> az = AzureBlobDownload('account name', 'account key', 'container name') >>> az.generate_url("filename.txt", sas=True) https://containername.blob.core.windows.net/blobname/filename.txt?se=2019-11-05T16%3A33%3A46Z&sp=w&sv=2019-02-02&sr=b&sig=t%2BpUG2C2FQKp/Hb8SdCsmaZCZxbYXHUedwsquItGx%2BM%3D
-
generate_url_mime
(blob_name: str, mime_type: str, sas: bool = False, read: bool = True, add: bool = False, create: bool = False, write: bool = False, delete: bool = False, access_time: int = 1) → str¶ Generate’s blob URL with MIME type. It can also generate Shared Access Signature (SAS) if
sas=True
.- Parameters
write (bool) –
Write access
New in version 2.0.
create (bool) –
Create access
New in version 2.0.
add (bool) –
Add access
New in version 2.0.
read (bool) –
Read access
New in version 2.0.
delete (bool) –
Delete access
New in version 2.0.
access_time (int) – Time till the URL is valid
blob_name (str) – Name of the blob
access_time – Time till the URL is valid
mime_type (str) – MIME type of the application
sas (bool) – Set
True
to generate SAS key
- Returns
Blob URL
>>> import os >>> from azblobexplorer import AzureBlobDownload >>> az = AzureBlobDownload('account name', 'account key', 'container name') >>> az.generate_url_mime("filename.zip", sas=True, mime_type="application/zip") https://containername.blob.core.windows.net/blobname/filename.zip?se=2019-11-05T16%3A33%3A46Z&sp=w&sv=2019-02-02&sr=b&sig=t%2BpUG2C2FQKp/Hb8SdCsmaZCZxbYXHUedwsquItGx%2BM%3D
-
read_file
(blob_name: str, timeout: int = 10) → dict[source]¶ Read a file.
- Parameters
timeout (int) –
Request timeout in seconds
New in version 2.0.
blob_name (str) – Give a file name.
- Return type
dict
- Returns
Returns a dictionary of name, content,
>>> from azblobexplorer import AzureBlobDownload >>> az = AzureBlobDownload('account name', 'account key', 'container name') >>> az.read_file('some/name/file.txt') { 'file_name': 'file.txt', 'content': byte content, 'file_size_bytes': size in bytes }
-