In Azure, you can store your data in various storage options provided by Azure such as Blob, Table, CosmosDB, SQL-DB, etc.
In this shot, we will learn how to read or download the data from a file stored in Azure Blob Storage using Python.
Azure Blob Storage is Microsoft’s object storage solution for the cloud. Blob storage is optimized to store massive amounts of unstructured data.
Unstructured data is data that doesn’t adhere to a particular data model or definition, such as text or binary data.
The first step is to create an Azure Storage account. Then, follow the instructions below:
Now, we are ready to connect this storage account using Python. First, install the required packages. Then, run the following command:
pip install azure-storage-blob
Below is the code to connect your storage account with the Python script:
from azure.storage.blob import BlobServiceClient STORAGEACCOUNTURL = "https://<STORAGE_ACCOUNT_NAME>.blob.core.windows.net" STORAGEACCOUNTKEY = "<STORAGE_ACCOUNT_KEY>" CONTAINERNAME = "<CONTAINER_NAME>" BLOBNAME = "<BLOB_FILE_NAME>" blob_service_client_instance = BlobServiceClient( account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY) blob_client_instance = blob_service_client_instance.get_blob_client( CONTAINERNAME, BLOBNAME, snapshot=None) blob_data = blob_client_instance.download_blob() data = blob_data.readall() print(data)
BlobServiceClient()class by passing the storage account URL and the access key. This will establish a connection to our storage account.
get_blob_client()function to connect to our blob file. We then pass the container name and the blob name to this function.
By following these steps, you can use Python to easily read and download files from Azure Blob Storage.
View all Courses