Search code examples
ruby-on-railsamazon-s3rails-activestorageruby-on-rails-5.2

How do I set my AWS S3 Rails 5.2 ActiveStorage to public?


According to the Rails guide, I can set the files to be public. However, when I tried it, I get this error:

Cannot load `Rails.config.active_storage.service`
invalid configuration option `:public'):

If I remove the public: true in my storage.yml file, it works. But then the file itself is private and so my Cloudflare CDN cannot serve it.

# config.storage.yml
amazon_dev:
  service: S3
  access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_KEY'] %>
  region: us-west-2
  public-read: true
  bucket: xxxxxx
# config/environments/development.rb
config.active_storage.service = :amazon_dev

Also set the AWS S3 to public: S3 bucket settings

Can't embed yet so this will be a link.

Thank you,


Solution

  • It has to do with your AWS settings, not Rails.

    When creating a bucket, UNTICK "block all public access":

    enter image description here

    See the difference of a public and a private bucket in the dashboard: enter image description here

    For adding a policy: enter image description here

    it can be something like

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "PublicRead",
                "Effect": "Allow",
                "Principal": "",
                "Action": [
                    "s3:GetObject",
                    "s3:GetObjectVersion"
                ],
                "Resource": [
                    "arn:aws:s3:::awsexamplebucket1/"
                ]
            }
        ]
    }