我正在尝试让 Carrierwave 与我的 Amazon S3 存储桶一起使用,但我的运气有点失望。在我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_SECRET_ACCESS_KEY']
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = ENV['AWS_S3_BUCKET']
config.fog_public = true
config.fog_attributes = {'Cache-Control' => 'max-age=315576000'}
end
我将 gemfigaro
用作容纳我的环境配置变量的机制。在 内部application.yml
,我定义了ENV['AWS_ACCESS_KEY_ID']
、ENV['AWS_SECRET_ACCESS_KEY']
和ENV['AWS_S3_BUCKET']
。
这三个变量似乎正确填充。使用 FTP 程序,我可以使用我在application.yml
文件中定义的访问密钥和秘密访问密钥连接到我的 amazon s3 实例。我已经三次检查我的公钥和私钥是否正确。
尝试在我的 Rails 应用程序中执行上传时,我收到一条错误消息,指出:
Expected(200) <=> Actual(403 Forbidden) response => #<Excon::Response:0x007fb29a862ac8
@data={:body=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><
Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided.
Check your key and signing method.</Message>
为了解决这个问题,我还尝试过:
- 创建一个新的 Amazon S3 存储桶
- 手动将 access_key_id/secret_access_key/fog_directory 放到配置文件中。
这两个选项似乎都不起作用。当我创建一个新的 S3 存储桶并在 中指定它时config.fog_directory
,我收到了同样的错误。
当我在carrierwave.rb
文件中手动键入密钥和存储桶名称时,我仍然收到相同的错误。
我尝试在网上搜索一下答案,但大多数答案似乎是用户错误地输入了错误的访问密钥或密钥。我不相信这里是这种情况,因为我已经carrierwave.rb
从我在 Amazon 上的 S3 管理控制台将密钥直接复制并粘贴到文件中。
如果有人有任何见解或您想让我尝试的事情,请随时发表评论。如果您需要更多信息,请告诉我,我会在这里发布。我的应用程序是一个 Rails 4 应用程序,具有以下优点:
gem 'carrierwave'
gem 'fog', github: 'fog/fog'
gem 'mini_magick'
gem 'figaro'
提前致谢!
编辑:几个补充:
我刚刚尝试创建一组新密钥,然后再次将密钥复制 + 粘贴到我的carrierwave.rb
文件中。我仍然收到同样的错误。我的文件现在如下所示:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => "***********",
:aws_secret_access_key => "***********"
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = 'soawsthree'
config.fog_public = true
config.fog_attributes = {'Cache-Control' => 'max-age=315576000'}
end
--
来自 S3 存储桶的响应。注意 - 下面列出的公共访问密钥是正确的。不确定在哪里传递秘密访问密钥?:
Expected(200) <=> Actual(403 Forbidden) response => #<Excon::Response:0x007fc6e3997008 @data={:body=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><StringToSignBytes>50 55 54 0a 0a 69 6d 61 67 65 2f 70 6e 67 0a 46 72 69 2c 20 31 31 20 4f 63 74 20 32 30 31 33 20 30 34 3a 31 30 3a 34 31 20 2b 30 30 30 30 0a 78 2d 61 6d 7a 2d 61 63 6c 3a 70 75 62 6c 69 63 2d 72 65 61 64 0a 2f 75 70 6c 6f 61 64 73 2f 70 68 6f 74 6f 2f 70 69 63 2f 33 2f 74 6f 75 63 68 2e 70 6e 67</StringToSignBytes><RequestId>22E53DA9E453D05E</RequestId><HostId>ccNccOFV+iJa3/BM9RiwAqEwmJhr1KIMvmFm0EjhrBGkN4hwMWOuRF7qH5diDYd3</HostId><SignatureProvided>hiogXz0b65S/bOSWKvca7hyXzYI=</SignatureProvided><StringToSign>PUT\n\nimage/png\nFri, 11 Oct 2013 04:10:41 +0000\nx-amz-acl:public-read\n/uploads/photo/pic/3/touch.png</StringToSign><AWSAccessKeyId>AKIAIKNIDTDI2WI7RPNA</AWSAccessKeyId></Error>", :headers=>{"x-amz-request-id"=>"22E53DA9E453D05E", "x-amz-id-2"=>"ccNccOFV+iJa3/BM9RiwAqEwmJhr1KIMvmFm0EjhrBGkN4hwMWOuRF7qH5diDYd3", "Content-Type"=>"application/xml", "Transfer-Encoding"=>"chunked", "Date"=>"Fri, 11 Oct 2013 04:10:34 GMT", "nnCoection"=>"close", "Server"=>"AmazonS3"}, :status=>403, :remote_ip=>"72.21.195.65"}, @body="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><StringToSignBytes>50 55 54 0a 0a 69 6d 61 67 65 2f 70 6e 67 0a 46 72 69 2c 20 31 31 20 4f 63 74 20 32 30 31 33 20 30 34 3a 31 30 3a 34 31 20 2b 30 30 30 30 0a 78 2d 61 6d 7a 2d 61 63 6c 3a 70 75 62 6c 69 63 2d 72 65 61 64 0a 2f 75 70 6c 6f 61 64 73 2f 70 68 6f 74 6f 2f 70 69 63 2f 33 2f 74 6f 75 63 68 2e 70 6e 67</StringToSignBytes><RequestId>22E53DA9E453D05E</RequestId><HostId>ccNccOFV+iJa3/BM9RiwAqEwmJhr1KIMvmFm0EjhrBGkN4hwMWOuRF7qH5diDYd3</HostId><SignatureProvided>hiogXz0b65S/bOSWKvca7hyXzYI=</SignatureProvided><StringToSign>PUT\n\nimage/png\nFri, 11 Oct 2013 04:10:41 +0000\nx-amz-acl:public-read\n/uploads/photo/pic/3/touch.png</StringToSign><AWSAccessKeyId>AKIAIKNIDTDI2WI7RPNA</AWSAccessKeyId></Error>", @headers={"x-amz-request-id"=>"22E53DA9E453D05E", "x-amz-id-2"=>"ccNccOFV+iJa3/BM9RiwAqEwmJhr1KIMvmFm0EjhrBGkN4hwMWOuRF7qH5diDYd3", "Content-Type"=>"application/xml", "Transfer-Encoding"=>"chunked", "Date"=>"Fri, 11 Oct 2013 04:10:34 GMT", "nnCoection"=>"close", "Server"=>"AmazonS3"}, @status=403, @remote_ip="72.21.195.65">