Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
azure
python

How to download files from Azure Blob Storage using Python

Harsh Jain

Introduction

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:

  • Search Storage Accounts in the Azure Portal.
  • Click New to create a new storage account.
  • Fill in all the detailsresource group, subscription, name of storage account, region, etc..
  • After successfully creating your storage account, open your storage account and click on Access Keys from the left navigation pane to get your storage account credentials. Copy and save them for future use.
  • Go to the Container section and create a new container. Please provide a name for your container and save it for future use.
  • You can now upload your files in this container.

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)
Download and read the files from Azure Blob Storage using Python
  • In line 1, we import the required package.
  • In lines 3 to 6, we define the storage account URL, its access key, the container name where our files are stored, and the blob namethe file name we want to read from Azure Blob Storage.
  • In line 8, we create an instance of BlobServiceClient() class by passing the storage account URL and the access key. This will establish a connection to our storage account.
  • In line 11, we use the get_blob_client() function to connect to our blob file. We then pass the container name and the blob name to this function.
  • Finally, in lines 14 and 15, we download the blob data, read the data completely, and then print it, assuming that we have a text file in our blob storage.

By following these steps, you can use Python to easily read and download files from Azure Blob Storage.

RELATED TAGS

communitycreator
azure
python
RELATED COURSES

View all Courses

Keep Exploring