简体   繁体   English

在S3和cloudfront上运行carrierwave私有文件

[英]rails carrierwave private files on S3 and cloudfront

I have both public and private files which I server from Amazon cloudfront, the public files work fine but now I'd like to secure some of them as private with an authenticated read. 我有公共和私人文件,我从亚马逊云端服务器,公共文件工作正常,但现在我想保护其中一些作为私有与经过身份验证的读取。

The private files have their own Uploader DocumentUploader, do the files need to be stored in separate buckets? 私有文件有自己的Uploader DocumentUploader,文件是否需要存储在不同的存储桶中? As it is now they are all in the one bucket. 就像现在一样,他们都在一个桶里。

I've done something similar with Paperclip awhile back but can't seem to find a good resource for doing it with Carrierwave and using a timed Authenticated_url 我曾经用Paperclip做了类似的事情,但似乎找不到使用Carrierwave并使用定时Authenticated_url来做这件事的好资源

I see they have something like it here: 我看到他们在这里有类似的东西:

http://www.rdoc.info/github/jnicklas/carrierwave/5d1cb7e6a4e8a4786c2b/CarrierWave/Storage/Fog/File#authenticated_url-instance_method 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: 取决于安全性,但您可以设置特定Uploader类本身的文件权限,覆盖默认权限,如下所示:

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. 这将自动导致此上传器中的文件现在以临时AWS过期和访问键为前缀,并且将来的上载将设置为私有,即不可公开访问。

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

As far as I can see here you may need to create another bucket for secured files. 至于我可以看到这里 ,你可能需要创建另一个桶加密文件。

You can implement the security for your 'private' files by your own, in your model (if you have one) you can add a field that checks if the file is secure or not, then you can manage this scenario using your controller. 您可以自己实现“私有”文件的安全性,在您的模型中(如果有的话)可以添加一个字段来检查文件是否安全,然后您可以使用控制器管理此方案。

One nice gem that you can use is cancan . 你可以使用的一个不错的宝石是cancan With it you can manage the model and some attributes (the secure field) and provide authorization or not, based on your user's profile. 有了它,您可以根据用户的个人资料管理模型和某些属性(安全字段)并提供或不授权。

You can setup carrierwave config in separate uploader. 您可以在单独的上传器中设置carrierwave配置。 like this. 像这样。

using gem 'aws-sdk', '~> 2.10' gem 'carrierwave-aws', '~> 1.1' 使用gem'aws-sdk','〜> 2.10'宝石'carrierwave-aws','〜> 1.1'

    class BusinessDocumentUploader < CarrierWave::Uploader::Base

    def initialize(*)
      super



      CarrierWave.configure do |config|
      config.storage    = :aws
      config.aws_bucket = Rails.application.secrets.aws_bucket
      config.aws_acl    = 'private'

    #acl: "private", # accepts private, public-read, public-read-write, authenticated-read, aws-exec-read, bucket-owner-read, bucket-owner-full-control
      # Optionally define an asset host for configurations that are fronted by a
      # content host, such as CloudFront.
      config.asset_host = Rails.application.secrets.aws_asset_host

      # The maximum period for authenticated_urls is only 7 days.
      config.aws_authenticated_url_expiration = 60 * 60 * 24 * 7
      # config.aws_authenticated_url_expiration = 2

      # Set custom options such as cache control to leverage browser caching
      config.aws_attributes = {
        expires: 1.week.from_now.httpdate,
        cache_control: 'max-age=604800'
      }

      config.aws_credentials = {
        access_key_id:     Rails.application.secrets.aws_access_key_id,
        secret_access_key: Rails.application.secrets.aws_secret_access_key,
        region:            Rails.application.secrets.aws_region # Required
      }

    end

    end
    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM