0

Currently I am able to upload files to blob storage by saving the file locally. But I want to stream the file directly to storage. as I am completely new to filesystem I am not able to do it using createReadStrem(filepath) as it is expecting file path and on search I got to see that its not possible to get the path from front end. I was dividing the file in to chunks and writing to stream. if anyone can help me with any way to do it?

Headers:
X-Chunk-Id: 0
X-Content-Id: a997d61859d6d3814d5f3f4a..
x-Content-Length: 19

payload:
fdfdfgytuujftyeeyjh    // hi.txt dummyfiledata it can be zip file also

API

const fs = require("fs");
function uploadToBlob(request, response) {
const chunkComplete = file.pushChunk(chunkId, chunk, chunkSize);
const size = file.getChunkLength(chunkId);
if (file.isCompleted()) {
var userrole = file.userrole;
var projectId = file.id;
var projectname = file.path;
var fileName = file.name;
var blobName = projectname + '/' + file.name;
var sourceFilePath = __dirname + '/uploads/' + file.name;
const fstream = fs.createReadStrem(sourceFilePath);
let chunks = file.getContent(); 
for (let j = 0; j < chunks.length; j++)
{
fstream.write(chunks[j]);
}
fstream.end();
delete fileStorage[fileId];
fstream.on("finish", () => {
// bs.createBlockBlobFromLocalfile( file gets saved to blob storage)
//Instead i want to use  bs.createBlockBlobFromstream() here and stream directly to blob without saving locally
}
}
}

1 Answer 1

0

This blog shows you how to streaming files to Azure Blob Storage with Node.js.

app.post('/upload', function (req, res) {
    var blobService = azure.createBlobService();
    var form = new multiparty.Form();

    form.on('part', function(part) {
        if (part.filename) {
            var filename = part.filename;
            var size = part.byteCount;

            var onError = function(error) {
                if (error) {
                    res.send({ grrr: error });
                }
            };
            blobService.createBlockBlobFromStream('container', filename, part, size, onError);
        } else {
            form.handlePart(part);
        }
    });

    form.parse(req);
    res.send("SWEET");
});

Convert buffer into stream with Readable method. like this:

const stream = Readable.from(buffer); 
blobService.createBlockBlobFromStream('container-name', 'filename', stream, buffer.byteLength, function(error, response) {         
    if (error) {             
        console.log('Error!');             
        console.log(error);         
    } else {             
        console.log('Blob uploaded successfully!');             
        console.log(response);         
    }     
});
4
  • I have the data in buffer so i want to convert it into stream and pass the arg to method.here is the code m trying var myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({ // frequency: 10, chunkSize: size }); myReadableStreamBuffer.put(c); blobService.createBlockBlobFromStream(containerName, blobName, myReadableStreamBuffer,10, err => {
    – s d
    Nov 11, 2020 at 8:02
  • error: 'Required argument callback for function _createBlobFromStream is not defined''ArgumentNullError'
    – s d
    Nov 11, 2020 at 8:04
  • Please try to convert buffer into stream with Readable method. like this: const stream = Readable.from(buffer); blobService.createBlockBlobFromStream('container-name', 'filename', stream, buffer.byteLength, function(error, response) { if (error) { console.log('Error!'); console.log(error); } else { console.log('Blob uploaded successfully!'); console.log(response); } });
    – unknown
    Nov 11, 2020 at 8:30
  • I edit my reply with this error, you could accept it, thank you.
    – unknown
    Nov 12, 2020 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.