I am using CarrierWave for image uploads on a rails-api application which in turn is consumed by a backbone.js client app. I notice that when there is no default image available it returns /assets/default.png. It in turn has to be http://dev-server:3000/assets/default.png. Here are my configuration settings:
# config/environments/development.rb
CarrierWave.configure do |config|
config.asset_host = "http://dev-server:3000"
end
# Image Uploader
....
def default_url
"/assets/default.png"
end
Where am I going wrong?
I'm using Rails 5 API and Carrierwave too.
A lucky guess got it working for me.
I have a file inside app/uploaders/image_uploader.rb.
The asset_host configuration is set inside this file (at least it works for me):
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
...
def asset_host
return "http://localhost:3000"
end
end
Hope this works for anyone else in the future having this problem.
[Edit (updated answer)]
Updating my answer to setup asset_host in rails config.
Rails.application.configure do
.
.
config.asset_host = 'http://dev-server:3000'
end
Then you can use asset_url method or image_url method of the helper. Since this is an image, I would recommend placing the image in app/assets/images folder and use image_url.
ActionController::Base.helpers.image_url("default.png")
This will give you the following URL:
http://dev-server:3000/images/default.png
You can try it in the console.
[Old Answer]
Looking at Carrierwave Documentation, it seems like your default_url method should look like this (Carrierwave does not automatically perpend asset_host to the default url):
def default_url
ActionController::Base.helpers.asset_path("default.png")
end
I am assuming that asset_host is setup properly in your Rails configuration. If not, please do so.
I would recommend some of your ideas:
I: Put Host in your environment z.B. development.rb
config.asset_host = 'http://localhost:3000'
II: Create file in config/initializers/carrierwave.rb
# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.storage = :file
config.asset_host = ActionController::Base.asset_host
end
III: Edit your uploader to:
def default_url
"#{asset_host}/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
IV: RESTART your server
I solved the problem by looking into the code of carrierwave. This is what I ended up doing:
def default_url
"#{asset_host}#{ActionController::Base.helpers.asset_path("default.png")}"
end
Also make sure that you include the asset_host configuration in your respective environment files.
Related
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.
I am struggling to access files on S3 with Carrierwave.
In my uploader file doc_uploader.rb I have the following code
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
to uplooad "doc" model defined as follow
class Doc < ActiveRecord::Base
belongs_to :user
mount_uploader :doc, DocUploader
end
To access the uploaded file I have the following line of code in a controller
#doc = current_user.docs.order("created_at").last #last file uploaded by user
io = open("#{Rails.root}/public" + #doc.doc.url)
Everything works perfectly locally. Now I want to move my file to S3 in the uploader I use fog and replace
storage :file
by
storage :fog
I adjust my config file carrierwave.rb and uploading works perfectly. However, to access the file I try to use
#doc = current_user.docs.order("created_at").last
io = open("#{#doc.doc.url}")
and I get the following error
No such file or directory # rb_sysopen - /uploads/doc/doc/11/the_uploaded_file.pdf
Could anyone give me the right syntax to access the file on S3 please? Thanks.
When accessing the asset through the console, it gives you only the path, you might need to append the protocol & host to the #doc.doc.url, something like:
io = open("http://example.com#{#doc.doc.url}")
Or you can set the asset url on the environment you need to, but this is not really necessary:
config.asset_host = 'http://example.com'
This only applies if you are using the console, on any web view this will not apply, carrierwave seems to handle it
I am building a rails app with carrierwave and fog for attachment storage. In my test environment, I am using fog local storage.
I am looking for a way to get the full attachment path with this configuration.
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'Local',
local_root: '/Users/me/fog',
endpoint: '/Users/me/fog',
}
config.fog_directory = 'test.myapp.com
config.fog_public = false
config.fog_attributes = { 'Cache-Control' => 'max-age=315576000' }
end
When I use any other storage options (like AWS S3), I can get the full url to an attachment just by doing my_object.my_attachment_url or my_object.my_attachment.path.
However, when using Local storage, I only get a relative path to my configuration options like my_object/my_attachment/1/test.jpg.
Is there any way through carrierwave or fog to get the full path to this local file?
For my example, the output I am looking for would be: /Users/me/fog/test.myapp.com/my_object/my_attachment/1/test.jpg
For me, the answer was modifying to carrierwave uploader class.
I had
def store_dir
"#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
which worked fine for AWS S3 as all the S3 specific info was inserted before this string. However, to get this to work with fog Local as well, I added:
if Rails.env.test?
def base_path
"#{File.expand_path(CONFIG.fog_local_root)}/#{CONFIG.fog_directory}/"
end
else
def base_path
''
end
end
So this seems like it should be quite easy... everyone is saying just to use config.asset_host. When I set that though, all the links inside my app still point to S3.
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => AWS_ACCESS_KEY_ID,
:aws_secret_access_key => AWS_SECRET_ACCESS_KEY,
:region => 'us-east-1'
}
config.fog_authenticated_url_expiration = 3.hours
config.asset_host = "http://xyz123.cloudfront.net"
config.fog_directory = S3_BUCKET_NAME
config.fog_public = false
config.fog_attributes = {
'Cache-Control' => "max-age=#{1.year.to_i}"
}
end
here is how I call my files...
image_tag book.attachments.first.filename.file.authenticated_url(:thumb175)
It looks to me like public_url prepends the proper host, but it takes 0 arguments... so how am I supposed to pass the proper response-content-disposition and response-content-type and the link expire time?
I had the same problem and spent far too long figuring out the answer! It turns out that when you set fog_public = false CarrierWave will ignore config.asset_host. You can demo this by setting config.fog_public = true: your URLs will now be CloudFront URLs rather than S3 URLs. This issue has been raised previously:
https://github.com/carrierwaveuploader/carrierwave/issues/1158
https://github.com/carrierwaveuploader/carrierwave/issues/1215
In a recent project I was happy using CarrierWave to handle uploads to S3, but wanted it to return a signed CloudFront URL when using Model.attribute_url. I came up with the following (admittedly ugly) workaround that I hope others can benefit from or improve upon:
Add the 'cloudfront-signer' gem to your project and configure it per the instructions. Then add the following override of /lib/carrierwave/uploader/url.rb in a new file in config/initializers (note the multiple insertions of AWS::CF::Signer.sign_url):
module CarrierWave
module Uploader
module Url
extend ActiveSupport::Concern
include CarrierWave::Uploader::Configuration
include CarrierWave::Utilities::Uri
##
# === Parameters
#
# [Hash] optional, the query params (only AWS)
#
# === Returns
#
# [String] the location where this file is accessible via a url
#
def url(options = {})
if file.respond_to?(:url) and not file.url.blank?
file.method(:url).arity == 0 ? AWS::CF::Signer.sign_url(file.url) : AWS::CF::Signer.sign_url(file.url(options))
elsif file.respond_to?(:path)
path = encode_path(file.path.gsub(File.expand_path(root), ''))
if host = asset_host
if host.respond_to? :call
AWS::CF::Signer.sign_url("#{host.call(file)}#{path}")
else
AWS::CF::Signer.sign_url("#{host}#{path}")
end
else
AWS::CF::Signer.sign_url((base_path || "") + path)
end
end
end
end # Url
end # Uploader
end # CarrierWave
Then override /lib/carrierwave/storage/fog.rb by adding the following to the bottom of the same file:
require "fog"
module CarrierWave
module Storage
class Fog < Abstract
class File
include CarrierWave::Utilities::Uri
def url
# Delete 'if statement' related to fog_public
public_url
end
end
end
end
end
Lastly, in config/initializers/carrierwave.rb:
config.asset_host = "http://d12345678.cloudfront.net"
config.fog_public = false
That's it. You can now use Model.attribute_url and it will return a signed CloudFront URL to a private file uploaded by CarrierWave to your S3 bucket.
I think you found this for yourself, but public urls will not expire. If you want that you'll need to use authenticated urls. For public urls I think you can simply get the url and append whatever query params you would like, at least for now. If that works well for you, we can certainly see about patching things to do the right thing.
What happened to asset_host in rails 3?
Earlier I can put following code into development.rb and get all assets not present on development:
ActionController::Base.asset_host = proc do |source, request|
unless File.exist?(File.join(RAILS_ROOT, 'public', source.sub(/\?\d+$/, '')))
'http://example.com'
end
end
But in rails 3 there is no such method and google does not help me.
The asset_host config information goes into you environment files, which have changed format slightly:
// environments/production.rb
Infinity::Application.configure do
config.action_controller.asset_host = "http://assets.example.com"
end
I'm not sure this will let you override in the same way as your code, though.