Search code examples
ruby-on-railsamazon-s3rails-activestorage

ActiveStorage and s3 - setup all images public by default


I need to upload a file in my rails 6 app to s3 in order to generate a public link to share with a third part application.

actually I am sending the link to the other app with:

object.media.service_url.sub(/\?.*/, '')

and the bucket config has this configuration:

enter image description here

and this policy:

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "Stmt1420751757000",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::XXXXXXXXXXXX:user/YYYYYYYYY"
        },
        "Action": "s3:*",
        "Resource": [
            "arn:aws:s3:::bucket-dev",
            "arn:aws:s3:::bucket-dev/*"
        ]
    }
]

}

but when I try to see the URL content I still having an AccessDenied response:

<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId>CD3A94D81CC8418D</RequestId>
    <HostId>5OFKgoUzoDxoB72g4AIYePONGkn7FtulUNk6kCga57FDSBACzenjipxyeekGNiRbBmXVKsEdxxfiQehb06z4wQ== 
  </HostId>
</Error>

There is some other configuration that I am still missing?


Solution

  • Well I solved that issue using:

    Rails.application.routes.url_helpers.url_for(object.media)
    

    Instead of:

    object.media.service_url.sub(/\?.*/, '')
    

    It works even if you have blocked all public access for your bucket.