raina
raina

Reputation: 92

Use Azure Function to Export CSV

I was trying to write an azure function that access azure blob storage, and use the generated url. So that once the user put this url, it will download a csv file from azure Blob Storage to the caller's local PC. From the azure function I wrote so far it is able to access the data on the blob storage. Does anyone know how to have it export csv to the calling user?

Upvotes: 0

Views: 10783

Answers (2)

Turbot
Turbot

Reputation: 5225

this is quick conversion that might help for you

import azure.functions as func

async def main(req: func.HttpRequest) -> func.HttpResponse:
    // access azure blog storage and get the csv read as string
    filebytes= bytes(csv, 'utf-8')

    return func.HttpResponse(body=filebytes, status_code=200, mimetype='application/octet-stream')

Upvotes: 2

Turbot
Turbot

Reputation: 5225

you need to create FileContentResult

public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, ILogger log)
{
    string csv;

    //Access azure blog storage and create in csv content

    byte[] filebytes = Encoding.UTF8.GetBytes(csv);

    return new FileContentResult(filebytes, "application/octet-stream") {
        FileDownloadName = "Export.csv"
    };
}

Upvotes: 3

Related Questions