在 S3 和 cloudfront 上使用 railscarrierwave 私有文件 [英] rails carrierwave private files on S3 and cloudfront

查看:23
本文介绍了在 S3 和 cloudfront 上使用 railscarrierwave 私有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从 Amazon cloudfront 提供服务器的公共和私有文件,公共文件工作正常,但现在我想通过经过身份验证的读取保护其中一些为私有文件.

私有文件有自己的Uploader DocumentUploader,文件需要存放在单独的bucket中吗?就像现在一样,它们都在一个桶中.

不久前我用 Paperclip 做过类似的事情,但似乎找不到使用 Carrierwave 和使用定时 Authenticated_url 的好资源

我看到他们在这里有类似的东西:

http://www.rdoc.info/github/jnicklas/carrierwave/5d1cb7e6a4e8a4786c2b/CarrierWave/Storage/Fog/File#authenticated_url-instance_method

但我不确定如何实现它.

任何提示将不胜感激.

解决方案

取决于安全程度,但您可以在特定的 Uploader Class 本身上设置文件权限以覆盖默认权限,如下所示:

class SomeUploader 

这将自动导致来自此上传器的文件现在预先带有临时 AWS 到期和访问密钥,并且未来上传将设置为私有,即不可公开访问.

http://www.rdoc.info/github/jnicklas/carrierwave/5d1cb7e6a4e8a4786c2b/CarrierWave/Storage/Fog/File#authenticated_url-instance_method

But I'm not sure how to implement it.

Any tips would be greatly appreciated.

解决方案

Depends how secure, but you can set file permissions on the particular Uploader Class itself overriding the default permissions like so:

class SomeUploader < CarrierWave::Uploader::Base

  def fog_public
    false
  end

  def fog_authenticated_url_expiration
    5.minutes # in seconds from now,  (default is 10.minutes)
  end
  .....

That will automatically cause the files from this Uploader to now be prepended with the temporary AWS expiration and accesskeys and future uploads will be set to private, ie not publicly accessible.

https://s3.amazonaws.com/uploads/something/1234/124.pdf?AWSAccessKeyId=AKIAJKOSTQ6UXXLEWIUQ&Signature=4yM%2FF%2F5TV6t4b1IIvjseenRrb%2FY%3D&Expires=1379152321

这篇关于在 S3 和 cloudfront 上使用 railscarrierwave 私有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆