Skip to content

Instantly share code, notes, and snippets.

@thepont
Last active February 24, 2023 05:27
Show Gist options
  • Save thepont/4a3ab0e094b076e06540ead8b93bb3f5 to your computer and use it in GitHub Desktop.
Save thepont/4a3ab0e094b076e06540ead8b93bb3f5 to your computer and use it in GitHub Desktop.
Simple Express HTTP example for streaming mp4 video from s3.
const AWS = require('aws-sdk');
const express = require('express');
const app = express();
const MOVIE_LOCATION_S3 = 'location/of/movie.mp4';
const AWS_S3_BUCKET = 's3/bucket'
const AWS_S3_ENDPOINT = 's3-ap-southeast-2.amazonaws.com'
const s3bucket = new AWS.S3(
{
params:{
Bucket : AWS_S3_BUCKET
},
endpoint : AWS_S3_ENDPOINT
});
app.use('/movie.mp4', (req, res)=>{
var s3Opt = {Key:MOVIE_LOCATION_S3};
if(req.headers.range){
s3Opt.Range = req.headers.range;
} else {
//return
return res.sendStatus(416);
}
var resp = s3bucket.getObject(s3Opt, (err, data) => {
if(err){
return console.err(err);
}
console.log(data);
res.status(206);
res.setHeader("Content-Range", data.ContentRange);
res.setHeader("Accept-Ranges", "bytes");
res.setHeader("Content-Length", data.ContentLength);
res.setHeader("Content-Type", "video/mp4");
res.send(data.Body);
});
})
app.use((req, res)=>{
res.writeHead(200, { "Content-Type": "text/html" });
res.end('<video src="http://localhost:8888/movie.mp4" controls></video>');
})
app.listen(8888, function () {
console.log('Movie Server on 8888!');
});
@thepont
Copy link
Author

thepont commented Jul 18, 2016

Set the constants MOVIE_LOCATION_S3, AWS_S3_BUCKET and AWS_S3_ENDPOINT to your content location. L5-L7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment