How to Get the Real File from S3 Using Carrierwave

How to get the real file from S3 using CarrierWave

Calling @document.file.read will get you the contents of the file from S3 in Carrierwave.

Downloading an image from S3 using carrierwave (without open)

I can suggest you use JavaScript to download a data(file/image) anything.

You can do ajax call to server to get an url.

#removed: target: '_self' no need it anymore
#added: remote: true
<%= link_to download_media_partnership_path(@partner, m: m.id), remote: true, data: {disable_with: "<i class='fa fa-spinner fa-spin media-icon'></i>"}
<i class="fa fa-download media-icon download" id=""></i>
<% end %>

def download_media
@media = TeamMedia.find(params[:m])
@url = @media.attachment.url

respond_to do |format|
format.js {}
end
end

Check this Link and implement into your javascript assets.

Do not forget to create download_media.js.erb file.

#download_media.js.erb

downloadFile("<%= @url %>");

Download a Carrierwave upload from S3

Thanks apneadiving. It was as easy as:

image = MiniMagick::Image::open(card.image.to_s)
image.write(somepath)

Rails, Amazon S3 storage, CarrierWave-Direct and delayed_job - is this right?

You have a good understanding of the process you need. To throw one more layer of complexity at you---you should wrap all of it in rails new(er) ActiveJob. ActiveJob simply facilities background processing inside rails via the processor of your choosing (in your case DelayedJobs). Then, you can create Jobs via a rails generator:

 bin/rails g job process_this_thing

Active Jobs offers a few "rails way" of handling jobs...but, it also allows you to switch processors with less hassle.

So, you create a carrierwave uploader (see carrierwave docs). Then, attach that uploader to a model. For carrierwave_direct you need to disassociate the file field from your models form and move the file field to its own form (use the form url method provided by carrierwave-direct).

You can choose to upload the file, then save the record. Or, save the record and then process the file. The set-up process is significantly different depending on which you choose.

Carrierwave and carrierwave-direct know where to save the file based on the fog credentials you put in the carrierwave initializer and by using the store_dir path, if set, in the uploader.

Carrierwave provides the uploader, which define versions, etc. Carrierwave_direct facilities uploading direct to your S3 bucket and processing versions in the background. Active Jobs, via DelayedJobs, provides the background processing. Fog is the link between carrierwave and your S3 bucket.

You should add a boolean flag to your model that is set to true when carrierwave_direct uploads your image and then set to false when the job finishing processing the versions. That way, instead of a broken link (while the job is running and not yet complete) your view will show something like 'this thing is still processing...'.

RailsCast is the perfect resource for completing this task. Check this out: https://www.youtube.com/watch?v=5MJ55_bu_jM

Carrierwave & Amazon S3 file downloading/uploading

Looks like you want to upload to S3, but have not-public URLs. Instead of downloading the file from S3 and using send_file, you can redirect the user to the S3 authenticated URL. This URL will expire and only be valid for a little while (for the user to download).

Check out this thread: http://groups.google.com/group/carrierwave/browse_thread/thread/2f727c77864ac923

Since you're already setting fog_public to false, do you get an authenticated (i.e. signed) url when calling resource.upload_url

How do I open a CSV file that I uploaded with Carrierwave and Fog to Amazon S3?

I tried Jared Beck's idea and it's working now, basically I added this condition to my BG job:

if Rails.env.production? || Rails.env.staging?
url = cbf.billing_file_name.url
cbf.billing_file_name.download!(url)
end

So the final code looks like this:

def self.perform(client_billing_file_id, email)
cbf = ClientBillingFile.find(client_billing_file_id)
if Rails.env.production? || Rails.env.staging?
url = cbf.billing_file_name.url
cbf.billing_file_name.download!(url)
end
filepath = cbf.billing_file_name.current_path
csv_file = CSV.read(filepath, headers: true)
.
.
.
end


Related Topics



Leave a reply



Submit