如何cachingnode.js服务器上的AWS S3文件并根据请求提供服务?

对于我的网站,我将所有assets (字体/图像/ js等)部署到S3 bucketindex.html (单页面的Ember.js应用程序)部署在弹性beanstalk node.js服务器上。 节点app.js会向www.domain.com/*发送任何请求并提供本地存储的index.html 。 我希望能够削减为每个生产构build部署新应用程序到弹性beanstalk的过程,并简单地将所有资产和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 。 caching的最佳做法是什么,最好是1分钟到期。

谢谢!

看看亚马逊的CloudFront。 听起来很熟悉你想要完成的任务,即文件不必再次通过你的服务器。 为整页加载的往返添加一点点。

也就是说,要在本地进行caching,可以将整个文件存储在Redis (或其他类似的快捷方式, Raikmemcache等)中。

  1. 在您的服务器上运行Redis
  2. 从S3中将文件存储到Redis中
  3. 设置保存到Redis后的呼气时间
  4. 在从S3重新提取之前,请检查Redis的自定义密钥
    • 如果存在,则使用它并重置超时
    • 如果不是,则从S3重新存储(存储在Redis中并设置超时)

我不确定如果这些文件很大,这会如何回应,但是每次都会比从S3中抽出更快。