将视频文件上传到NODEJS中的AZURE BLOB STORAGE

时间:2017-06-21 08:26:32

标签: node.js azure blob

我是将文件上传到blob存储的新手,我需要帮助将视频文件上传到nodejs中的azure blob存储。我为这项服务做了一些代码。

这是我的服务代码段

 uploadCourseVideo.post(multipartMiddleware, function (req, res) {
    var dataStream;
    console.log(req.body, req.files);
fs.readFile(req.files.file.path, function (err, dataStream) {
            var blobSvc = azure.createBlobService('ariasuniversity', 'account key');
            blobSvc.createContainerIfNotExists('elmsvideos', function (error, result, response) {
                if (!error) {
                    // Container exists and is private
                    blobSvc.createBlockBlobFromStream('elmsvideos', 'myblob', dataStream, dataStream.length, function (error, result, response) {
                        if (!error) {
                            // file uploaded
                        }
                    });
                }
            });
});`

我得到的错误是Stream.pause()不是函数。THe Error Image

请帮帮我。感谢

1 个答案:

答案 0 :(得分:1)

您使用了fs.readfile()函数,该函数不会返回流,从而引发您的问题。 您可以使用fs.createReadStream()函数,然后可以使用createWriteStreamToBlockBlob提供一个流来写入块blob。

var readStream = fs.createReadStream(req.files.file.path);

var blobSvc = azure.createBlobService('ariasuniversity', 'account key');
blobSvc.createContainerIfNotExists('elmsvideos', function (error, result, response) {
    if (!error) {
        // Container exists and is private
        readStream.pipe(blobSvc.createWriteStreamToBlockBlob('elmsvideos', 'myblob', function (error, result, response) {
            if(!error) {
                // file uploaded
            }
        }));

    }
}); 
相关问题