Search code examples
pythonazureazure-blob-storage

Unable to Upload csv into Azure Blob Storage with LocationParseError label empty or too long


Trying to load a dataframe as a csv into azure blob storage but getting the below error. I have had a look online but can't see what I am doing wrong. Some advice would be much appreciated.

'DefaultEndpointsProtocol=https;AccountName=test;AccountKey=Test==;EndpointSuffix=core.windows.net', label empty or too long

from azure.storage.blob import ContainerClient
from azure.storage.blob import BlobServiceClient

client = "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=Test==;EndpointSuffix=core.windows.net"
container_name = "test"
blob_name = "testCsv.csv"

container = ContainerClient.from_connection_string(client, container_name)
blob_service_client = BlobServiceClient(client)

data = {
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}

df = pd.DataFrame(data)

csv_string = df.to_csv(encoding='utf-8', index=False, header=True)

csv_bytes = str.encode(csv_string)

blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
blob_client.upload_blob(csv_bytes, overwrite=True)

Solution

  • Silly mistake, I needed to feed the blob name I wanted to create into .get_blob_client. And I didn't need to create blob_service_client = BlobServiceClient(client)

    Amended code:

    blob_name = "testCsv.csv"
    blob_client = container.get_blob_client(blob_name)
    blob_client.upload_blob(csv_bytes, overwrite=True)