使用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传输后端。
修正步骤
正确导入
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')确保安装Azure扩展依赖
执行以下命令安装smart_open的Azure支持包(若未安装):pip install smart_open[azure] --upgrade
验证
运行修正后的代码即可正常写入Azure Blob Storage文件,原有读取逻辑无需改动可继续使用。
内容的提问来源于stack exchange,提问作者PC_User
相关产品推荐
相关产品推荐

