createReadStream doesn't take blob url?
Asked Answered
C

1

1

this.state.videoBlob is a blob object. I used URL.createObjectURL to generate a blob URL, and passed it to fs.createReadStream, like below: fs.createReadStream(URL.createObjectURL(this.state.videoBlob)) This blob url looks like: blobURL: blob:http://localhost:3000/dabe5cdd-00cc-408a-9f3d-b0ba5f2b10b3

But I got an error saying: TypeError: fs.createReadStream is not a function

The problem won't exist if I passed some online video URL. So how can I read blob from fs.createReadStream? Thanks!

Circumvallate answered 25/3, 2020 at 22:34 Comment(3)
When I look at the code behind fs.readStream(), it calls new ReadStream() and passes the path/url to that. On this line of code, it appears that the only type of url that is supported is a file URL. The doc is silent on that topic which is why I went and looked at the code.Desrochers
I don't understand the point of a URL like this: blob:http://localhost:3000/dabe5cdd-00cc-408a-9f3d-b0ba5f2b10b3 so I'm not sure what to do with it. If it's a remote resource, then you can just use http.get() or request() on the actual URL and you'll have a readStream from that.Desrochers
blob:http://localhost:3000/dabe5cdd-00cc-408a-9f3d-b0ba5f2b10b3 is the blob address for a webcam video I did in my browserCircumvallate
D
3

When I look at the code behind fs.createReadStream(), it calls new ReadStream() and passes the path/url to that. On this line of code, it appears that the only type of url that is supported is a file URL. The doc is silent on that topic which is why I went and looked at the code. So, it does not appear to me that fs.createReadStream() supports that type of pseudo-URL.

Since you just want a readstream from that URL and you have the actual URL of the remote resource, I would suggest you just use either http.get() or request() or something similar as those will all contact the remote host and return to you a readStream. Since your objective was to get a readStream, this is one way to achieve that.

http.get('http://localhost:3000/dabe5cdd-00cc-408a-9f3d-b0ba5f2b10b3', (res) => {
     // res is a readstream here
}).on('error', (err) => {
     // error on the request here
});

FYI, you may find this answer on Blob URLs to be useful. I don't see any evidence that fs.createReadStream() supports blob URLs. In the browser, they are something created only by the internals of the browser and are only useful within that specific web page context (they refer indirectly to some internal storage) and can't be passed outside the web page or even preserved from one web page to the next. If you wanted your server to have access to the actual data from a blob URL created in the browser, you'd have to upload the actual data to your server. Your server can't access a blob URL created in the browser.

Desrochers answered 26/3, 2020 at 0:0 Comment(2)
@Circumvallate - Does this explain things for you?Desrochers
Thank you @jfriend00. It explains why fs.createReadStream() didn't work for browser generated blob.Circumvallate

© 2022 - 2024 — McMap. All rights reserved.