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

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

问题描述

 使用Express with Node,我可以成功上传文件,并将其传递到Azure存储。 > app.get('/ upload',function(req,res){
res.send(
'< form action =/ uploadmethod =postenctype =multipart / form -data>'+
'< input type =filename =snapshot/>'+
'< input type =submitvalue =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。这似乎是一个低效率和不必要的步骤,我最终不得不管理清理临时文件目录。



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

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

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



vjacobs代码实际上是从另一个站点下载文件并传递给流到Azure,所以我不知道是否可以适应在我的情况下工作。



如何访问和传递上传的文件流直接到Azure使用节点?

解决方案

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



如果我们禁用了Express的bodyparser()中间件,请使用Multiparty(npm install multiparty)(一个Formidable的分支),可以访问multipart数据(有关更多信息,请参阅他们关于此操作的注释)。不同于Formidable,多方不会将文件流传输到磁盘,除非你告诉它。

  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帮助找到解决方案。


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

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.

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

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 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.

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

解决方案

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.

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

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