rails carrierwave private files on S3 and cloudfront

后端 未结 3 801
遥遥无期
遥遥无期 2020-12-30 07:53

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 authentica

3条回答
  •  别那么骄傲
    2020-12-30 08:24

    You can setup carrierwave config in separate uploader. like this.

    using gem 'aws-sdk', '~> 2.10' gem '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
    

提交回复
热议问题