salmon
salmon

Reputation: 7

Paperclip S3 Heroku - missing required :bucket option

I'm trying to use Paperclip and SWS S3 on Heroku to let users upload images.

I follow this tutorial and what I have when create new user

ArgumentError in UsersController#create

app/controllers/users_controller.rb:28:in `create'

i have a no idea with it

users_controller.rb

def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to bequem!"
      redirect_to @user
    else
      render 'new'
    end
  end

new.html.erb

<div class="col-md-4 col-md-offset-4">
<%= provide(:title, 'Sign up') %>
<h1>Sign up</h1>


<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :avatar %><br />
  <%= f.file_field :avatar %>
-->
  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :password %>
  <%= f.password_field :password %>

  <%= f.label :password_confirmation, "Confirmation" %>
  <%= f.password_field :password_confirmation %>

  <%= f.submit "Create my account", :class => "btn btn-large btn-primary" %>
<% end %>


</div>

paperclip config on production and development

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

user model

class User < ActiveRecord::Base

  # This method associates the attribute ":avatar" with a file attachment
  has_attached_file :avatar, styles: {
    thumb: '100x100>',
    square: '200x200#',
    medium: '300x300>'
  }

  attr_accessible :name, :email, :password, :password_confirmation, :avatar

papertrail log

Started GET "/signup" for 176.226.131.101 at 2014-01-20 17:45:54 +0000 
Jan 20 09:45:55 milker app/web.1:  Processing by UsersController#new as HTML 
Jan 20 09:45:55 milker app/web.1:    Rendered shared/_error_messages.html.erb (0.6ms) 
Jan 20 09:45:55 milker app/web.1:    Rendered users/new.html.erb within layouts/application (164.7ms) 
Jan 20 09:45:55 milker app/web.1:    Rendered layouts/_shim.html.erb (0.0ms) 
Jan 20 09:45:55 milker app/web.1:  Completed 200 OK in 199ms (Views: 168.1ms | ActiveRecord: 15.3ms) 
Jan 20 09:46:11 milker app/web.1:  Started POST "/users" for 176.226.131.101 at 2014-01-20 17:46:11 +0000 
Jan 20 09:46:11 milker app/web.1:  Processing by UsersController#create as HTML 
Jan 20 09:46:11 milker app/web.1:    Parameters: {"utf8"=>"✓", "authenticity_token"=>"5KZg5kTVOWKN/VBFlw6BC5pJe1EGCBYtVN5zBXLXNT4=", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f4f673ad6b8 @original_filename="3adPUTPPeg8.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"3adPUTPPeg8.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20140120-2-n87ap6>>, "name"=>"slasher", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"} 
Jan 20 09:46:11 milker app/web.1:  Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:11 milker app/web.1:  Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:11 milker app/web.1:  Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:11 milker app/web.1:  Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:11 milker app/web.1:  Command :: convert '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' -auto-orient -resize "100x100>" '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-k3pl9m' 
Jan 20 09:46:11 milker app/web.1:  Command :: file -b --mime '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-k3pl9m' 
Jan 20 09:46:11 milker app/web.1:  Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:12 milker app/web.1:  Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:12 milker app/web.1:  Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:12 milker app/web.1:  Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:12 milker app/web.1:  Command :: convert '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' -auto-orient -resize "200x" -crop "200x200+0+33" +repage '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-1acel98' 
Jan 20 09:46:12 milker app/web.1:  Command :: file -b --mime '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-1acel98' 
Jan 20 09:46:12 milker app/web.1:  Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:12 milker app/web.1:  Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:12 milker app/web.1:  Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:12 milker app/web.1:  Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' 
Jan 20 09:46:12 milker app/web.1:  Command :: convert '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' -auto-orient -resize "300x300>" '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-1fz8qbp' 
Jan 20 09:46:12 milker app/web.1:  Command :: file -b --mime '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-1fz8qbp' 
Jan 20 09:46:12 milker app/web.1:  [paperclip] saving /users/avatars/000/000/007/original/3adPUTPPeg8.jpg 
Jan 20 09:46:13 milker heroku/router:  at=info method=POST path=/users host=milker.herokuapp.com request_id=97266e1a-098b-4676-9ad2-791e85d25f43 fwd="176.226.131.101" dyno=web.1 connect=14ms service=2457ms status=500 bytes=643 
Jan 20 09:46:13 milker app/web.1:  Completed 500 Internal Server Error in 1653ms 
Jan 20 09:46:13 milker app/web.1:  ArgumentError (missing required :bucket option): 
Jan 20 09:46:13 milker app/web.1:    app/controllers/users_controller.rb:28:in `create' 

Any ideas?
thank you in advance

Upvotes: 1

Views: 2429

Answers (4)

dinosaurW
dinosaurW

Reputation: 113

Add this to your application.rb file inside the module and class:

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

Upvotes: 0

salmon
salmon

Reputation: 7

Working user model. Plan to use Fedora in the future.

has_attached_file :avatar, :styles => { :small => '60x60#', :large => '300x300#' }, :default_style => :large,
  :storage => :s3,
  :default_url => '/images/:attachment/missing_:style.png',
  :path => "users/:id/avatar/:style.:extension",
  :bucket => "bequem",
  :s3_credentials => {
    :access_key_id => "xxx",
    :secret_access_key => "xxx"
  }

Upvotes: 0

Richard Peck
Richard Peck

Reputation: 76774

Further to manojild's answer, here's what you need to do (we've had this problem before):


Model

Don't know why, but you've got to include the :bucket option in your Paperclip model:

has_attached_file :image,
                :styles => { :medium => "x300", :thumb => "x100" },
                :default_url => "************",
                :storage => :s3,
                :bucket => 'bucket_name',
                :s3_credentials => S3_CREDENTIALS (we set this in an initializer)

ENV Vars

You're using ENV vars in your development.rb

Unless you've actually set these VARs in your operating environment, you won't be able to access them. I'd recommend using the Figaro gem to handle ENV vars in development & production, keeping consistency

Really you want your ENV vars to be like this:

:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']

You have them with the actual content inside currently

Upvotes: 2

manojlds
manojlds

Reputation: 301147

From your code:

:bucket => ENV['BEQUEM'],
:access_key_id => ENV['AKredacted'],
:secret_access_key => ENV['AKredacted']

Follow the tutorial exactly. It asks you to add:

:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']

These are environmental variables, and will be set using the heroku config command. You have replaced these with the actual values ( and potentially causing a security issue by exposing them here in public)

As mentioned in the tutorial, run these commands from you console:

$ 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

Don't forget to replace your_bucket_name etc with the actual value here :)

Upvotes: 1

Related Questions