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?
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:
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.