简体   繁体   English

尝试设置亚马逊的S3存储桶:403禁止错误和设置权限

[英]Trying to set up Amazon's S3 bucket: 403 Forbidden error & setting permissions

I'm following Hartl's railstutorial.org and have arrived at 11.4.4: Image upload in production . 我正在关注Hartl的railstutorial.org并且已经到了11.4.4:生产中的图像上传 What I've done: 我做了什么:

  • Signed up for Amazon Web Services 注册亚马逊网络服务
  • In Amazon Identity and Access Management I created a user. 在Amazon Identity and Access Management中,我创建了一个用户。 The user was created succesfully. 用户已成功创建。
  • In Amazon S3 I created a new bucket. 在Amazon S3中,我创建了一个新存储桶。
  • Set permissions on the new bucket: 设置新存储桶的权限:

Permissions: The tutorial instructs to "grant read and write permission to the user created in the previous step". 权限:本教程指示“授予对上一步中创建的用户的读写权限”。 However, under 'Permissons' for the bucket the new user's name is not mentioned. 但是,在存储桶的“Permissons”下,未提及新用户的名称。 I could only choose between Everyone, Authenticated users, Log delivery, Me, and a username Amazon seems to have created itself from my name + a number. 我只能选择Everyone,Authenticated用户,Log delivery,Me和用户名,亚马逊似乎已经从我的名字+数字创建了自己。 I have tried it by selecting Authenticated users and checked the boxes for Upload/Delete and View permissions (and not selecting List and Edit permissions). 我通过选择Authenticated用户并选中上传/删除和查看权限(而不是选择列表和编辑权限)框来尝试它。 I also tried it by selecting Everyone and checked all the four boxes. 我也通过选择Everyone并检查所有四个框来尝试它。 The results were the same. 结果是一样的。

  • I implemented in my Rails application following the instructions of the tutorial (I don't belief anything wen't wrong there, so I haven't included those steps here to not make it too long). 我按照本教程的说明在我的Rails应用程序中实现(我不相信那里有任何错误,所以我没有在这里包含这些步骤,以免使它太长)。

The initializer carrier_wave.rb is set to the code below. 初始化程序carrier_wave.rb设置为以下代码。 I've added region: 'eu-west-1' to the initializer (an idea I got from here ) to get rid of the message connecting to the matching region will be more performant . 我已经添加了region: 'eu-west-1'到初始化器(我从这里得到的一个想法)来摆脱connecting to the matching region will be more performant的消息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
  • It worked in development and I pushed to Heroku. 它在开发中工作,我推到了Heroku。

Error: When using the uploader in production to upload an image, I get the error We're sorry, but something went wrong. 错误:在生产中使用上传器上传图片时,我收到错误We're sorry, but something went wrong. The Heroku server log says: 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) 

What have I done wrong? 我做错了什么? I seems to have something to do with the permissions? 我似乎与权限有关?


Update: The cause turned out to be the policies granted to the user. 更新:原因结果是授予用户的策略。 If I grant the user standard AmazonS3FullAccess then it works. 如果我授予用户标准的AmazonS3FullAccess,那么它可以工作。 It doesn't work with just AmazonS3ReadOnlyAccess, since then the user can't save a new image. 它不适用于AmazonS3ReadOnlyAccess,因为用户无法保存新图像。 In my application the user basically only needs 2 rights: upload its own avatar image and read the avatar image. 在我的应用程序中,用户基本上只需要2个权限:上传自己的头像图像并阅读头像图像。 Would it be safe to use AmazonS3FullAccess or should I write my own custom policy? 使用AmazonS3FullAccess是安全的还是我应该编写自己的自定义策略?

I tried the custom policy below, which should give the application read and write rights (adopted from here ) but that still generated the 403 Forbidden error. 我尝试了下面的自定义策略,它应该赋予应用程序读写权限(从这里采用)但仍然生成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/*"]
    }
  ]
}

To enable S3 file Uploads, I had to: 要启用S3文件上传,我必须:

  • specify my region (us-west) 指定我的地区(us-west)
  • create an IAM user 创建一个IAM用户
  • add a Bucket Policy specifying that user as a Principal 添加Bucket Policy,将该用户指定为Principal
{
    "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/*"
            ]
        }
    ]
}

Be sure to include both the top-level and "/*" Resource, and include any other "Action" attributes you need. 确保包含顶级和“/ *”资源,并包含您需要的任何其他“操作”属性。

The root of the problem indeed turned out to be the permissions. 问题的根源确实证明是权限。 It was necessary to write a custom policy and set the CORS configuration on the bucket. 必须编写自定义策略并在存储桶上设置CORS配置。 For any future users that want to implement this chapter of the rails tutorial, see Writing an IAM policy and CORS configuration for Amazon S3 for the necessary code. 对于希望实现rails教程本章的任何未来用户,请参阅为Amazon S3编写IAM策略和CORS配置以获取必要的代码。

在我的情况下s3:PutObjectAcl是缺少的权限

You may be missing a step which is to set your IAM policy if using an IAM user to configure your bucket. 如果使用IAM用户配置存储桶,您可能会错过设置IAM策略的步骤。

Go to IAM console, select your user, go to the permissions tab , click the attach policy button and add administrator access . 转到IAM控制台,选择您的用户,转到permissions tab ,单击attach policy按钮并添加administrator access

After that the error should go away and you can upload files with no problem ;) 之后错误应该消失,你可以上传文件没有问题;)

I had the same problem. 我有同样的问题。 Tried many solutions including S3@FullAccess, creating users, writing custom policies. 尝试了许多解决方案,包括S3 @ FullAccess,创建用户,编写自定义策略。 If someone encounters the problem, then check bucket permissions. 如果有人遇到问题,请检查存储桶权限。 Go to bucketname > Permissions > Public Access Settings, then change two ACLs settings from True to False. 转到bucketname>权限>公共访问设置,然后将两个ACL设置从True更改为False。

I had the same problem, the thing is Amazon changed their layout so now creating a user policy is a bit different than before. 我有同样的问题,事情是亚马逊改变了他们的布局,所以现在创建一个用户政策与以前有点不同。 So once you create a policy remember to attach the policy to the user in question or you will get the 403 forbidden error 因此,一旦您创建策略,请记住将策略附加到相关用户,否则您将收到403禁止错误

I had both the issues from the tutorial - 我从教程中得到了两个问题 -

Look in your heroku logs - 查看你的heroku日志 -

Forbidden 被禁止

and

Socket Error 套接字错误

Going round trying to resolve this over a day I basically reset back to basics, Recreated a new IAM user and a new s3 bucket but this time left the REGION as US standard - the only s3 authority I could find to attach as the policy was full access - nothing else - so attached that 为了解决这个问题,我基本上重新回到了基础,重新创建了一个新的IAM用户和一个新的s3存储桶,但这次将REGION作为美国标准 - 我可以找到附加的唯一s3权限,因为策略已满访问 - 没有别的 - 如此附加

Also retyping and confirming ENV variables on heroku as well as COPY AND PASTING the carrier_wave.rb from tutorial - to be sure to be sure .) 还要在heroku上重新输入和确认ENV变量,以及从教程中复制和粘贴carrier_wave.rb - 一定要确定。)

first time round I was fiddling and unsure what to do so i might of clicked a few different things- adding groups and permissions, viewing wrong things - sometimes a RESET and STEP through everything especially after exploring AWS and IMS and s3 for the first time. 第一次我正在摆弄并且不确定该怎么做,所以我可能会点击一些不同的东西 - 添加组和权限,查看错误的东西 - 有时是重置和STEP,尤其是在第一次探索AWS和IMS和s3之后。

Interesting that in heroku they INSTRUCT to set the bucket region as US standard and post a warning - Be sure to create a bucket in the same region as your app to take advantage of AWS's free in-region data transfer rates. 有趣的是,在heroku中,他们将桶区域设置为美国标准并发布警告 - 确保在与应用程序相同的区域中创建一个存储桶,以利用AWS的免费区域内数据传输速率。

good read for heroku to s3 configuration here -> https://devcenter.heroku.com/articles/s3 很好地阅读heroku到s3配置 - > https://devcenter.heroku.com/articles/s3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM