使用 Node 将上传的文件流式传输到 Azure blob 存储 [英] Stream uploaded file to Azure blob storage with Node

查看:17
本文介绍了使用 Node 将上传的文件流式传输到 Azure blob 存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Node 中使用 Express,我可以成功上传文件并将其通过以下代码块传递到 Azure 存储.

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, res) {
    res.send(
    '<form action="/upload" method="post" enctype="multipart/form-data">' +
    '<input type="file" name="snapshot" />' +
    '<input type="submit" value="Upload" />' +
    '</form>'
    );
});

app.post('/upload', function (req, res) {
    var path = req.files.snapshot.path;
    var bs= azure.createBlobService();
    bs.createBlockBlobFromFile('c', 'test.png', path, function (error) { });
    res.send("OK");
});

这工作得很好,但 Express 创建了一个临时文件并首先存储图像,然后我将它从文件上传到 Azure.这在过程中似乎是一个低效且不必要的步骤,我最终不得不管理临时文件目录的清理工作.

This works just fine, but Express creates a temporary file and stores the image first, then I upload it to Azure from the file. This seems like an inefficient and unnecessary step in the process and I end up having to manage cleanup of the temp file directory.

我应该能够使用 Azure SDK 中的 blobService.createBlockBlobFromStream 方法将文件直接流式传输到 Azure 存储,但我对 Node 或 Express 不够熟悉,无法理解如何访问流数据.

I should be able to stream the file directly to Azure storage using the blobService.createBlockBlobFromStream method in the Azure SDK, but I am not familiar enough with Node or Express to understand how to access the stream data.

app.post('/upload', function (req, res) {

    var stream = /// WHAT GOES HERE ?? ///

    var bs= azure.createBlobService();
    bs.createBlockBlobFromStream('c', 'test.png', stream, function (error) { });
    res.send("OK");
});

我发现以下博客表明可能有一种方法可以做到这一点,当然 Express 也在抓取流数据并将其解析并保存到文件系统中.http://blog.valeryjacobs.com/index.php/streaming-media-from-url-to-blob-storage/

I have found the following blog which indicates that there may be a way to do so, and certainly Express is grabbing the stream data and parsing and saving it to the file system as well. http://blog.valeryjacobs.com/index.php/streaming-media-from-url-to-blob-storage/

vjacobs 代码实际上是从另一个站点下载文件并将该流传递到 Azure,所以我不确定它是否可以适应我的情况.

vjacobs code is actually downloading a file from another site and passing that stream to Azure, so I'm not sure if it can be adapted to work in my situation.

如何使用 Node 访问上传的文件流并将其直接传递到 Azure?

How can I access and pass the uploaded files stream directly to Azure using Node?

推荐答案

解决方案(基于与@danielepolencic 的讨论)

SOLUTION (based on discussion with @danielepolencic)

使用 Multiparty(npm install multiparty),Formidable 的一个分支,如果我们从 Express 禁用 bodyparser() 中间件,我们可以访问多部分数据(更多信息请参阅他们的注释).与 Formidable 不同,Multiparty 不会将文件流式传输到磁盘,除非您告诉它.

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

感谢@danielepolencic 帮助找到解决方案.

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

这篇关于使用 Node 将上传的文件流式传输到 Azure blob 存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆