Download a Carrierwave upload from S3

自古美人都是妖i 提交于 2019-12-05 00:33:17

问题


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

回答1:


Thanks apneadiving. It was as easy as:

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



回答2:


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.



来源:https://stackoverflow.com/questions/14179553/download-a-carrierwave-upload-from-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!