I'd like to download an image that was uploaded to S3 using carrierwave. The image is on the Card model, mounted as an uploader. I saw this answer, but had trouble getting that solution to work. My code is:
#download image from S3
uploader = card.image #image is the mounted uploader
uploader.retrieve_from_store!(File.basename(card.image.url))
uploader.cache_stored_file!
that last line throws: "... caused an exception (undefined method `body' for nil:NilClass)..."
My carrierwave config looks like:
#config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.storage = :fog
config.cache_dir = "#{Rails.root}/tmp/upload"
...
end
Thanks apneadiving. It was as easy as:
image = MiniMagick::Image::open(card.image.to_s)
image.write(somepath)
I have tried this in Rails 5 to download file from AWS S3.
def download
image = card.image
# validate existing image from AWS S3
if image.try(:file).exists?
data = open(image.url)
send_data data.read, type: data.content_type, x_sendfile: true
end
end
I hope help everyone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With