【问题标题】:Download a Carrierwave upload from S3从 S3 下载 Carrierwave 上传
【发布时间】:2012-12-20 05:08:48
【问题描述】:

我想下载一张使用carrierwave 上传到S3 的图片。图像在卡片模型上,作为上传器安装。我看到了this answer,但无法让该解决方案发挥作用。我的代码是:

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

最后一行抛出:“...导致异常(nil:NilClass 的未定义方法 `body')...”

我的carrierwave配置如下:

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

【问题讨论】:

  • 你可以这样做:file = open card.image.url

标签: ruby-on-rails ruby amazon-s3 download carrierwave


【解决方案1】:

谢谢apneadiving。这很简单:

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

【讨论】:

  • 哇,不知道imagemagick可以这样下载和保存文件,只有URL,不错!
  • 您能否进一步详细说明这是如何工作的?这段代码会去哪里?
  • 我在从 S3 下载图像的自定义后台作业中获得了该代码。但它可能在任何地方。
  • @joseph.hainline 我想给用户一个下载选项,我怎么能在这里指定你写 somepath 的路径,因为我不知道用户的系统
【解决方案2】:

我已经在 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

希望对大家有所帮助。

【讨论】:

    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多