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

使用smart_open写入Azure Blob Storage时遇NotImplementedError问题

问题分析与解决方案

错误原因

  • 代码未正确导入BlobServiceClient类:仅导入azure.storage.blob模块,但未直接引入BlobServiceClient,导致创建的客户端对象未被smart_open识别为Azure专用客户端,进而 fallback 到HTTP协议处理逻辑——而HTTP协议不支持写入(wb模式)。
  • 读取操作正常是因为HTTP协议支持只读访问,但写入需要Azure Blob Storage的专用客户端驱动。
  • 额外可能原因:若未安装smart_open的Azure扩展依赖,也会导致无法启用Azure传输后端。

修正步骤

  1. 正确导入BlobServiceClient
    修改导入语句,直接引入目标类:

    import os
    from smart_open import open
    from azure.storage.blob import BlobServiceClient
    
    connect_str = os.environ['AZURE_STORAGE_CONNECTION_STRING']
    transport_params = {
        'client': BlobServiceClient.from_connection_string(connect_str),
    }
    with open('azure://mycontainer/my_file.txt', 'wb', transport_params=transport_params) as fout:
        fout.write(b'hello world')
    
  2. 确保安装Azure扩展依赖
    执行以下命令安装smart_open的Azure支持包(若未安装):

    pip install smart_open[azure] --upgrade
    

验证

运行修正后的代码即可正常写入Azure Blob Storage文件,原有读取逻辑无需改动可继续使用。

内容的提问来源于stack exchange,提问作者PC_User

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 03:27:09