I'm currently working on a project where I need to upload a local text file to an Azure Blob using Python. I wrote the following code to accomplish this:
def upload_blob(self, file_name, local_file_path):
file_name = 'upload_test.txt'
blob_service_client = BlobServiceClient.from_connection_string(self.storage_connection_str,timeout=120)
container_client = blob_service_client.get_container_client(self.storage_container_name)
# Define the directory path and filename
directory_name = self.blob_prefix
# Create a BlobClient object for the file
blob_client = container_client.get_blob_client(directory_name + "/" + file_name)
# Upload the file to the specified directory
with open(local_file_path, "rb") as data:
blob_client.upload_blob(data)
However, I've been running into an error where the code works fine for local variables in byte format, but encounters problems when trying to upload a local text file.
I'm fairly new to Python and Azure Blob, so I'm not quite sure how to fix this issue. Has anyone else encountered a similar problem and found a solution? Any help or advice would be greatly appreciated!
Thank you in advance.