0

I am using carrierwave and fog-aws while uploading a file to aws and storing the aws url in my local DB table.I have created carrierwave.rb file to config all fog-aws credentials.

begin
 CarrierWave.configure do |config|                      # required
 config.storage = :fog

 config.fog_credentials = {
  :provider               => 'AWS',       # required
  :aws_access_key_id      => 'Key_id',       # required
  :aws_secret_access_key  => 'access_key',       # required
  :region                 => 'us-west-2'  # o\tional, defaults to 'us-east-1'
  # :fog                   => 'host',
  # :endpoint               => 'host'
 }
 config.fog_directory  = 'my-images-server' # required
 # see https://github.com/jnicklas/carrierwave#using-amazon-s3
 # for more optional configuration
 config.fog_public     = true   # optional, defaults to true

My uploader file contains

class QueryUploader < CarrierWave::Uploader::Base
 storage :fog
 def store_dir
  base_dir = File.join(Rails.root, "public", "uploads")
  "#{base_dir}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
 end
 def cache_dir
  "/tmp/service-quep"
 end
 def extension_white_list
   %w(sql)
 end
end

I read many articles and breaking my head from 2days, still could not find any solution.My access key is without space and bucket name is not trailing with a slash.Can anyone please tell why this

Expected(200) <=> Actual(403 Forbidden) excon.error.response :body => "\nSignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.YTUUYUDTDYJBKJNUFYD

error is coming.

4

1 回答 1

1

看,在这种方法上你不需要编写额外的代码,因为 Rails 目录映射会自动像你使用"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"它会自动映射一样/public/uploads/...

你的代码

def store_dir
    base_dir = File.join(Rails.root, "public", "uploads")
    "#{base_dir}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

这将是

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

你可以检查一下this gist这对于使用 Rails、CarrierWave 和 AWS 非常有用,你也可以检查一下

于 2018-03-21T05:54:58.583 回答