11
Jul

Unlocking the Power of Azure Blob Storage and FastAPI

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 efficiently use API integration services with third-party APIs. This way, you can 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 an application.
4. Azure Blob storage comes with its pricing structure, which includes costs based on storage capacity, data egress, and transaction operations.

Prerequisites:

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.

Steps:

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.

install the Azure

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.

Configure the Azure Storage Account

4. Create a Container:

A container is a logical grouping of blobs. We can create a container using the BlobServiceClient object.

create a container using the BlobServiceClient

5. Create the FastAPI API:

Implementing API integration services, we can create API using FastAPI. For this, we will create two endpoints: one for uploading documents and another for downloading documents.

Create the FastAPI

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 protected presigned URL to download a blob

Create unprotected presigned URL to download a blob:

Create unprotected presigned URL to download a blob

Delete a document from azure blob

Delete a document from azure blob

Conclusion:

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:

1. Azure Blob Storage SDK for Python.
2. Azure Storage Blob library for Python.
3. FastAPI official extensions.
4. Example projects and tutorials.

share

Everything You Need to Know About FastAPI Security With JWT

Everything You Need to Know About FastAPI Security With JWT

previous-blog-arrowPrevious
How Can a Bespoke Software Development Company Empower Your Business?

How Can a Bespoke Software Development Company Empower Your Business?

next-blog-arrowNext