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
Related
I am trying to generate a csv file in a rake task and...
Email it
Upload it to Amazon s3.
Here is the task.
desc "This task is called by the Heroku scheduler add-on"
require 'csv'
task :send_report => :environment do
file = Baseline.to_csv
ReportMailer.database_report(file).deliver_now
Report.create!(:data => file)
end
The generation of the csv file and attachment to the email works fine (not shown). Its the carrierwave upload that isn't working. Please note that I have other uploaders for other models and they work fine so my bucket settings are correct.
Here are the other files.
class Report < ActiveRecord::Base
mount_uploader :data, ReportUploader
end
and
class ReportUploader < 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 csv xls)
end
end
I have tried various permutations such as store! with not luck. I should add that if I look at the database, the new report is being created (and the data attribute is "nil", with no upload in sight)
Thanks
I am using carrierwave to upload and process a file. After the process is done I want to be able to delete the original file. I put
after :store, :unlink_original
def unlink_original(file)
return unless delete_original_file
file.delete if version_name.blank?
end
in my uploader. I also added
class CarrierWave::Uploader::Base
add_config :delete_original_file
end
CarrierWave.configure do |config|
config.delete_original_file = true
end
to my config/initializers/carrierwave.rb:
The original file is still in the directory along with the processed file. How would I go about deleting this file the right way after carrierwave is done with it?
I used the paperclip gem to add an upload function for a pdf file. The upload works, once uploaded, the file is saved to location
/system/uploaded_files/sources/000/000/006/original/file.pdf
now I am trying to access this using the pdf reader
require 'pdf-reader'
require 'open-uri'
reader = PDF::Reader.new(open('/system/uploaded_files/sources/000/000/006/original/file.pdf'))
I get the following error
Errno::ENOENT: No such file or directory - /system/uploaded_files/sources/000/000/006/original/file.pdf
How do I access this file using the pdf-reader gem?
Let's say your model is called PDF and the asset is saved as file.
class Pdf < ActiveRecord::Base
has_attached_file :file
end
Now try this:
p = Pdf.find(params[:id]) # get the object
reader = PDF::Reader.new(open(p.file.url))
I want to upload multiple images to cloudinary by association of a carrierwave active record. How can I do this in a seeds file, using an array of remote urls?
Ive read countless articles how to use carrierwave/cloudinary helper tags to target an image upload within an html form, but nothing on doing this directly within code, any ideas?
That's not so hard to do at all.
So what I did:
# Gemfile
gem 'cloudinary'
gem 'carrierwave'
#config/cloudinary.yml
development:
cloud_name: "carrierwave-example"
api_key: "YOUR CLOUDINARY CREDENTIALS"
api_secret: "YOUR CLOUDINARY CREDENTIALS"
# in your uploader
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave #include cloudinary lib
# storage :file - comment this out
# def store_dir - comment this too
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
# end
end
# You model, that uses uploader
class Picture < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
After writting this simple staff you can create Pictures, that will store images in clodinary like this:
Picture.create(image: "/path/to/image")
or if you have remote links of images, U just itterate through them
["http://image1.jpg","http://image2.jpg","http://image3.jpg"].each do |link|
Picture.create(remote_image_url: link)
end
Just remember to use remote_#{your_column_name}_url if you have remote link
Here's my old code to sends a file to the browser:
def show
send_file File.join(Rails.root, 'tmp', 'price.xls')
end
But recently I've found out that tmp folder can't be used as a persistent storage on Heroku, so I decided to move the file to AWS S3.
That's what I've got so far:
def show
uploader = PriceUploader.new
uploader.retrieve_from_store!('price.xls')
end
Now, how do I send the file to the browser?
upd
I itentionally didn't mount the uploader
Figured it out.
def show
uploader = PriceUploader.new
uploader.retrieve_from_store!('price.xls')
uploader.cache_stored_file!
send_file uploader.file.path
end
In my case
# find uploader ...
send_file(uploader.path,
filename: uploader.filename,
type: "application/<some-type>")