Is there a way to read a file object using Fog and CarrierWave?
Asked Answered
C

2

12

I'm using CarrierWave and Fog to store attachments to an ActiveRecord model on S3. I can use them to generate the URL, but I can't seem to locate any method to actually read a file.

Is this something that the Fog gem will even do, or do I need to use the S3 gem for that?

Chloroform answered 27/8, 2012 at 15:52 Comment(0)
B
26

You can use the read method on the uploader object. If we have a model called MyModel which has a field called attachment which was added using CarrierWave's mount_uploader method, we can do the following:

obj = MyModel.first
contents = obj.attachment.read

This will get the actual contents of the file. Check out this doc.

Bogle answered 27/8, 2012 at 16:13 Comment(1)
No, this does not work on locally stored files with a local_root set.Chloroform
C
3

You just require open-uri

require 'open-uri'
file_contents = open(string_url) {|f| f.read }
Collegiate answered 27/8, 2012 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.