Azure Blob Storage, a Microsoft object storage solution, provides optimized cloud storage for storing unstructured data. Unstructured data could be text, images, or binary data. Blob Storage is ideal for storing files directly in a web browser. Additionally, it can also be used to store data for backup, archiving, and data analysis. Azure Blob Storage also helps in creating powerful cloud-based apps and data lakes for analytical needs.
This article will explain how to create Azure Blob Storage using Python. It will also elaborate on how to read the stored data, update the stored data, and delete it. In this guide, we will utilize the Python Pillow library to create an image blob.
Prerequisites:
For this tutorial, the Python 3.5.x version would work. Also, make sure that Azure Subscription is active with Azure Storage Account. You can create an Azure Storage Account for free.
Steps Involved:
1. Create Container in Azure Storage:
A single Azure Storage account can have many containers, and containers can ultimately be used to store blobs. To create a container in Azure Storage account, click Containers under Data Storage from the menu bar. Next, click on the + icon to create a container. Assign it a name of your choice.
2. Get the Connection String:
It is crucial to get the connection string for the Azure Storage Container to communicate and interact with it. To get the connection string, click on the Security tab present on the navigation bar. Under the Security tab, click Access Keys. In the Access Keys section, copy the connection string.
After copying the connection string value, set it as a PATH, an environment variable, on the command prompt. To perform this step, run the code given below:
setx AZURE_STORAGE_CONNECTION_STRING "Your_Connection_String"
3. Install the Required Packages:
Thirdly, install the Azure Storage Blobs client library for Python using the pip command on the command prompt. Make sure to install pip before executing this command. Otherwise, the installation process will not be complete.
pip install azure-storage-blob
4. Create the Image Resource:
In this step, create a sample image using Python’s Pillow Library. Create a circle inside the image canvas and save it as an image. For this step, execute the following code:
from PIL import Image, ImageDraw
image = Image.new('RGBA', (200, 200))
draw = ImageDraw.Draw(image)
draw.ellipse((20, 20, 180, 180), fill = 'black', outline ='black')
draw.point((100, 100), 'black')
image.save('test.png')
Before executing this code, make sure to install the Pillow library using the command given below:
pip install pillow
5. Create Blob in Azure Storage Account:
The next step is to set up a client to communicate with the Azure Container. Since the Azure Storage Blob library enables users to create three kinds of storage resources: the storage account, the container, and the blob, a client is required to interact with the resources. The first step is to get the connection string from the PATH. Then, use that connection string to set up the Blob Service Client object.
import os
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
Now, the next step is to create the Blob Service Client object as follows:
from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
Now, go ahead and set the path for the local image file which was created in Step 4.
local_file_name = "test.png"
local_path = "/content"
upload_file_path = os.path.join(local_path, local_file_name)
After that, create a blob client. Here, it’s feasible to provide the same name for the blob as the local filename for convenience.
blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)
Lastly, create the blob on the Azure Storage account.
with open(upload_file_path, "rb") as data:
blob_client.upload_blob(data)
Finally, you have created a blob in your container!
6. Read Blobs from Azure Storage Account:
Azure Blob Service Object can also be used to read the blobs from the container. For this step, execute the following code:
print("\nListing blobs...")
# Read all the blobs in the container
blob_list = container_client.list_blobs()
for blob in blob_list:
print("\t" + blob.name)
7. Update the Blob on Azure Storage:
Update operations can be executed in a similar manner as elaborated above. For instance, you want to update the color of the existing image and overwrite it on the blob. In this case, you will use the upload_blob() function and pass TRUE in the overwrite parameter.
For example:
with open(upload_file_path, "rb") as data:
blob_client.upload_blob(data, overwrite=True)
Hence, the existing image blob will be overwritten with the new blob image.
8. Delete the Blob from the Azure Storage Account:
It is possible to delete the existing blobs from the container in your Azure Storage Account using the Blob Service Object as shown below:
container_client.delete_container()
After executing the command, head over to Azure Portal to confirm that the operation is successful.
Conclusion:
In conclusion, the Azure Storage account provides functionality to perform CRUD operations not only through its portal or Powershell but also using Python. This article has covered the basic create, read, update and delete operations on blob storage using Azure Storage functionality. We hope this guide will assist you in setting up your blob storage and performing CRUD operations using Python successfully.
Happy coding and don't forget to try something new every day :)