从S3下载Carrierwave上传

我想使用carrierwave下载上传到S3的图像。 图像位于卡型号上,作为上传器安装。 我看到了这个答案 ,但是无法让这个解决方案起作用。 我的代码是:

#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! 

最后一行抛出:“…导致exception(未定义的方法`body’为nil:NilClass)……”

我的carrierwave配置如下:

 #config/initializers/carrierwave.rb CarrierWave.configure do |config| config.storage = :fog config.cache_dir = "#{Rails.root}/tmp/upload" ... end 

谢谢apneadiving 。 它很简单:

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

我在Rails 5中尝试过从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 

我希望能帮助大家。