Python Code upload csv(df) back to azure blob

Adrian 181 Reputation points
2021-06-03T12:18:46.057+00:00

Hey

I've got some code that downloads a blob as xlsx from azure blob storage, it loads it into a data frame and runs some transformation on it.

I would now like to save this df back into azure blob as a csv, preferably replacing the original file. This is the code I currently have. I am able to download the blob, run the transformations and save it locally, I would like to now push it to blob and preferable replace the original one.

Does anyone know of an efficient way of doing so? I've been having some troubles incorporating the information I found online into my code.

Thanks!

import pandas as pd
import numpy as np
import datetime
import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__

def function (df):
    (df operations)
    a = df.to_csv('current19.csv', index=False)


CONNECTION_STRING = ""
CONTAINERNAME = ""
BLOBNAME = ""
LOCALFILENAME = "" 

blob_service_client = BlobServiceClient.from_connection_string(CONNECTION_STRING) #instantiate new blobservice with connection string
container_client = blob_service_client.get_container_client(CONTAINERNAME) #instantiate new containerclient
blob_client = blob_service_client.get_blob_client(container = CONTAINERNAME, blob=BLOBNAME)

#READ PRODUCTS FILE
f = open(LOCALFILENAME, "wb")
f.write(blob_client.download_blob().content_as_bytes())
f.close()
df = pd.read_excel(r''+LOCALFILENAME)
#print(df)


function(df)

This currently download blob and saves it locally, I need to add a few lines so it pushed it back to azure blob and overrides the original file

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,542 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.