I have a video file on aws s3 and try to stream it to client through a nodejs-express server. this is my backend code
const S3 = require('aws-sdk/clients/s3');
const accessKeyId = process.env.AWS_ACCESS_KEY
const secretAccessKey = process.env.AWS_SECRET_KEY
const region = process.env.AWS_BUCKET_REGION
const bucketName = process.env.AWS_BUCKET_NAME
const s3 = new S3({
region,
accessKeyId,
secretAccessKey,
})
function getFileStream(fileKey) {
const downloadParams = {
Key: fileKey,
Bucket: bucketName
}
return s3.getObject(downloadParams).createReadStream();
}
async function getFileSize(fileKey) {
const fileParams = {
Key: fileKey,
Bucket: bucketName
}
const data = await s3.headObject(fileParams).promise();
return data.ContentLength;
}
router.get("/test-video-stream", async (req, res) => {
const fileName = 'test-video';
const fileSize = await getFileSize(fileName);
const headers = {
"Content-Range": `bytes 0-${fileSize}`,
"Accept-Ranges": "bytes",
"Content-Type": "video/mp4",
};
res.writeHead(200, headers)
const fileStream = getFileStream(fileName);
fileStream.pipe(res);
})
and this is my front end code
<video width="320" height="240" controls="">
<source type="video/mp4" src="http://localhost:8000/test-video-stream">
</video>
I can play the video from start time 0:00 but can not click into time control bar of the video player to choose time for video start playing. Can anyone explain me why this happen ? Thank you a lot !