0
votes

I would upload an audio file to azure blob storage. First I make a http request to url to get the file .

Then I would "directly" save it to azure blob storage without the need to store it in server then upload it.

Here's my code:

request
  .get(url, {
    auth: {
      bearer: token
    }
  })
  .on("response", async function(response) {
    const res = await blobService.createAppendBlobFromStream(
      "recordings", // container name
      "record.wav",
      response, // should be stream
      177777, // stream length
      function(err, res) {
        try {
          console.log(res);
        } catch (err) {
          console.log(err);
        }
      }
    );
  });

Actually when I upload a file in blob storage and check my database I get an empty file with no data inside, I think I'm not sending the data stream correcty.

What I expect is to get the audio file in blob storage with data inside that I get from the get request

I should also specify the stream length but I don't know how to get it I putted a random number but it should be the right stream length. I have checked response object but I havn't found that infomation.

1
What is happening vs. what are you expecting to happen? Any errors? - Lukas Knuth
You should be able to get the correct stream-length from the response if the server sends it with response.headers['content-length']. Try that. - Lukas Knuth
Have you made sure that the request you sent (from which you want to send the data to Azure) actually returns something? Try piping the stream to a file (on your machine) and see if its complete. - Lukas Knuth
Yes I have already piped it in file and I get audio file with data inside ( recorded voice ) - infodev
Any errors? Have you tried setting the correct size (see my comment)? Have you tried doing it with a file from disk (not from the response)? - Lukas Knuth

1 Answers

0
votes

I think you can't upload a file directly so first of all create a folder in your blob storage then create a file. Read the selected file data and write it into the created file using file stream or else.

here is the upload file code,

 app.post('/upload',function(req,res){
 if(req.files.fileInput){
 var file = req.files.fileInput,
 name = file.name,
 type = file.mimetype;
 var uploadpath = __dirname + '/uploads/' + name;
 file.mv(uploadpath,function(err){
  if(err){res.send("Error Occured!")}
  else {res.send('Done! Uploading files')}
  });
}
  else {
  res.send("No File selected !");
  res.end();
};
})