Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

howto: Basic setup of carrierwave [Heroku and S3]

I have an S3 bucket for production and development. I have done my research and came across this post but my current config does not work as expected. I get the following exception (below) locally and I get no file uploads to my S3 bucket from my heroku app:

 is not a recognized storage provider
 Extracted source (around line #3):

 1: 
 2: <p><%= user.name %></p>
 3: <%= image_tag user.avatar.url %>
 4: <%= link_to 'Show', user %>
 5: <%= link_to 'Edit', edit_user_path(user) %>
 6: <%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %>

However When I set storage :file inside of the *_uploader.rb file everything works as expected locally. But still noting ever gets sent to my S3 bucket.

Here is my set up:

user.rb

class User < ActiveRecord::Base
 attr_accessible :name, :avatar, :avatar_cache, :remote_avatar_url, :remove_avatar
 mount_uploader :avatar, AvatarUploader
end

fog.rb

CarrierWave.configure do |config|
  if Rails.env.production?
    config.storage = :fog
    config.fog_credentials = {
    :provider              => 'AWS',
    :aws_access_key_id     => ENV['S3_K'],
    :aws_secret_access_key => ENV['S3_SCRT'],
    :region                => ENV['S3_RG']
  }
  config.fog_directory  = ENV['S3_BUCKET']
  config.fog_host       = 'http://www.example.com' 
  config.fog_public     = true                                    # optional, defaults to true
  config.fog_attributes = {'Cache-Control' => 'max-age=315576000'}  # optional, defaults to {}

else
 #for development and testing locally
  config.storage = :file
  config.enable_processing = false
 end
end

*_uploader.rb

class AvatarUploader < CarrierWave::Uploader::Base

 storage :fog

 def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
 end

 def extension_white_list
  %w(jpg jpeg gif png)
 end

 end

users_controller.rb

def new
 @user = User.new
 @user.avatar = params[:file]
  respond_to do |format|
   format.html # new.html.erb
   format.json { render json: @user }
  end
end

def create
 @user = User.new(params[:user])
 @user.avatar = params[:file]
  respond_to do |format|
   if @user.save
    format.html { redirect_to @user, notice: 'User was successfully created.' }
    format.json { render json: @user, status: :created, location: @user }
   else
    format.html { render action: "new" }
    format.json { render json: @user.errors, status: :unprocessable_entity }
   end
end

end

UPDATE Thanks to @CanBerkGüder I can confirm that I am able to save the record but not the image file. Anytime I attempt to create a user object, my heroku log spits out:

2012-02-20T23:19:45+00:00 app[web.1]:   app/controllers/users_controller.rb:46:in `block in create'
2012-02-20T23:19:45+00:00 app[web.1]:   app/controllers/users_controller.rb:45:in `create'
2012-02-20T23:19:45+00:00 app[web.1]: 
2012-02-20T23:19:45+00:00 app[web.1]: cache: [POST /users] invalidate, pass
like image 817
rhodee Avatar asked Nov 14 '22 09:11

rhodee


1 Answers

OK, here's an idea. CarrierWave still includes an S3 adapter for backward compatibility that uses fog underneath, which I personally use instead of :fog. In theory, there should be no difference between the two, but I think it's worth a shot. Here's my CarrierWave initializer from a live application running on Heroku:

CarrierWave.configure do |config|
  if Rails.env.production?
    config.root = Rails.root.join('tmp')
    config.cache_dir = 'carrierwave'

    config.storage = :s3
    config.s3_access_key_id = ENV['S3_KEY']
    config.s3_secret_access_key = ENV['S3_SECRET']
    config.s3_bucket = ENV['S3_BUCKET']
  else
    config.storage = :file
  end
end

The first two lines (config.root and config.cache_dir) are there to work around Heroku's read-only filesystem, which should have nothing to do with your problem.

like image 164
Can Berk Güder Avatar answered Dec 19 '22 05:12

Can Berk Güder