BON
BON

Reputation: 443

rails 4 paperclip upload to amazon s3 not working

I have been following the rails 4 tutorial on railstutorial.org. I have completed most of it, the project is hosted on heroku but now want to add image uploading to Amazon S3. I have followed the guide on the heroku website but cannot get anything to upload to my bucket on S3 (Europe).

I am using paperclip 3.5.2.

Post model

  has_attached_file :post_photo,
                    styles: {
                        thumb: '100x100>',
                        square: '200x200#',
                        medium: '300x300>'
                    },
                    :storage => :s3,
                    :s3_credentials => {
                        :access_key_id => ENV['S3_KEY'],
                        :secret_access_key => ENV['S3_SECRET'] },
                    :s3_protocol => "https",
                    :path => ":class/:id/:basename_:style.:extension",
                    :url => ':s3_eu_url',
                    :bucket => 'bucket_name' 

Post Controller

def post_params
  params.require(:post).permit(:post_photo, :user_username, :title, :comment, :location, :user_id)
end

config/initializers/Paperclip.rb

Paperclip.interpolates(:s3_eu_url) { |attachment, style|
  "#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
}

config/environment.rb

require 'aws/s3'
AWS::S3::DEFAULT_HOST = "s3-eu-west-1.amazonaws.com"

config/environments/production.rb

 # config/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']
      },
      :url => ':s3_eu_url',
      :path => ":class/:id/:basename_:style.:extension"
  }

Upvotes: 4

Views: 3738

Answers (1)

CDub
CDub

Reputation: 13354

So, I've gotten this working (not with Europe S3, but that shouldn't matter) not having anything in config/environments/production.rb -- mostly because I use the ENV variables to help control which bucket I'm pointing at, API keys, etc.

Here's my config:

In config/environments/production.rb:

Only the standard config -- nothing to do with paperclip.

In config/initializers/paperclip.rb:

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

To the above, you'd want to add:

Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'

Then, your Post model should only have to have something like:

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

This may be obvious, but also make sure you have the aws-sdk gem loaded in your Gemfile.

Let me know if you have questions. I've set this up quite a few times and will totally help troubleshoot. :)

Upvotes: 4

Related Questions