服务存储在S3在EX preSS / nodejs的应用程序文件应用程序、文件、EX、nodejs

2023-09-11 08:53:39 作者:厌倦

我的应用程序,其中用户的照片是私有的。我将照片存储(缩略图也可以)在AWS S3。有一个在网站,用户可以查看自己的照片(即缩略图)的页面。现在我的问题是我怎么为这些文件。我已经评估了一些选项有:

I have app where user's photos are private. I store the photos(thumbnails also) in AWS s3. There is a page in the site where user can view his photos(i.e thumbnails). Now my problem is how do I serve these files. Some options that I have evaluated are:

在使用签名的URL生成从CloudFront的(或AWS)提供文件服务。但问题是每一个用户刷新页面我再次创建这么多的签约URL和加载时间。因此,因此,我将无法缓存的图片中,本来是一个不错的选择浏览器。反正有做仍然在JavaScript?我不能有那些网址较长的有效期,由于安全问题。其次在规定时间内,如果有人拉住了该URL,他可以查看文件,而无需通过身份验证的应用程序运行。 在其他选项是为从我前preSS应用程序本身的文件,它从S3服务器流后。这让我有HTTP缓存头,因此使浏览器缓存。这也使得确保没有可以查看文件,而不进行身份验证。理想情况下,我想流的文件和我使用NGINX代理中继对方分流到NGINX托管。但正如我看到,只能是更多钞票,如果该文件存在于同一个系统中的文件。但在这里我不得不流呢,当我得到的流完全恢复。不想在本地存储的文件。

我无法评价这两个选项将是一个更好的选择?我想重定向尽可能多的工作,尽可能地S3和CloudFront的,但即使使用被烫伤的网址也使得请求首先到我的服务器。我也想缓存功能。

I am not able to evaluate which of the two options would be a better choice?? I want to redirect as much work as possible to S3 or cloudfront but even using singed urls also makes the request first to my servers. I also want caching features.

那么,什么是理想的方法呢?与答案的具体问题,关于这些方法?

So what would be ideal way to do? with the answers for the particular questions pertaining to those methods?

推荐答案

我只想从S3流式处理。这很容易,并签署网址是要困难得多。只要确保你设置内容类型内容长度当您上传图像到S3头。

i would just stream it from S3. it's very easy, and signed URLs are much more difficult. just make sure you set the content-type and content-length headers when you upload the images to S3.

var aws = require('knox').createClient({
  key: '',
  secret: '',
  bucket: ''
})

app.get('/image/:id', function (req, res, next) {
  if (!req.user.is.authenticated) {
    var err = new Error()
    err.status = 403
    next(err)
    return
  }

  aws.get('/image/' + req.params.id)
  .on('error', next)
  .on('response', function (resp) {
    if (resp.statusCode !== 200) {
      var err = new Error()
      err.status = 404
      next(err)
      return
    }

    res.setHeader('Content-Length', resp.headers['content-length'])
    res.setHeader('Content-Type', resp.headers['content-type'])

    // cache-control?
    // etag?
    // last-modified?
    // expires?

    if (req.fresh) {
      res.statusCode = 304
      res.end()
      return
    }

    if (req.method === 'HEAD') {
      res.statusCode = 200
      res.end()
      return
    }

    resp.pipe(res)
  })
})