How to parse Multi-part form data in an Azure Func

2019-04-10 15:40发布

问题:

I want to write a NodeJS HTTP endpoint using Azure Functions.

This endpoint will be a POST endpoint which takes files and upload these to blob storage.

However, NodeJS multipart form data parsers are all in the form of httpserver or expressJS middleware.

Is there any available tools that can parse the multipart form data after it has all been received from the Function Application's wrapper?

Thanks!

回答1:

As Azure Functions has wrapped http server object in Node.js, and exposes a simple req and context with several functionalities, refer to https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#exporting-a-function for details.

And mostly, Azure Functions is designed for triggers and webhooks requests, you can refer to https://docs.microsoft.com/en-us/azure/azure-functions/functions-compare-logic-apps-ms-flow-webjobs for the detailed comparison.

Meanwhile, you can try the answer of Image upload to server in node.js without using express to parse the request body content to file content, and upload to Azure Storage leveraging Azure Storage SDK for node.js, you can install custom node modules via KUDU console. Refer to https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#node-version--package-management for more info.

And I suggest you can try to leverage Azure API App in node.js to approach your requiremnet. As it is an expressjs based project, which will be more easier to handle upload files.

Any further concern, please feel free to let me know.



回答2:

You can try to use this adapter for functions and express, it may allow you to successfully use the multi-part middleware you want: https://github.com/yvele/azure-function-express

As a less desirable option, you can parse the body yourself, all the multi-part data will be available in req.body and will look something like this:

------WebKitFormBoundarymQMaH4AksAbC8HRW
Content-Disposition: form-data; name="key"

value
------WebKitFormBoundarymQMaH4AksAbC8HRW
Content-Disposition: form-data; name=""


------WebKitFormBoundarymQMaH4AksAbC8HRW--

I do think it's a good idea to support httpserver / express better in order to enable this extensibility.