英文:
Python smart_open throws NotImplementedError with code from documentation
问题
我正在尝试使用 smart_open 模块将数据写入存储在 Azure Blob 存储中的 .txt 文件。然而,在运行文档中提供的示例代码时,我遇到了以下错误:
NotImplementedError: http support for mode 'wb' not implemented
以下是产生错误的代码:
import os
from smart_open import open
import azure.storage.blob
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')
我的代码几乎与示例代码相同,唯一的区别是用于访问 Blob 存储的连接字符串。奇怪的是,尝试从文件中读取数据时,一切都按预期工作。以下是我使用的可工作代码:
connect_str = os.environ['AZURE_STORAGE_CONNECTION_STRING']
transport_params = {
'client': BlobServiceClient.from_connection_string(connect_str),
}
for line in open('azure://mycontainer/my_file.txt', transport_params=transport_params):
print(line)
我正在使用 Python 3.9.12 和 smart_open 版本 6.3.0。尽管 smart_open 指示它支持模式 "wb",但它会引发错误。可能是什么原因导致了这个问题?
英文:
I am trying to write to a .txt file stored in Azure Blob Storage using the smart_open module. However, when running the example code provided in the documentation, I encounter the following error:
NotImplementedError: http support for mode 'wb' not implemented
Here is the code that produces the error:
import os
from smart_open import open
import azure.storage.blob
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')
My code is almost identical to the example code, with the only difference being the connection string used to access blob storage. Strangely, when trying to read from the file, everything works as expected. Here is the working code that I used:
connect_str = os.environ['AZURE_STORAGE_CONNECTION_STRING']
transport_params = {
'client': BlobServiceClient.from_connection_string(connect_str),
}
for line in open('azure://mycontainer/my_file.txt', transport_params=transport_params):
print(line)
I am using Python 3.9.12 and smart_open version 6.3.0. Even though smart_open indicates that it supports mode "wb", it throws an error. What could be causing this?
答案1
得分: 0
"http
" 协议不支持 'wb'
模式。
问题出在你在连接字符串中传递了 http
协议。
连接字符串:
DefaultEndpointsProtocol=https;AccountName=venkat123;AccountKey=xxxxxx;EndpointSuffix=core.windows.net
代码:
我尝试了相同的代码,但使用 https
协议来将数据写入 Azure Blob 存储中的 .txt 文件,使用 smart_open 模块执行成功。
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://test/test.txt', 'wb', transport_params=transport_params) as fout:
fout.write(b'hello world')
print("文件已上传并写入txt!!!")
输出:
文件已上传并写入txt!!!
Portal:
英文:
> http support for mode 'wb' not implemented
The problem is here you are passing http
protocol in your connection string.
Connection string:
DefaultEndpointsProtocol=https;AccountName=venkat123;AccountKey=xxxxxx;EndpointSuffix=core.windows.net
Code:
I tried with the same code with https
protocol to write to a .txt file stored in Azure Blob Storage using the smart_open module it executed successfully.
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://test/test.txt', 'wb', transport_params=transport_params) as fout:
fout.write(b'hello world')\
print("The file is uploaded with written txt!!!")
Output:
The file is uploaded with written txt!!!
Portal:
答案2
得分: 0
所以结果表明,错误是由我尝试访问文件的方式引起的。
最初,我复制了我的 Blob 文件属性中提供的 URL。然而,我需要按照文档和 Venkatesan 所说的方式访问我的文件。
所以这是正确的方式:
with open('azure://MY-CONTAINER/TEXT-FILE.txt', 'wb', transport_params=transport_params) as fout:
因为连接字符串已经包含了访问我的文件所需的所有详细信息。
尽管出于某种原因,使用我 Blob 文件属性提供的 URL 读取文件也可以正常工作。
英文:
So turns out, the error was caused by how I was trying to access my file.
Originally I was copying the URL provided in the properties of my blob file. However, I needed to access my file exactly as the documentation and Venkatesan said.
So this is the correct way:
with open('azure://MY-CONTAINER/TEXT-FILE.txt', 'wb', transport_params=transport_params) as fout:
Because the connection string already has all the details needed to access my file.
Although for some reason, reading the file with the url provided by the properties of my blob file, works fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论