简体   繁体   English

Paperclip S3 Heroku - 缺少必需:桶选项

[英]Paperclip S3 Heroku - missing required :bucket option

I'm trying to use Paperclip and SWS S3 on Heroku to let users upload images. 我正在尝试在Heroku上使用Paperclip和SWS S3来让用户上传图像。

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 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 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 papertrail日志

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"=>"slava.proshechkin@softeica.ru", "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 先感谢您

Further to manojild's answer, here's what you need to do (we've had this problem before): 继manojild的回答之后,这就是你需要做的事情(我们之前遇到过这个问题):


Model 模型

Don't know why, but you've got to include the :bucket option in your Paperclip model: 不知道为什么,但你必须在Paperclip模型中包含:bucket选项:

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 ENV Vars

You're using ENV vars in your development.rb 您在development.rb使用ENV vars

Unless you've actually set these VAR s in your operating environment, you won't be able to access them. 除非您在操作环境中实际设置了这些VAR否则您将无法访问它们。 I'd recommend using the Figaro gem to handle ENV vars in development & production, keeping consistency 我建议使用Figaro宝石来处理开发和生产中的ENV变量,保持一致性

Really you want your ENV vars to be like this: 真的,你希望你的ENV变量是这样的:

: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 你有他们当前的实际内容

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. 这些是环境变量,将使用heroku config命令设置。 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 :) 别忘了用这里的实际值替换your_bucket_name等:)

Working user model. 工作用户模型。 Plan to use Fedora in the future. 计划将来使用Fedora。

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"
  }

Add this to your application.rb file inside the module and class: 将它添加到模块和类中的application.rb文件中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM