MTRS Academy
MTRS Academy

Reputation: 1

upload_blob and download_blob takes 2 seconds to execute which increases execution time of our API

This is how we are creating a pdf on azure blob storage and reading the file from Azure blob storage

import logging
import os
from azure.storage.blob import ContainerClient
from starlette.config import Config
from datetime import timedelta
from datetime import datetime
import pytz

class azure_storage_account:
    def __init__(self, azure_storage_connection_string="",container_name =""):
        dir_path = os.getcwd()
        config = Config(dir_path + os.sep + '.env')
        if azure_storage_connection_string == "":
            self.azure_storage_connection_string = config('AZURE_STORAGE_CONNECTION_STRING')
        else:
            self.azure_storage_connection_string = azure_storage_connection_string

        if container_name == "":
            self.container_name = config('AZURE_API_FILE_CONTAINER')
        else:
            self.container_name = container_name
        
        self.container_client = ContainerClient.from_connection_string(conn_str=self.azure_storage_connection_string,container_name=self.container_name)


    def create_file(self,file_path,data=""):
        try:
            blob_client = self.container_client.get_blob_client(file_path)
            blob_client.upload_blob(data,overwrite=True)
            return True
        
        except Exception as e:
            logging.error(e)
            return False


    def read_file(self,file_path):
        try:
            file_stream =  self.container_client.download_blob(file_path)
            file_content = file_stream.readall()
            return file_content
        
        except Exception as e:
            logging.error(e)
            return False

upload_blob and download_blob takes 2 seconds to execute which increases execution time of our API

i tried async but it didnt reduced time. func tools caching also didnt helped

for redis caching we would need a new server

Upvotes: 0

Views: 53

Answers (1)

Dasari Kamali
Dasari Kamali

Reputation: 3649

A 2-second delay is might be due to the network and storage setup, which affects data transfer speed. I have used max_concurrency for the given code for faster multi-threaded uploads and downloads tasks.

main.py:

import time
from storage import AzureStorage

storage = AzureStorage()
local_file_path = "C:/KamSTO/xxxxxxxx/basic-text.pdf"  
blob_file_path = "uploaded-file.pdf"  

with open(local_file_path, "rb") as file:
    file_data = file.read()

start_time = time.time()
upload_status = storage.upload_file(blob_file_path, file_data)
upload_time = time.time() - start_time
print(f"Upload Success: {upload_status}, Time Taken: {upload_time:.2f} seconds")

start_time = time.time()
downloaded_data = storage.download_file(blob_file_path)
download_time = time.time() - start_time

if downloaded_data:
    with open("downloaded-file.pdf", "wb") as file:
        file.write(downloaded_data)
    print(f"Downloaded File Saved as: downloaded-file.pdf, Time Taken: {download_time:.2f} seconds")
else:
    print("Failed to download file")
  • I used BlobServiceClient to manage connections more efficiently, reducing resource usage and improving performance.

storage.py :

import logging
from azure.storage.blob import BlobServiceClient
from config import AZURE_STORAGE_CONNECTION_STRING, AZURE_STORAGE_CONTAINER_NAME

class AzureStorage:
    def __init__(self):
        try:
            self.blob_service_client = BlobServiceClient.from_connection_string(AZURE_STORAGE_CONNECTION_STRING)
            self.container_client = self.blob_service_client.get_container_client(AZURE_STORAGE_CONTAINER_NAME)
        except Exception as e:
            logging.error(f"Failed to connect to Azure Blob Storage: {e}")

    def upload_file(self, file_path, data):
        """Upload a file to Azure Blob Storage."""
        try:
            blob_client = self.container_client.get_blob_client(file_path)
            blob_client.upload_blob(data, overwrite=True, max_concurrency=5) 
            return True
        except Exception as e:
            logging.error(f"Upload failed: {e}")
            return False

    def download_file(self, file_path):
        """Download a file from Azure Blob Storage."""
        try:
            blob_client = self.container_client.get_blob_client(file_path)
            file_stream = blob_client.download_blob(max_concurrency=5)
            file_content = file_stream.readall()  
            return file_content
        except Exception as e:
            logging.error(f"Download failed: {e}")
            return False

config.py :

import os
from dotenv import load_dotenv
load_dotenv()

AZURE_STORAGE_CONNECTION_STRING = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
AZURE_STORAGE_CONTAINER_NAME = os.getenv("AZURE_STORAGE_CONTAINER_NAME")

.env :

AZURE_STORAGE_CONNECTION_STRING=<storageConneString>
AZURE_STORAGE_CONTAINER_NAME=<containerName>

Output :

You can see the total runtime for the task was 1.73 sec.

enter image description here

Dowloaded in the local project path:

enter image description here

Blob uploaded to the Azure blob storage.

enter image description here

Upvotes: 0

Related Questions