Unlocking the Power of Azure Blob Storage and FastAPI
Azure Blob Storage is a cloud-based object storage solution provided by Microsoft Azure. In today’s era, API integration services play an important role in enabling seamless communication between different applications and systems. One of the powerful tools for building APIs is FastAPI, a high-performance web framework for building APIs with Python. FastAPI becomes even more versatile when combined with Azure Blob (a scalable cloud storage solution provided by Microsoft Azure). It allows developers to integrate third-party APIs efficiently. It allows you to store and manage massive amounts of unstructured data, such as text files, images, videos, and more Azure Blob storage offers high availability, durability, and scalability, making it suitable for a wide range of applications.
Key Features of Azure Blob storage:
1. Blob Types: Azure Blob Storage offers three types of blobs: block blobs, append blobs, and page blobs. Block blobs are ideal for storing large files, append blobs are optimized for append operations, and page blobs are used for random read and write operations.
2. Data Security: Azure Blob Storage provides various security features, including encryption at rest and in transit, role-based access control (RBAC), and shared access signatures (SAS) for granular access control.
3. Scalability and Performance: Azure Blob Storage scales seamlessly to accommodate growing data volumes and high-traffic workloads. It provides high throughput and low latency for efficient data access.
4. Lifecycle Management: You can define lifecycle policies to automatically transition or delete blobs based on their age or other criteria. This helps optimize storage costs and data management.
Disadvantages of using Azure Blob:
1. Integrating Azure Blob Storage with FastAPI introduces an additional layer of complexity to your application architecture. You must handle authentication, storage account management, and the interaction with the Azure Blob storage API.
2. If you want to switch to a different storage provider, you may need to rewrite parts of your code that interact directly with Azure Blob storage.
3. When using Azure Blob storage, FastAPI application depends on network connectivity to interact with the storage service. Any network interruptions or latency issues can impact the performance and availability of application.
4. Azure Blob storage comes with its pricing structure, which includes costs based on storage capacity, data egress, and transaction operations.
Before getting started, you should have the following:
1. An Azure account with an active subscription.
2. A storage account in your Azure account.
3. The Azure Blob Storage SDK for Python was installed.
4. FastAPI and its dependencies installed.
1. Create an Azure Storage Account:
The first step is to create an Azure Storage Account. You can create it by following the instructions provided in the official Azure documentation.
2. Install Required Libraries:
Next, we need to install the required libraries. We can install the Azure Blob Storage SDK for Python and FastAPI using pip.
3. Configure the Azure Storage Account:
We need to configure the connection string to interact with Azure Blob Storage. The connection string contains the account name, account key, and endpoint URL of the storage account.
You can get the connection string from the Azure portal by navigating to your storage account and clicking the “Access keys” tab. Copy the connection string and paste it into the code.
4. Create a Container:
A container is a logical grouping of blobs. We can create a container using the BlobServiceClient object.
5. Create the FastAPI API:
Now, we can create the API using FastAPI. We will create two endpoints: one for uploading documents and another for downloading documents.
In the above code, the create_upload_file function handles the file upload and saves the file in the Azure Blob Storage container. The read_upload_file function handles the file download and returns the file data.
Difference between protected and unprotected presigned URL
A presigned URL is a URL that provides temporary access to an object in Azure Blob Storage. This URL can be used to allow a third-party to access the object without requiring them to have an Azure account.
A protected presigned URL is a URL that requires authentication to access the object. This URL is typically used when you want to grant access to an object to a specific user or set of users and you want to ensure that only those users can access the object.
An unprotected presigned URL, on the other hand, is a URL that does not require authentication to access the object. This URL is typically used when you want to provide temporary access to an object to many users or when you want to provide access to the object to users who do not have an Azure account.
Now, we can create the API using FastAPI. We will create two endpoints: one for generating a protected presigned URL and another for generating an unprotected presigned URL.
Create protected presigned URL to download a blob:
Create unprotected presigned URL to download a blob:
Delete a document from azure blob
In this blog, we discussed how to upload, download and delete documents from Azure Blob Storage using FastAPI. We used the Azure Blob Storage SDK for Python to interact with Azure Blob Storage and FastAPI to create the API.
A protected presigned URL is more secure and is typically used when you want to restrict access to an object to a specific set of users, In contrast, an unprotected presigned URL is less secure but can be used to provide temporary access to an object to a large number of users or to users who do not have an Azure account.
Here are some references that can help you with FastAPI integration with Azure Blob storage: