Search code examples
node.jsamazon-s3safarivideo-streamingnode.js-stream

Serve remote url with node.js stream


I have a video stored in amazon s3.

Now I'm serving it to the client with node.js stream

return request(content.url).pipe(res)

But, the following format is not working with safari.

Safari is unable to play streamed data. But, the same code works for chrome and firefox.

I did some research and found out

chrome's request content-range param looks like

[0-]

But, safari does the same with content ranges

[0-10][11-20][21-30]

Now if the content was stored in my server, I could have break the file in chucks with

fs.createReadStream(path).pipe(res)

to serve safari with it's requested content range

As mentioned in this blog https://medium.com/better-programming/video-stream-with-node-js-and-html5-320b3191a6b6

How can I do the same with remote url stored in s3?

FYI, It's not feasible to download the content temporarily on server and delete it after serving. As, the website is supposed to receive good traffic.


Solution

  • How can I do the same with remote url stored in s3?

    Don't.

    Let S3 serve the data. Sign a URL to temporarily allow access to the client. Then, you don't have to serve or proxy anything and you save a lot of bandwidth. An example from the documentation:

    var params = {Bucket: 'bucket', Key: 'key'};
    var url = s3.getSignedUrl('getObject', params);
    console.log('The URL is', url);
    

    ...As, the website is supposed to receive good traffic.

    You'll probably also want to use a CDN to further reduce your costs and enhance the performance. If you're already using AWS, CloudFront is a good choice.