ALI Sajjad Rizvi
ALI Sajjad Rizvi

Reputation: 185

Uploading Video File to AZURE BLOB STORAGE in NODEJS

I am new to uploading files to blob storage and I need help for uploading video file to azure blob storage in nodejs. I have done a bit of code for this service.

here is my service code snippet

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

The error which I am getting is of Stream.pause() is not a function.THe Error Image

Please Help me out. Thanks

Upvotes: 1

Views: 2044

Answers (1)

Aaron Chen
Aaron Chen

Reputation: 9950

You used fs.readfile() function which wouldn't return a stream, thus raising your issue. You could use fs.createReadStream() function instead, and then you can use createWriteStreamToBlockBlob to provide a stream to write to a block 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
            }
        }));

    }
}); 

Upvotes: 1

Related Questions