Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a Carrierwave upload from S3

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
like image 988
joseph.hainline Avatar asked Jan 06 '13 05:01

joseph.hainline


2 Answers

Thanks apneadiving. It was as easy as:

image = MiniMagick::Image::open(card.image.to_s)
image.write(somepath)
like image 120
joseph.hainline Avatar answered Oct 05 '22 22:10

joseph.hainline


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.

like image 23
akbarbin Avatar answered Oct 06 '22 00:10

akbarbin