Carrierwave上传与Amazon S3 - 403 Forbidden错误错误、上传、Amazon、Carrierwave

2023-09-11 23:41:19 作者:回首尽独你

我试图用Carrierwave与Amazon S3在我的Rails应用程序,我不断收到错误

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"  

配置/初始化/ 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

我的桶的名字是buildinprogress

My bucket name is "buildinprogress"

我已经双重检查,我的访问密钥ID和访问密钥是正确的。

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

我该如何解决这个错误?

How can I fix this error??

推荐答案

这是雾/取消执行委员会的问题,即不停地抛出随机误差对我来说太。

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

我的修正是删除宝石'雾'并将其替换创业板carrierwave-AWS代替。

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

然后,在你的 * _ uploader.rb 更改

storage :fog ---> storage :aws

和更新您的 carrierwave.rb 文件例:

  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

有关详细信息请查看 carrierwave-AWS GitHub的页面

For more info check out the carrierwave-aws GitHub page