"missing required :bucket option" for Paperclip/S3
Asked Answered
F

3

11

In my Rails app I'm letting users upload an image when they create a "release", and it should upload directly to S3. I'm getting the following error in both development and production.

EDIT: I should note that this error happens when trying to upload from the release edit page on form submit.

ArgumentError in ReleasesController#update

missing required :bucket option
Rails.root: /Users/jasondemeuse/pressed

I've done this before with no issues using Carrierwave, but can't figure out what I'm doing wrong now that I'm using Paperclip. All of the fixes I've seen on SO and elsewhere are heroku issues, but I'm getting the same problem on development and none of the fixes have helped.

Here's the relevant code ("..." indicates not relevant snippets):

development.rb

Appname::Application.configure do

...

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

production.rb

Appname::Application.configure do

...

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

release.rb

class Release < ActiveRecord::Base
  attr_accessible ... :banner


  belongs_to :user


  has_attached_file :banner, styles: {
    thumb: '100x100>',
    square: '200x200#',
    medium: '300x300>',
    spread: '1200x200'
  }

  has_many :banners, :dependent => :destroy
  accepts_nested_attributes_for :banners, :allow_destroy => true


end

show.html.erb

<%= image_tag @release.banner.url(:medium) %>
<%= @release.banner.url %>

// Have both of these in for now to see if they work, but since the upload isn't working it's giving me the missing.png

_form.html.erb

<%= f.label "Add A Banner?" %><br />
<%= f.file_field :banner %>

heroku config (have the same in .bash_profile for development)

AWS_ACCESS_KEY_ID:            XXXXXXXXXXXXXXXX
AWS_BUCKET:                   appname
AWS_SECRET_ACCESS_KEY:        XXXXXXXXXXXXXXXXXXXXXXXXXXX

EDIT: Here's my the relevant part of my controller too

  def update
    @release = Release.find(params[:id])


    respond_to do |format|
      if @release.update_attributes(params[:release])
        format.html { redirect_to [@user,@release], notice: 'Release was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @release.errors, status: :unprocessable_entity }
      end
    end
  end

I know this should be extremely simple and I'm sure I just forgot something obvious, but I've been going over this walkthrough as well as fixes I've found and nothing seems to work. Is there a rake task or bundle that I forgot to run or something?

Thank you in advance!

EDIT 2: The below answers helped me out a lot, and switching to the fog gem fixed most things for me. Just in case others are having these same issues, I also was having another problem that was making this one confusing for me. If you're having heroku issues and a Paperclip::PaperclipError (Item model missing required attr_accessor for 'image_file_name'):, make sure you run heroku rake db:migrate then restart heroku with heroku restart. I loaded my schema and wrongly assumed I wouldn't need to do that.

A SO answer with the above can be found here.

Fichte answered 25/7, 2013 at 16:44 Comment(7)
What version of paperclip are you using?Conti
3.4.2 - I have gem "paperclip", "~> 3.0" in my Gemfile.Fichte
I guess one thing to try is run AWS_BUCKET=appname rails server and see if you still get the error.Conti
Also, does the problem occur in your development env AND on heroku, or have you tested only in your development env?Conti
It happens on both development and production/heroku in the same way, I guess that's why I assumed it was a obvious dumb error that I overlooked.Fichte
Can you add controller code?Peipus
It's just the default rails generated controller in the update function. From looking at the Paperclip docs, the only reason to change it would be if you had validations github.com/thoughtbot/paperclip#validationsFichte
P
19

I think that's because :bucket should be an option passed to Paperclip not to S3.
Fixed config

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

And Paperclip::Storage::S3 doc seems to confirm that, even being so poorly written/formatted.

EDIT:

In one of my projects I use Paperclip with Fog gem and this works well

Paperclip::Attachment.default_options.merge!(
  :storage => :fog,
  :fog_credentials => {
    :provider => 'AWS',
    :aws_access_key_id => ENV['S3_ACCESS_KEY_ID'],
    :aws_secret_access_key => ENV['S3_SECRET_ACCESS_KEY'],
    :region => 'eu-west-1' # in case you need it
  },
  :fog_directory => ENV['S3_BUCKET'], # only one of those is needed but I don't remember which
  :bucket => ENV['S3_BUCKET']
)
Peipus answered 30/7, 2013 at 14:43 Comment(8)
That didn't seem to make a change unfortunately, I included it in the s3_credentials according to the heroku walkthrough for paperclip. I'm going to look into it more, maybe something in that walkthrough was incorrect? Thanks for your help!Fichte
In my opinion you should stick with Paperclip's readme not some Heroku guide which is noone knows how old.Peipus
It was posted/updated in April and written by a dev at Thoughtbot, but yeah I did also look through the Paperclip readme.Fichte
Just saw your edit with fog. I've used that before with carrierwave and it worked well, I'll give it a try right now. Thanks for sticking with this!Fichte
I spent my fair share fighting with this some time ago ;)Peipus
I was going to suggest moving the bucket outside the s3_credentials hash, but if you look at the source for the s3 storage option, either format is valid. So that's not the issue.Conti
I switched to fog and everything is working! I never fixed my issue with the aws-s3 gem but oh well fog is better for what I'm doing later anyways. Thanks so much for all your help!Fichte
Great answer,this is very useful for me.Tonometer
A
1

In my case it was that I was using foreman (Heroku) which uses an .env file to store environment variables. So, when I did rake db:migrate it couldn't find the ENV['AWS_ACCESS_KEY_ID']

What I did to run my migration was I temporarily added my AWS credentials directlñy into Carrierwave config block and then removed them after...

This is not a permanent solution because next time you migrate it will say the same thing...

For the permanent solution see: Use environment variables in Rake task

which says use: foreman run rake some_task this way all variables defined in .env are loaded for the rake task too

Arte answered 5/7, 2014 at 23:17 Comment(0)
M
0

Add this to your application.rb file inside the module and class. create a local_env.yml file and put your environment variables in there. This code will load your environment variables on server start :

config.autoload_paths += %W(#{config.root}/lib)
config.before_configuration do
    env_file = File.join(Rails.root, 'config', 'local_env.yml')
    YAML.load(File.open(env_file)).each do |key, value|
        ENV[key.to_s] = value
    end if File.exists?(env_file)
end
Mcgann answered 16/3, 2016 at 1:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.