Node.js的EX pressjs代理从Amazon S3的静态静态、EX、js、Node

2023-09-11 09:16:43 作者:男人不拽要俊逸

当一个请求进入一个网页,如 app.get(/)我想从Amazon S3的返回一个静态的HTML页面。我知道我可以从S3请求,然后将其发送,但似乎慢。反正是有告诉请求者得到S3文件的情况下直接更改URL?

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?

感谢。

如果做不到这一点,什么是服务从S3文件最快的方法是什么?

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

本教程介绍了第一次写入文件

This tutorial shows writing the file first

HTTP://www.hacksparrow。 COM /节点JS-亚马逊S3-如何对获得-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.

推荐答案

当你怀疑,你不能请求者从S3获取的情况下直接更改URL。你要代理的远程页面:

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.

 
精彩推荐
图片推荐