Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js expressjs proxy static from amazon s3

When a request comes in for a page, eg app.get("/") I want to return a static HTML page from amazon s3. I know I can request it from S3 and then send it, but that seems slow. Is there anyway to tell the requester to get the file from s3 directly without changing the url?

Thanks.

Failing that, what's the fastest way to serve the file from s3?

This tutorial shows writing the file first

http://www.hacksparrow.com/node-js-amazon-s3-how-to-get-started.html

// We need the fs module so that we can write the stream to a file
var fs = require('fs');
// Set the file name for WriteStream
var file = fs.createWriteStream('slash-s3.jpg');
knox.getFile('slash.jpg', function(err, res) {
    res.on('data', function(data) { file.write(data); });
    res.on('end', function(chunk) { file.end(); });
});

Is there a way to send the file without writing it first? Writing it seems awfully slow.

like image 798
Harry Avatar asked Oct 29 '25 17:10

Harry


1 Answers

As you suspected, you cannot get the requester to fetch from S3 directly without changing the URL. You have to proxy the remote page:

var http = require('http'),
    express = require('express'),
    app = express();

app.get('/', function(req, res) {
  http.get('http://www.stackoverflow.com', function(proxyRes) {
    proxyRes.pipe(res);
  });
});

app.listen(8080);

You can cache the remote page for better performance.

like image 180
Laurent Perrin Avatar answered Oct 31 '25 07:10

Laurent Perrin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!