DEV Community

Discussion on: Streaming files from AWS S3 using NodeJS Stream API with Typescript

Collapse
 
srikanth597 profile image
srikanth597

I couldn't understand the calculations entirely can u please make me understand?

Since 64kb is _s3DataRange, S3 file size is let's say 128kb, then u will fetch first 64kb
But when modifying the S3StreamParams Range its caculated as bytes = -64kb ( in minus)
That's bit strange for me understand.

My second question is on next iteration it will start from 65th KB. Where exactly this iteration begins? There's no loop here that instructs it to keep repeating till completion of last bit In 128kb file.

Collapse
 
about14sheep profile image
about14sheep • Edited

For your first question, the Range parameter isn't a calculation. So the (-) hyphen there can be read as, 'grab a range of bytes starting at byte 65(up too)128'. So it wouldn't be negative.

Your second questions is really good. This is where the simplicity of NodeJS Readable Stream API comes in. When this stream is in the data flowing mode, it will call the _read() method whenever there is room in the buffer (and the stream is not paused). So you don't need to write in a loop, the super class Readable handles all this for you!

Hope this helps! If you have any other questions please let me know!

Collapse
 
arpitkumar profile image
Arpitkumar

How can we handle seeking of the video?

  <video
        onContextMenu={(e) => e.preventDefault()}
        id="videoPlayer"
        width="650"
        height="auto"
        controls
        <source src={`/api/videos?videoId=${id}`} type="video/mp4" />
      </video>
Enter fullscreen mode Exit fullscreen mode

I have created a simple video element but I am not able to seek video. Is there anything we have to do in S3DownloadStream class to make it work?

I am using Next.js for UI.

@about14sheep

Thread Thread
 
about14sheep profile image
about14sheep

That would need to be handled on the frontend. Once the S3DownloadStream grabs a range, it just pushes it through to the output. Seeking would be handled by the video player