1
votes

I am attempting to use Carrierwave with Amazon S3 in my Rails app, and I keep getting the error

"Excon::Errors::Forbidden (Expected(200) <=> Actual(403 Forbidden)."  
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.

I also receive the warning

"[WARNING] fog: the specified s3 bucket name() is not a valid dns name, which will negatively impact performance.  For details see: http://docs.amazonwebservices.com/AmazonS3/latest/dev/BucketRestrictions.html"  

config/initializers/carrierwave.rb:

CarrierWave.configure do |config|
  config.fog_credentials = {
    provider: 'AWS',                      
    aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
    aws_secret_access_key: ENV["AWS_ACCESS_KEY"]
  }
  config.fog_directory = ENV["AWS_BUCKET"]                
end

My bucket name is "buildinprogress"

I've double checked that my access key ID and access key are correct.

How can I fix this error??

1
I'm finding that if I hardcode the bucket name in my carrierewave.rb file, it works, but when I have the bucket name defined as an environment variable, it doesn't work.scientiffic
That can't be; you probably messed up something with the ENV variables then. Do a puts ENV["AWS_BUCKET"].inspect in the initializer file and check that the correct bucket name is printed...severin

1 Answers

6
votes

It is a problem with Fog/Excom that kept throwing random errors for me too.

My fix was to remove gem 'fog' and replace it with gem 'carrierwave-aws' instead.

Then, in your *_uploader.rb change

storage :fog ---> storage :aws

and update your carrierwave.rb file Ex.:

  CarrierWave.configure do |config|
    config.storage    =  :aws                  # required
    config.aws_bucket =  ENV['S3_BUCKET']      # required
    config.aws_acl    =  :public_read

    config.aws_credentials = {
      access_key_id:      ENV['S3_KEY'],       # required
      secret_access_key:  ENV['S3_SECRET']     # required
    }

    config.aws_attributes = {
                              'Cache-Control'=>"max-age=#{365.day.to_i}",
                              'Expires'=>'Tue, 29 Dec 2015 23:23:23 GMT'
                            }
  end

For more info check out the carrierwave-aws GitHub page