Stream uploaded file to Azure blob storage with Node

后端 未结 4 1715
野的像风
野的像风 2020-12-13 21:18

Using Express with Node, I can upload a file successfully and pass it to Azure storage in the following block of code.

app.get(\'/upload\', function (req, re         


        
4条回答
  •  被撕碎了的回忆
    2020-12-13 21:52

    SOLUTION (based on discussion with @danielepolencic)

    Using Multiparty(npm install multiparty), a fork of Formidable, we can access the multipart data if we disable the bodyparser() middleware from Express (see their notes on doing this for more information). Unlike Formidable, Multiparty will not stream the file to disk unless you tell it to.

    app.post('/upload', function (req, res) {
        var blobService = azure.createBlobService();
        var form = new multiparty.Form();
        form.on('part', function(part) {
            if (part.filename) {
    
                var size = part.byteCount - part.byteOffset;
                var name = part.filename;
    
                blobService.createBlockBlobFromStream('c', name, part, size, function(error) {
                    if (error) {
                        res.send({ Grrr: error });
                    }
                });
            } else {
                form.handlePart(part);
            }
        });
        form.parse(req);
        res.send('OK');
    });
    

    Props to @danielepolencic for helping to find the solution to this.

提交回复
热议问题