[英]File retrieval from AWS S3 to Node.js server, and then to React client
[英]How to cache an AWS S3 file on a node.js server and serve it on request?
對於我的網站,我將所有assets
(fonts / images / js等)部署到S3 bucket
。 index.html
(單頁Ember.js應用程序)已部署在彈性beantalk node.js
服務器上。 節點app.js
接受對www.domain.com/*
任何請求,並提供本地存儲的index.html
。 我希望能夠減少為每個生產版本將新應用程序部署到Elastic beantalk的過程,並只需將所有資產和index.html
部署到S3 bucket
。
這是我到目前為止的內容:
var AWS = require('aws-sdk'),
fs = require('fs');
/*
* AWS Security credentials
*/
AWS.config.loadFromPath('./config.json');
var port = process.env.PORT || 3000,
http = require("http");
var static = require('node-static');
/*
* Create a node-static server instance
* to serve the './public' folder
*/
var file = new static.Server();
/*
* Fetch .index.html from S3
* and cache it locally
*/
var s3 = new AWS.S3();
var params = {Bucket: 'assets', Key: 'index.html'};
var file = fs.createWriteStream('index.html');
s3.getObject(params).
on('httpData', function(chunk) { file.write(chunk); }).
on('httpDone', function() { file.end(); }).
send();
var server = http.createServer(function (request, response) {
request.addListener('end', function () {
file.serveFile('index.html', 200, {}, request, response);
}).resume();
}).listen(port);
我認為這只會在服務器首次啟動時從S3獲取index.html
。 緩存的最佳實踐是什么,最好是1分鍾過期。
謝謝!
看看亞馬遜的CloudFront。 聽起來很熟悉您要完成的工作,即文件不必再次通過服務器。 給整個頁面加載的往返行程增加了一點。
也就是說,要在本地緩存,您可以將整個文件存儲在Redis
(或其他快速的東西,例如Raik
, memcache
等)。
我不確定如果文件很大,它將如何響應,但是它仍然比每次從S3提取速度更快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.