如何设置paperclip将多个文件上传到s3

时间:2014-03-10 02:39:32

标签: heroku ruby-on-rails-4 amazon-s3 paperclip

我对rails非常陌生,我正在尝试设置我的rails应用程序以使用paperclip将多个图像上传到s3。

我做了什么:

environments/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']
  }
}

在我的模型中:

has_attached_file :avatar,
styles: {
large: '640x480#',
medium: '300x300>',
thumb: '100x100#' 
}

# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

我不确定此处包含哪些其他信息。但是我通过heroku上的指南来设置它。当我尝试上传图像时,我没有收到错误或heroku日志中的任何内容。它只是没有显示我的s3桶中的项目。

这是我从Heroku获得的日志:

2014-03-10T02:33:32.836518+00:00 app[web.1]: Started PATCH "/projects/1-casper-fire-ems-station-no-3" for 97.112.188.114 at 2014-03-10 02:33:32     +0000
2014-03-10T02:33:32.840016+00:00 app[web.1]: Processing by ProjectsController#update as HTML
2014-03-10T02:33:32.840221+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"inRDCj8EKZppoykWoVWnQTuETCc8prymcWTAg8SeJxI=",     "project"=>{"name"=>"No. 3", "category_id"=>"1", "type_id"=>"1", "description"=>"2\" Snow retention system.", "size"=>"14,800 sf", "city"=>    "Chyenne", "state"=>"WY", "year"=>"2010", "assets_attributes"=>{"0"=>{"asset"=>#<ActionDispatch::Http::UploadedFile:0x007f5a36dc9370 @tempfile=#<    Tempfile:/tmp/RackMultipart20140310-13-npdcmq>, @original_filename="181740-MetalRoofRetrofit.jpg", @content_type="image/jpeg", @headers="Content-    Disposition: form-data; name=\"project[assets_attributes][0][asset]\"; filename=\"181740-MetalRoofRetrofit.jpg\"\r\nContent-Type: image/jpeg\r\n">    , "caption"=>"first image", "homescreen_picture"=>"1"}, "1"=>{"caption"=>"", "homescreen_picture"=>"0"}, "2"=>{"caption"=>"",     "homescreen_picture"=>"0"}, "3"=>{"caption"=>"", "homescreen_picture"=>"0"}, "4"=>{"capti
2014-03-10T02:33:32.840221+00:00 app[web.1]: on"=>"", "homescreen_picture"=>"0"}}}, "commit"=>"Update Project", "id"=>"1-casper-fire-ems-station-    no-3"}
2014-03-10T02:33:32.887302+00:00 app[web.1]: Redirected to http://slow-prison-8111.herokuapp.com/
2014-03-10T02:33:32.887413+00:00 app[web.1]: Completed 302 Found in 47ms (ActiveRecord: 11.2ms)
2014-03-10T02:33:33.022342+00:00 app[web.1]: Processing by StaticController#index as HTML
2014-03-10T02:33:33.020299+00:00 app[web.1]: Started GET "/" for 97.112.188.114 at 2014-03-10 02:33:33 +0000
2014-03-10T02:33:33.028706+00:00 app[web.1]:   Rendered static/index.html.erb within layouts/application (0.9ms)
2014-03-10T02:33:33.034121+00:00 app[web.1]: Completed 200 OK in 12ms (Views: 4.9ms | ActiveRecord: 4.6ms)
2014-03-10T02:33:33.519801+00:00 app[web.1]: Started GET "/assets/BRLogoClear.png" for 97.112.188.114 at 2014-03-10 02:33:33 +0000
2014-03-10T02:33:34.887395+00:00 heroku[router]: at=info method=HEAD path=/ host=slow-prison-8111.herokuapp.com request_id=d9290026-84f1-459a-8ee2    -a841aa9e196a fwd="50.31.164.139" dyno=web.1 connect=2ms service=18ms status=200 bytes=873
2014-03-10T02:33:34.884045+00:00 app[web.1]: Completed 200 OK in 9ms (Views: 4.3ms | ActiveRecord: 2.3ms)
2014-03-10T02:33:34.873392+00:00 app[web.1]: Started HEAD "/" for 50.31.164.139 at 2014-03-10 02:33:34 +0000
2014-03-10T02:33:34.875333+00:00 app[web.1]: Processing by StaticController#index as */*
2014-03-10T02:33:34.880412+00:00 app[web.1]:   Rendered static/index.html.erb within layouts/application (0.2ms)

如果我需要提供更多信息,请告诉我。我几天来一直在努力解决这个问题,我不知道发生了什么。

2 个答案:

答案 0 :(得分:1)

我搞定了! 我将模型更改为以下内容并再次开始工作!我必须在某个时候编辑它并在has_attached_file之后输入错误的名称。

class Asset < ActiveRecord::Base
  belongs_to :project

  has_attached_file :asset,
  styles: {
    large: '640x480#',
    medium: '300x300>',
    thumb: '100x100#'
  },
  :path => ':class/:id/:style.:extension'

end

答案 1 :(得分:0)

我使用paperclip上传多个文件。这是我在模型中的内容:

has_attached_file :avatar, 
    :styles => { :medium => "100x100>", :thumb => "50x50>" },
    :convert_options => { :thumb => "-quality 92", :medium => "-quality 92" },
    :storage => :s3,
    :bucket => 'media.mydomain.com',
    :s3_credentials => {
        :access_key_id => 'my_access_key',
        :secret_access_key => 'my_secret'
    },
    :path => ':class/:id/:style.:extension'

我的production.rb或任何其他配置文件中没有任何内容