Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

ruby on rails - Carrierwave Upload with Amazon S3 - 403 Forbidden Error

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??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...