Nic*_*ick 5 ruby ruby-on-rails amazon-s3 amazon-web-services railstutorial.org
我正在关注Hartl的railstutorial.org并且已经到了11.4.4:生产中的图像上传.我做了什么:
权限: 本教程指示"授予对上一步中创建的用户的读写权限".但是,在存储桶的"Permissons"下,未提及新用户的名称.我只能选择Everyone,Authenticated用户,Log delivery,Me和用户名,亚马逊似乎已经从我的名字+数字创建了自己.我通过选择Authenticated用户并选中上传/删除和查看权限(而不是选择列表和编辑权限)框来尝试它.我也通过选择Everyone并检查所有四个框来尝试它.结果是一样的.
初始化carrier_wave.rb程序设置为以下代码.我已经添加region: 'eu-west-1'到初始化器(我从这里得到的一个想法)去除消息connecting to the matching region will be more performant.
if Rails.env.production?
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_ACCESS_KEY'], # Set these key's using heroku config:set S3_ACCESS_KEY=<access key>
:aws_secret_access_key => ENV['S3_SECRET_KEY'],
:region => 'eu-west-1'
}
config.fog_directory = ENV['S3_BUCKET']
end
end
Run Code Online (Sandbox Code Playgroud)
错误:
在生产中使用上传器上传图像时,我收到错误We're sorry, but something went wrong.Heroku服务器日志说:
app[web.1]: SQL (1.7ms) UPDATE "users" SET "avatar" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["avatar", "animaatjes.png.gif"], ["updated_at", "2015-05-20 12:37:56.683858"], ["id", 18]]
heroku[router]: at=info method=POST path="/users/18" host=xxx.herokuapp.com request_id=xxx-7f9f-4580-89ba-xxx fwd="xx.xxx.xx.xxx" dyno=web.1 connect=0ms service=3461ms status=500 bytes=1714
app[web.1]: Completed 500 Internal Server Error in 3317ms (ActiveRecord: 13.0ms)
app[web.1]: (1.4ms) ROLLBACK
app[web.1]: Excon::Errors::Forbidden (Expected(200) <=> Actual(403 Forbidden)
app[web.1]: excon.error.response
app[web.1]: :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>F8xxxD89</RequestId><HostId>MdB5iSMgxxx1vqE+Q=</HostId></Error>"
app[web.1]: :headers => {
app[web.1]: "Connection" => "close"
app[web.1]: "Content-Type" => "application/xml"
app[web.1]: "Date" => "Wed, 20 May 2015 12:37:57 GMT"
app[web.1]: "Server" => "AmazonS3"
app[web.1]: "x-amz-id-2" => "MdB5iSMg***K1vqdP+E+Q="
app[web.1]: "x-amz-request-id" => "F80A**C58"
app[web.1]: }
app[web.1]: :local_address => "***.**.**.**"
app[web.1]: :local_port => *****
app[web.1]: :reason_phrase => "Forbidden"
app[web.1]: :remote_ip => "**.***.***.***"
app[web.1]: :status => 403
app[web.1]: :status_line => "HTTP/1.1 403 Forbidden\r\n"
app[web.1]: app/controllers/users_controller.rb:46:in 'update'
app[web.1]: Completed 500 Internal Server Error in 6151ms (ActiveRecord: 60.7ms)?
Run Code Online (Sandbox Code Playgroud)
我做错了什么?我似乎与权限有关?
更新: 原因结果是授予用户的策略.如果我授予用户标准的AmazonS3FullAccess,那么它可以工作.它不适用于AmazonS3ReadOnlyAccess,因为用户无法保存新图像.在我的应用程序中,用户基本上只需要2个权限:上传自己的头像图像并阅读头像图像.使用AmazonS3FullAccess是安全的还是我应该编写自己的自定义策略?
我尝试了下面的自定义策略,它应该赋予应用程序读写权限(从这里采用),但仍然会产生403 Forbidden错误.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["arn:aws:s3:::mybucket"]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": ["arn:aws:s3:::mybucket/*"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
要启用S3文件上传,我必须:
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowFileUpload",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::XXXXXXX:user/instaswan"
},
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:PutObjectAcl"
],
"Resource": [
"arn:aws:s3:::instaswan-dev",
"arn:aws:s3:::instaswan-dev/*"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
确保包含顶级和"/*"资源,并包含您需要的任何其他"操作"属性.
问题的根源确实是权限问题。有必要编写自定义策略并在存储桶上设置 CORS 配置。对于想要实现 Rails 教程的本章的任何未来用户,请参阅为 Amazon S3 编写 IAM 策略和 CORS 配置以获取必要的代码。