如何使用载波获取S3专用存储桶中对象的访问URL

时间:2019-01-09 12:48:28

标签: ruby-on-rails amazon-s3 carrierwave

我正在使用策略为“桶和对象不是公共的”的carrierwave将文件上传到s3的专用桶。

现在,当我尝试为该私有存储桶中的对象生成带有@docuemnt.pdf_file.url的访问URL时,即使在将bucketname传递为bucket_name is required的情况下,我仍然收到一个错误消息,提示“ @docuemnt.pdf_file.url 'privatebucketname'”收到错误消息“ ArgumentError: Version privatebucketname doesn't exist!

我应该如何使用载波从我的Rails应用程序中获取URL来访问私有存储桶对象。

我的上传者代码如下:

class SecureUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick


  storage :fog

  def fog_directory
    ENV['AWS_SECURED_BUCKET']
  end

  def fog_public
    false
  end

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

我的载波配置如下:

CarrierWave.configure do |config|
  config.fog_credentials = {
    provider:               'AWS',
    aws_access_key_id:      ENV['AWS_KEY'],
    aws_secret_access_key:  ENV['AWS_SECRET'],
    region:                 ENV['AWS_REGION']
  }

  # For testing, upload files to local `tmp` folder.
  if Rails.env.test? || Rails.env.cucumber?
    config.storage           = :file
    config.enable_processing = false
    config.root              = "#{Rails.root}/tmp"
  else
    config.storage = :fog
  end

  config.cache_dir        = "#{Rails.root}/tmp/uploads" # To let CarrierWave work on Heroku
  config.fog_directory    = ENV['AWS_BUCKET']
end

1 个答案:

答案 0 :(得分:0)

I normally have an initializer carrierwave.rb and keep config there.

CarrierWave.configure do |config|
  if Rails.env.production?
    config.fog_provider = 'fog/aws'
    config.enable_processing = true
    config.fog_directory = Rails.application.credentials.aws[:bucket]
    config.fog_public = false # optional, defaults to true
    config.fog_attributes = { "Cache-Control" => "max-age=315576000" }

    config.fog_credentials = {
      provider: "AWS",
      aws_access_key_id: Rails.application.credentials.aws[:access_key_id]
      aws_secret_access_key: Rails.application.credentials.aws[:secret_access_key]
      region: Rails.application.credentials.aws[:region]
    }
    config.storage = :fog
  else
    config.enable_processing = false
    config.storage = :file
  end
end