0
votes

My Rails app is hosted on Heroku but I need to get Paperclip working locally so I can upload photos in testing. I'm really close, but I'm stuck on the "missing required :bucket option" error.

I've tried moving around settings in my environment files as well as well as adding a config/application.yml file with my AWS credentials in it, but no dice.

Here's my config from development.rb (looks the same in production.rb):

config.paperclip_defaults = {
        :storage => :s3,
        :s3_credentials => {
            :bucket => ENV['S3_BUCKET_NAME'],
            :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
            :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
            }
        }

I've tried moving :bucket attribute outside of :s3_credentials but it didn't change anything. Application.yml looks like this:

AWS_ACCESS_KEY_ID="xxxxxxxxxxxxxxxxx"
AWS_SECRET_ACCESS_KEY="xxxxxxxxxxxxxxxxxxxx"
S3_BUCKET_NAME="xxxxxx"

I also installed the dot-env gem and added an s3.env file to the index of my app which contains the same credentials as above, but even that hasn't worked. Nor did adding my Paperclip config inline with the has_attached_file in my model.

What do I do??

2
The docs suggest to me that the file should be called .env, not s3.env. Are the environment variables set when development.rb is loaded?Frederick Cheung
Can you explain in a bit more detail what you mean about the environment variables in development.rb?Tom Maxwell
Actually changing to .env worked!Tom Maxwell
i recommend using figaro, it works very well with heroku github.com/laserlemon/figarojuankuquintana
Another vote for Figaro. It'll use the application.yml. No need for dot-env and another environment file.Nick Veys

2 Answers

1
votes

Alternatively, if someone runs into this problem and doesn't want to use a .env file, the following works.

Assuming your bucket is hosted in the US region, put a file called aws.yml under the config dir. The file should be in the following format:

development:
 access_key_id: AWS_ACCESS_KEY_ID
 secret_access_key: AWS_SECRET_KEY_ID

production:
 access_key_id: AWS_ACCESS_KEY_ID
 secret_access_key: AWS_SECRET_KEY_ID

In your model:

class Model < ActiveRecord::Base
  has_attached_file :avatar, styles: {
   thumb: '100x100>',
   square: '200x200#',
   medium: '300x300>'}, :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/aws.yml",
    :bucket => "your_bucket_name"

    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

Replace your_bucket_name with the name of your actual bucket.

Gemfile:

gem 'aws-sdk', '< 2.0'

Under Environments, production and development:

config.paperclip_defaults = {
    :storage => :s3,
    :s3_credentials => {
        :bucket => ENV['S3_BUCKET_NAME'],
        :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
        :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
        }
    }

To put it in production on Heroku, you have to set the following using the terminal:

$ heroku config:set S3_BUCKET_NAME=your_bucket_name
$ heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
$ heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key
0
votes

Your Application.yml file doesn't look like YAML. It should look like this:

AWS_ACCESS_KEY_ID: "xxxxxxxxxxxxxxxxx"
AWS_SECRET_ACCESS_KEY: "xxxxxxxxxxxxxxxxxxxx"
S3_BUCKET_NAME: "xxxxxx"

If you are loading the yml file like they advise here you'll want your yml file to look like this:

development:
  AWS_ACCESS_KEY_ID: "xxxxxxxxxxxxxxxxx"
  AWS_SECRET_ACCESS_KEY: "xxxxxxxxxxxxxxxxxxxx"
  S3_BUCKET_NAME: "xxxxxx"

You need a way to set the environment variables for your app in your development environment. Using Application.yml or .env file should work, but you probably don't need both.