user3400095
user3400095

Reputation: 11

How to setup paperclip to upload multiple files to s3

I'm pretty new to rails and I'm trying to setup my rails application to use paperclip to do multiple image uploads to s3.

What I've done:

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

in my model:

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/

I'm not sure what other information to include here. But I went through the guide on heroku to get it setup. When I try to upload an image I don't get an error or anything in the heroku logs. It just doesn't show the items in my s3 bucket.

Here's the log I get from 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)

If I need to provide any more info please let me know. I've been struggling with this issue for days now and i don't know what's going on.

Upvotes: 0

Views: 352

Answers (2)

user3400095
user3400095

Reputation: 11

I got it working! I changed the model to the following and it started working again! I must have edited it at some point and put the wrong name after 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

Upvotes: 1

Tom
Tom

Reputation: 135

I have used paperclip for uploading multiple files. Here is what I have in the model:

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'

I don't have anything in my production.rb or any other config file

Upvotes: 0

Related Questions