Carrierwave/Fog - Argument error, provider not recognized - ruby-on-rails

I'm using Carrierwave 0.5.3 and Fog to upload images to Amazon-S3.
The setup works smoothly when running locally, no errors.
But when running on Heroku, uploads fail with this message:
2011-03-31T12:53:46-07:00 app[web.1]: ArgumentError ( is not a recognized storage provider):
2011-03-31T12:53:46-07:00 app[web.1]: app/controllers/useditems_controller.rb:36:in `create'
I've got an initializer:
# /config/initializers/fog.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'secret',
:aws_secret_access_key => 'also secret',
:region => 'eu-west-1'
}
config.fog_directory = 'jabberwocky'
end
And an uploader:
# /app/uploaders/image_uploader.rb
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support:
include CarrierWave::RMagick
# Choose what kind of storage to use for this uploader:
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"useditems"
end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_limit => [220, 2000]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
end
I've traced the error message to Fog, and it seems that Fog, under Heroku, isn't getting the config info from the initializer. :provider is somehow empty. But I'm stumped as to how to fix it.
Any help would be much appreciated.
I'm using:
rails 3.0.4
heroku 1.19.1
fog 0.7.1
ruby 1.9.2 under rvm

The error was due to the fact that I had mistakenly added the initializer to the .gitignore-file. Thus, it was never uploaded to Heroku.

Adding this for completeness...
After smashing my head against the wall for hours with this error message, I found out that I had this line at the beginning of the carrierwave initializer:
if Rails.env.test?
...
So the initializer was only considered in the test environment. After removing it, everything worked as expected.

Related

How can use Cloudinary just on production and use my file storage on development (Using Carrierwave Rails)?

I have an application Rails deployed on Heroku. I'm using the adds-on Cloudinary and the gem Carrierwave to upload images.
Following the Cloudinary documentation I have could configure the uploading images on Production successfully but in Development, it is also uploading to the cloud. My issue here is that I would like to use the file storage in my local machine instead of upload images to Cloudinary (just upload to Cloudinary in production).
I have tried to fix that using storage :file if Rails.env == "development" but didn't work. Any idea to solve it?
My uploader is:
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
include CarrierWave::MiniMagick
# storage :file if Rails.env == "development"
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process resize_to_fit: [50, 50]
end
version :card do
process resize_to_fit: [250, 250]
end
end
Thanks!
I also remember not being able to handle this situation in a past project. As we were using AWS S3 we could differentiate assets from deve and prod by storing them in a different bucket, however in Cloudinary this seems to be not possible.
Maybe this setup we used for test environment might help you in your dev environment.
Place this code in config/initializers/carrierwave.rb
if Rails.env.test? || Rails.env.development?
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = false
config.asset_host = ActionController::Base.asset_host
end
ImageUploader
CarrierWave::Uploader::Base.descendants.each do |klass|
next if klass.anonymous?
klass.class_eval do
# Need to set the storage here otherwise the class
# overrides it in the uploader definition
storage :file
def cache_dir
"#{Rails.root}/spec/support/uploads/tmp"
end
def store_dir
"#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
end
else
CarrierWave.configure do |config|
# Production setup
end
end
Hope this helps you. It worked for us.

Cannot access the pictures uploaded by Carrierwave versions in production environment

This is my Uploader:
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process :resize_to_fit => [nil, 600]
version :thumb do
process :resize_to_fill => [150,150]
end
# Choose what kind of storage to use for this uploader:
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
if original_filename
#name ||= Digest::MD5.hexdigest(current_path)
"#{#name}.#{file.extension}"
end
end
end
In production.rb, I set config.serve_static_assets = false.
And I then deployed this project on a production server(Nginx + Passenger) using Capistrano. When I upload a picture, it will generate 2 copies under the /home/deploy/Gallary/current/public/uploads/picture/photo/ dir, just as following shows:
And I can access the first one through browser(because this one is the default file which Carrierwave generated), while the second one(generated by version :thumb) threw a exception just like this:
ActionController::RoutingError (No route matches [GET] "/uploads/picture/photo/49/thumb_6d9596c7449d3714eadb74b9c71beec2.jpg")
Actually this file thumb_6d9596c7449d3714eadb74b9c71beec2.jpg does exist in the dic right there.
So, what's wrong? And what should I do?
I had a similar problem. Found the answer in another thread.
So try this:
Server unable to find public folder in rails 3 production environment
Worked for me

carrierwave image upload to s3 "hostname does not match certificate error"

I first got carrierwave working by following the directions from this railscast:
http://railscasts.com/episodes/253-carrierwave-file-uploads
Then I hooked up s3 by following the directions here:
http://railgaadi.wordpress.com/2012/06/03/saving-files-in-amazon-s3-using-carrierwave-and-fog-gem/
My image_uploader.rb file:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :fog
def store_dir
"development/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :iphone do
process :resize_to_limit => [320, 160]
end
end
And my fog.rb file:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'xxx', # required
:aws_secret_access_key => 'xxx', # required
}
config.fog_directory = 'goodlife.carrierwave' # required
end
This is the error I'm getting:
hostname "goodlife.carrierwave.s3-us-west-1.amazonaws.com" does not match the server certificate
Any advice? Thanks!
Adding :path_style => true to config.fog_credentials worked for me. I learned it from an answer to
Amazon S3 - hostname does not match the server certificate (OpenSSL::SSL::SSLError) + rails.
Is goodlife.carrierwave the name of your bucket?
Edit:
Remove the period from your bucket name. That should fix it.
From Amazon:
If you want to access a bucket by using a virtual hosted-style
request, for example, http://mybucket.s3.amazonaws.com over SSL, the
bucket name cannot include a period (.).

403 Error When Uploading Image on Heroku to S3 with Carrierwave and Fog

Everything is working as expected locally. Once I push to heroku I can no longer upload images.
The error code I get from heroku logs is:
Excon::Errors::Forbidden (Expected(200) <=> Actual(403 Forbidden)
The XML response contains: <Code>AccessDenied</Code><Message>Access Denied</Message>
My fog.rb:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => ENV["ACCESS_KEY_ID"],
:aws_secret_access_key => ENV["SECRET_ACCESS_KEY"]
#:region => 'eu-west-1'
}
#Required for Heroku
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = ENV["BUCKET_NAME"]
end
My Uploader:
class ImageUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
Heroku has the correct environment variables - I used the figaro gem. I also set them manually after I got the 403 the first few times to make sure figaro had no errors.
I thought this may be a problem with the region but my bucket is US and carrierwave documentation says the default is us-east-1
What is causing the issue on Heroku but not locally?
Forbidden may mean an issue with the configured directory (rather than the other credentials). Are you using the same BUCKET_NAME value both locally and on Heroku? I know I've certainly tried to use things with a different bucket that I had not yet created (which might also given this error). So checking the value is what you expect (and that the bucket already exists) are a couple good starting points. Certainly happy to discuss and continue helping if that doesn't solve it for you though.

Carrierwave / Fog / S3 "is not a recognized storage provider"

I have a Rails app that is using Carrierwave for file uploads. It has been working fine but I want to start using Amazon S3 for my image storage. I am getting this error:
ArgumentError ( is not a recognized storage provider):
app/controllers/salons_controller.rb:52:in `update'
I have made sure I have the latest gems for Carrierwave and Fog. This is in my Gemfile:
gem 'carrierwave'
gem 'aws-sdk'
gem 'fog'
fog.rb looks like:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'MYACCESSKEY',
:aws_secret_access_key => 'MYSECRETKACCESSKEY',
:region => 'us-east-1'
}
config.fog_directory = 'andrunix'
config.fog_public = true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
The Uploader class looks like:
class SalonImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :fog
def store_dir
# "andrunix" is the bucket name on S3
"andrunix/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
If I change the storage back to 'file', it works fine. Setting storage to 'fog' generates this error.
OK, I'm an idiot. :)
At some point, I don't know where, I added a fog.rb file with my CarrierWave configuration to the lib/carrierwave/storage directory. I got desperate, paid for a Railscasts subscription so I could watch episode #383 (http://railscasts.com/episodes/383-uploading-to-amazon-s3?autoplay=true) and at 3:02 I found the error of my ways. The Carrierwave configuration needed to be placed in config/initializers/carrierwave.rb.
I don't know where I got this other location but once I moved the config to the proper location, everything is good.
I just ran into the same problem, and people must be aware that any typo in the config file : "config/initializers/carrierwave.rb", leads to that error.

Resources