使用 Azure 函数导出 CSV

Use Azure Function to Export CSV

我试图编写一个访问 azure blob 存储的 azure 函数,并使用生成的 url。因此,一旦用户输入 url,它就会从 azure Blob Storage 下载一个 csv 文件到调用者的本地 PC。 从到目前为止我编写的 azure 函数,它能够访问 blob 存储上的数据。有谁知道如何让它导出 csv 给调用用户?

您需要创建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"
    };
}

这很快conversion可能对你有帮助

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')