This project demonstrates an Azure Function that handles file uploads and processes images using the Azure Computer Vision API. The function parses multipart form-data and analyzes the uploaded images to identify car types.
- Node.js (LTS version recommended)
- Azure Functions Core Tools
- An Azure account with an active subscription
- Azure Computer Vision API endpoint and key
- Clone the repository:
git clone https://github.com/
cd azure-vision-api
- Install dependencies:
npm install
- Setup environment variables:
Create a .env
file in the root of the project and add your Azure Computer Vision API endpoint and key:
AZURE_COMPUTER_VISION_ENDPOINT=https://<your-endpoint>.cognitiveservices.azure.com/
AZURE_COMPUTER_VISION_KEY=<your-computer-vision-key>
NODE_ENV=development
─ .funcignore
─ .gitignore
─ host.json
─ local.settings.json
─ package-lock.json
─ package.json
─ README.md
─ src
└── functions
├── helpers.js
├── index.js
└── visionAPIHandler.js
- Start the Azure Function:
func start
- Test the function
- Use Postman to easily test the file upload.
- Make sure you've selected
form-data
in theBody
and have added the key asimage
of typeFile
and value as the image to be uploaded.
- Make sure you've selected
Many Node.js/Express libraries like multer
are designed to work with standard Node.js HTTP servers, where they expect the request object to be an instance of http.IncomingMessageHowever
. However, cloud functions such as Azure Functions wrap the HTTP request object and expose a simplified version, often referred to as HttpRequest. This object includes properties like query and params but does not include the raw stream that certain libraries, such as multer, require to parse multipart form-data. (Refer https://stackoverflow.com/a/40717096/5544970).
Because of this, multer and similar middleware cannot be directly used with Azure Functions. For instance, the multiparty package relies on the .on event which expects the raw stream, but this stream is not provided by Azure Functions.
To handle multipart form-data in cloud functions, you can manually parse the data. This involves reading the body of the request and parsing it yourself. While more complex, this method accommodates the limitations of the cloud function's request object. The process involves two main steps:
- Accumulate Request Data: Collect the data chunks from the request into a buffer.
- Parse the Buffer: Use a package like busboy to parse the buffered data manually.
By manually handling the request data and parsing it, you can work around the limitations imposed by cloud functions and effectively manage multipart form-data uploads.