Rails paperclip error `AWS::S3::Errors::BadRequest` on `exists?` and `clear`
Asked Answered
J

1

8

all.

I've got everything working fine: images are being uploaded and can be accessed via Web with paperclip and S3, except when I log into rails c and type something like this:

> User.first.avatar.exists?
[AWS S3 400 0.093287 0 retries] head_object(:bucket_name=>"mozg-staging-static",:key=>"users/avatars/000/000/001/original/289736.jpg") AWS::S3::Errors::BadRequest AWS::S3::Errors::BadRequest

=> false

The same thing is with the clear method. Found no solution yet.

I have this permission policy:

    {
            "Sid": "Stmt1436958517000",
            "Effect": "Allow",
            "Action": [
                "s3:AbortMultipartUpload",
                "s3:DeleteObject",
                "s3:DeleteObjectVersion",
                "s3:GetBucketAcl",
                "s3:GetBucketCORS",
                "s3:GetObject",
                "s3:GetObjectAcl",
                "s3:GetObjectVersion",
                "s3:GetObjectVersionAcl",
                "s3:ListBucket",
                "s3:ListBucketMultipartUploads",
                "s3:ListBucketVersions",
                "s3:ListMultipartUploadParts",
                "s3:ListObjects",
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:PutObjectVersionAcl",
                "s3:RestoreObject"
            ],
            "Resource": [
                "arn:aws:s3:::mozg-staging-static",
                "arn:aws:s3:::mozg-staging-static/*"
            ]
        }

Thank you for support.

Janiecejanifer answered 15/7, 2015 at 15:59 Comment(1)
What version of paperclip? What version of aws-sdk? Do exists? and clear work when running a server even though they don't work in the console? (i.e. add that line you ran in the console to a controller somewhere--does it log the same error?)Tsosie
U
0

This could be a problem with your policy, can you try:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
          "s3:ListBucket"
      ],
      "Resource": ["arn:aws:s3:::mozg-staging-static"]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:GetObject",
        "s3:DeleteObject"
      ],
      "Resource": ["arn:aws:s3:::mozg-staging-static/*"]
    }
  ]
}

The ListBucket action needs to be set at a bucket level, whereas the PutObject, GetObject and DeleteObject need to be set against objects in the bucket. I left out the other actions to keep the answer short. You will of course need to add them back in if they are required. You can find a list of actions and whether they are bucket or object actions here: http://docs.aws.amazon.com/AmazonS3/latest/dev/using-with-s3-actions.html

Unstrained answered 3/6, 2016 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.