Hi,
In your code, you're trying to output CSV files to Azure Blob Storage using an Azure Function. However, there are a couple of issues:
- The
outputblob
parameter in your function is of typefunc.Out[str]
, which expects a string as input. You're trying to set it with the output ofdf.to_csv()
, which returnsNone
. Instead, you should write the CSV data to a file and then upload that file to blob storage. - You're using a local file path (
/tmp/
) which won't work in Azure Functions as they are serverless and have limited access to the file system.
import logging
import azure.functions as func
import pandas as pd
import random
import os
@app.blob_trigger(name="myblob", path="old/{name}", connection="Storage")
@app.blob_output(name="outputblob", path="new/{name}", connection="Storage")
def BlobTrigger(myblob: func.InputStream, outputblob: func.Out[str]):
results = random.sample(range(1, 500), 7)
step = 3 # Adjust step size as needed
for i in range(0, len(results), step):
results_chunked = results[i:i+step]
df = pd.DataFrame(results_chunked)
csv_data = df.to_csv(index=False)
# Generate a unique file name
file_name = str(i) + '.csv'
# Write CSV data to a temporary file
temp_file_path = os.path.join(os.environ['TMP'], file_name)
with open(temp_file_path, 'w') as temp_file:
temp_file.write(csv_data)
# Upload the temporary file to blob storage
outputblob.set(open(temp_file_path, 'rb'))
# Delete the temporary file
os.remove(temp_file_path)
This code generates CSV files in chunks and uploads them to Azure Blob Storage using the Azure Function's outputblob
parameter. Make sure to adjust the step
variable according to your chunk size requirements.