howto: Basic setup of carrierwave [Heroku and S3]

2019-03-29 12:07发布

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

2条回答
爷的心禁止访问
2楼-- · 2019-03-29 12:11

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.

查看更多
干净又极端
3楼-- · 2019-03-29 12:13

One issue might be

config.fog_host       = 'http://www.example.com'

If you use this setting, you probably need a real host there. Supposedly you can comment that line out, although I've seen it highly recommended.

And I assume you set your ENV variables on Heroku?

heroku config:add S3_K=[my_s3_key] S3_SCRT=[my_s3_secret] S3_RG=[us-east-1]S3_BUCKET=[my_bucket_name]

I used pretty much the same setup. Mine didn't work, but I think I got a step or two further:

Missing required arguments: aws_access_key_id, aws_secret_access_key
查看更多
登录 后发表回答