You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Blob存储上传下载操作耗时过长导致API执行效率低下的优化咨询

Azure Blob存储上传下载操作耗时过长导致API执行效率低下的优化咨询

我现在在做一个功能,需要在Azure Blob存储上创建PDF文件,并且从Blob存储读取文件,目前的实现代码如下:

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和download_blob这两个操作每次执行都要花2秒左右,直接拉高了我们API的整体响应时间。

我已经尝试了几种优化思路,但都没什么效果:

  • 改成异步实现,耗时并没有减少
  • 用functools做缓存,也没起到预期的加速作用
  • 考虑过用Redis缓存,但这需要额外部署新的服务器,暂时不想采用这种方案

想请教下有没有其他不需要额外依赖、能有效降低Blob存储操作耗时的优化方法?

备注:内容来源于stack exchange,提问作者MTRS Academy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 10:38:04