In Azure, you can store your data in various storage options provided by Azure like Blob, Table, CosmosDB, SQL-DB, etc. In this shot, we will learn how to append data to a text file stored in Azure Blob Storage.
“Azure Blob storage is Microsoft’s object storage solution for the cloud. Blob storage is optimized for storing 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.” -
Azure Blob Storage documentation https://learn.microsoft.com/en-us/azure/storage/blobs/
Let us first create an Azure Storage account. Follow the steps mentioned below:
The blob that is appended with data is called an append blob. An append blob is made up of blocks and is optimized for append operations. Each block or data that you want to append in the blob can be of a different size, up to a maximum of 4 MB, and an append blob can include up to 50,000 blocks. Therefore, the maximum size of an append blob is slightly more than 195 GB (4 MB X 50,000 blocks).
Now, we are ready to connect this storage account using Python. Let us first install the required packages. Run the following command:
pip install azure
Now, see the code to connect the Storage account with our Python script.
from azure.storage.blob import AppendBlobServicedef append_data_to_blob(data):service = AppendBlobService(account_name="<STORAGE_ACCOUNT_NAME>",account_key="<STORAGE_ACCOUNT_KEY>")try:service.append_blob_from_text(container_name="<CONTAINER_NAME>", blob_name="<FILE_NAME>", text = data)except:service.create_blob(container_name="<CONTAINER_NAME>", blob_name="<FILE_NAME>")service.append_blob_from_text(container_name="<CONTAINER_NAME>", blob_name="<FILE_NAME>", text = data)print('Data Appended to Blob Successfully.')append_data_to_blob('Hello Azure Blob Storage!')
append_data_to_blob()
that accepts the data we need to append to our blob.<STORAGE_ACCOUNT_NAME>
and <STORAGE_ACCOUNT_KEY>
, which we have already saved after the creation of our storage account.data
to a file <FILE_NAME>
that is present inside our blob container <CONTAINER_NAME>
. However, we haven’t created any containers yet; so, we need to keep this statement in a try-except
block.create_blob()
function and passing the <CONTAINER_NAME>
and <FILE_NAME>
. You can specify any file name. For example, if you want to append your data
to the log.txt
file; then, you need to replace <FILE_NAME>
with log.txt
. The <CONTAINER_NAME>
could be any name unique to your storage account.data
to the file <FILE_NAME>
.data
.So, in this way, we can append data generated at regular intervals and store them in the blob storage.