0

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.

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
Shadiqur
  • 490
  • 1
  • 5
  • 18

1 Answers1

0

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.

You can use the below code to upload files from local to Azure blob storage using Python.

Code:

from azure.storage.blob import BlobServiceClient

def uploadfile():
    connectionstring="your connection string"
    containername="test"
    filename="sample.txt"
    local_file_path="C:\\Users\\saswithip.txt"
    directory_name="folder1"
    client=BlobServiceClient.from_connection_string(connectionstring)
    container_client=client.get_container_client(containername)
    blob_client = container_client.get_blob_client(directory_name + "/" + filename)
    with open(local_file_path, "r", encoding="utf-8") as data:
       contents = data.read().encode("utf-8")
       blob_client.upload_blob(contents, overwrite=True)
       print("The file is uploaded successfully!!!")
uploadfile()

Output:

The file is uploaded successfully!!!

enter image description here

Portal: enter image description here

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
  • my File is pipe seperated txt file and its size is more than 3 mb . i used the encoding but didn't work gives azure.core.exceptions.ServiceResponseError: ('Connection aborted.', TimeoutError('The write operation timed out')) – Shadiqur Apr 24 '23 at 06:35
  • @Shadiqur, try using this blob_client.upload_blob(contents, overwrite=True, blob_type="BlockBlob", timeout=120). – Venkatesan Apr 24 '23 at 12:12
  • that also not working . i works fine for small file size. i want to upload a large file like 1 gig with low internate speed . i found this issue on git but couldn't find a working solution https://github.com/Azure/azure-sdk-for-python/issues/12166 – Shadiqur Apr 25 '23 at 14:51
  • @Shadiqur, please refer this https://stackoverflow.com/questions/75032295/slow-upload-to-azure-blob-storage-with-python/75039295#75039295 – Venkatesan Apr 26 '23 at 05:20