Carrierwave / Fog / S3 "is not a recognized storage provider" - ruby-on-rails

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.

Related

uninitialized constant CarrierWave::Storage::Fog with Google Cloud Storage

I have seen lots of other people having similar problems to me but none of the listed solutions apply so I am hoping this awesome community can help me out.
I am trying to use the sitemap_generator gem but I host with Heroku so I am trying to follow their documentation here to use Carrierwave to upload the sitemaps to Google Cloud Storage. I am already using Google Cloud to upload my images with all works fine so I was hoping it would be straightforward however the files are not being uploaded. The documentation says you need to add:
config.storage = :fog
To your carrierwave config file however whenever I add it, I get the following error:
gems/carrierwave-49fdad1ec6ca/lib/carrierwave/uploader/configuration.rb:75:in `eval': uninitialized constant CarrierWave::Storage::Fog (NameError)
My Carrierwave config looks like this:
CarrierWave.configure do |config|
config.cache_dir = "#{Rails.root}/tmp/"
config.storage = :fog
config.fog_credentials = {
:provider => 'Google'
}
config.fog_directory = 'bucket-name'
config.asset_host = 'https://domain.storage.googleapis.com'
end
(fog_directory and asset_host are replaced with dummy values)
And in my gem file I have:
gem 'fog'
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
I have seen a lot of people using AWS with the same error but their solution is to change to use fog gem instead of fog-aws (which I am already doing) and require fog/aws. I have tested this like so:
gem 'fog', require: 'fog/google'
But still have the same issue.
Can anyone suggest what I can do to try and resolve this? Any help would be greatly appreciated!!
Many thanks
I got the same error using fog-aws for Amazon S3. It seems this error occurs if storage is fog, doesn't matter it's amazon s3 or google cloud.
I used carrierwave gem to upload user profile picture so I solved this by moving storage configuration after credentials configuration as below:
CarrierWave.configure do |config|
if Rails.env.staging? || Rails.env.production?
config.fog_provider = 'fog/aws'
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: ENV['AWS_REGION']
}
config.storage = :fog
config.fog_directory = ENV['S3_BUCKET']
config.fog_public = true
config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" }
else
config.storage = :file
config.enable_processing = Rails.env.development?
end
end
Another solution was to add require 'carrierwave/storage/fog' at the top, in above file. For me the file was carrier_wave.rb under initializers directory.
Adding links where I found above solutions.
Medium Carrierwave Fog
I know this post is 7 months old but i just wasted 36h trying to implement that same gem... no luck. It seems that documentation is slightly outdated.
If you ask me, fog-google is unnecessary at this point!
Try this gem for CarrierWaveUploader integration with google.
carrierwave-google-storage github

Upload files to amazon aws3 rails with carrierwave error hostname does not match the server certificate

I followed this tutorial:
http://lifesforlearning.com/uploading-images-with-carrierwave-to-s3-on-rails/
I had working carrierwave uploader which was storing files to disk space
What I did step by step:
1)added fog gem and run bundle install and bundle update
2)in config/initializers I created r3.rb file with this:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'mykey',
:aws_secret_access_key => 'mysecretkey',
:region => 'us-west-2' # Change this for different AWS region.
}
config.fog_directory = "bucket-main"
end
I ran rails s and tried to save some photo. But as you can see on the picture my bucket is empty.So they must be stored to my disk.
What do I do now?
Update I changed storage to fog.
Here is my photouploader class code:
# encoding: utf-8
class PhotoUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
And now I get this error:
hostname "bucket-main.bucket-main.s3-us-west-1.amazonaws.com" does not
match the server certificate (OpenSSL::SSL::SSLError)
i eventually solved my problem by updating
bundle update fog
and
bundle update carrierwave
Try adding path_style to your config and the fog_directory
config.fog_credentials = {
...
:path_style => true
}
config.fog_directory = 'bucket-main'
I just spent a few hours tracking down the cause of this error, which I was also getting:
hostname "bucket-main.bucket-main.s3-us-west-1.amazonaws.com" does not match the server certificate (OpenSSL::SSL::SSLError)
The odd thing is how the bucket name is repeated twice in the hostname. It turned out I had configured the wrong region name. Notice in your config.fog_credentials you have
:region => 'us-west-2'
...but the hostname in the exception has s3-us-west-1? If your bucket is in one AWS region, but you configure a different region in your Fog credentials, Fog will try to follow a redirect from AWS, and somehow the bucket name gets doubled up in this situation. Fog produces a warning about the redirect, but Carrierwave ends up hiding this from you.
Set :region in your Fog credentials to where the bucket actually is in AWS, and the does not match the server certificate exception will stop happening.

Carrierwave + fog + aws s3 and rails in production

I have just migrated from paperclip to carrierwave and managed to successfully get uploading to S3 to work locally on my machine, but after deploying the Rails application to my server (which uses Ubuntu, Passenger with nginx, was a mission getting it to work), and when i try to upload an image, it tries to save it to public/uploads/... which comes up with a permission denied error, I have looked and searched everywhere to find out why its not working, and have found nothing.
My Uploader file:
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::Compatibility::Paperclip
storage :fog
def store_dir
"/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
fog.rb
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
provider: 'AWS', # required
aws_access_key_id: '*******', # required
aws_secret_access_key: '********', # required
region: 'ap-southeast-2', # optional, defaults to 'us-east-1'
}
config.fog_directory = 'publicrant' # required
# config.fog_public = false
config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" } # optional, defaults to {}
end
Ok so after hours of googling and failing miserably at finding a solution, turns out, in a production environment it did need to put the file temporary in uploads/tmp before it pushes it to S3 Bucket
Seems like you doesn't have read permissions for other users (o+r). Check it use command:
namei -lm <absolute path to your current/public>
and grant read permissions:
chmod o+r <directory>
In your case I think it will be /home/<user> directory.

Rspec and Carrierwave. When changing config.storage to file for testing, I get an ArgumentError is not a recognized storage provider exception

I have two carrierwave uploaders in my application. ImageUploader is for uploading locally and ImageRemoteUploader for uploading to Amazon S3 storage using fog. ImageUploader has storage set to :file and ImageRemoteUploader has storage set to :fog. This setup works fine, but when I start to set up my rspec tests, things change.
The problem arises when I change the ImageRemoteUploader to use :file storage during testing. I do this in my fog initialization file. The file,
/config/initializers/fog.rb, looks like:
CarrierWave.configure do |config|
if Rails.env.test?
config.storage = :file
config.enable_processing = false
else
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'XXXXXXXX', # required
:aws_secret_access_key => 'XXXXXX', # required
:region => 'XXXX' # optional, defaults to 'us-east-1'
}
config.fog_directory = 'xxx' # required
config.fog_public = true
end
end
When I do this, I get an ArgumentError is not a recognized storage provider carrierwave exception. When I use the fog credentials (I don't set config.storage to :file), the test works as expected.
Carrierwave 0.7.1, Rails 3.2.8, Ruby 1.9.3, Rspec 2.10
Thanks.
I'd try moving the config.storage and config.enable_processing lines into lib/initializers/carrierwave.rb, as recommended in the Carrierwave docs.
Fog also has its own mocking support, which is enabled by running Fog.mock! before the examples. This might be a better approach.

Carrierwave/Fog - Argument error, provider not recognized

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.

Resources