Upload a file to S3 using CarrierWave without a model, is this possible?

CarrierWave has amazing documentation until you need to do this without a model!

I have bootloader and fog settings set, and all of them work fine when using a mounted bootloader on a model, but now I want to do this without a model.

I have it:

uploader = CsvUploader.new something = uploader.store!(File.read(file_path)) uploader.retrieve_from_store!(self.file_name) 

When I call .store! the code starts right away, which is strange, since it takes a few seconds to download a file?

Then after calling .retrieve_from_store! The uploader object has all the correct S3 data, such as full URLs and more.

However, the call:

 uploader.file.exists? 

returns false. And looking at the s3 urls returns an error not found from s3.

So what am I doing wrong? To repeat, it works during installation, so I don't think these are my fog settings.

My bootloader:

 class CsvUploader < CarrierWave::Uploader::Base # 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: include CarrierWave::MimeTypes process :set_content_type def store_dir "uploads/public/extranet_csvs" end def cache_dir "/tmp/uploads" 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(csv) end end 
+8
ruby-on-rails-3 amazon-s3 carrierwave fog
source share
1 answer

I think you want File.open instead of File.read . The latter returns a raw string that CarrierWave does not know how to store .

 uploader = CsvUploader.new File.open(file_path) do |file| something = uploader.store!(file) end uploader.retrieve_from_store!(self.file_name) 

In the docs this would probably be clearer, but I confirmed this by setting the specifications . Bummer that CarrierWave is failing here.

+13
source share

All Articles