流上传的文件天青Blob存储与节点 [英] Stream uploaded file to Azure blob storage with Node

查看:207
本文介绍了流上传的文件天青Blob存储与节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用前preSS与节点,我可以成功上传文件并将其传递到Azure存储在code以下块。

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

这工作得很好,但前preSS创建一个临时文件和第一家门店的图像,然后我把它上传从文件到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存储,但我不与节点或Ex $不够熟悉p $ PSS了解如何访问数据流。

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

我发现下面的博客这表明有可能是一个办法这样做,肯定防爆preSS被抓住了流数据和解析,并保存到文件系统也是如此。的http://blog.valeryjacobs.com/index.php/streaming-media-from-url-to-blob-storage/

vjacobs code实际上是从下载其他网站上的文件,并通过该流天青,所以我不知道这是否可以适应在我的处境工作。

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.

如何访问并通过上传的文件使用节点直接传输到Azure的?

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

推荐答案

SOLUTION (基于与@danielepolencic讨论)

SOLUTION (based on discussion with @danielepolencic)

使用多方(NPM安装多方)的厉害叉子,我们可以访问多部分数据,如果我们从禁用前preSS的bodyparser()中间件(见这样做的更多信息的注释)。不像厉害,多方不会对文件传输到磁盘,除非你告诉它。

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.

这篇关于流上传的文件天青Blob存储与节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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