【问题标题】:Is there a way to read a file object using Fog and CarrierWave?有没有办法使用 Fog 和 CarrierWave 读取文件对象?
【发布时间】:2012-08-27 15:52:28
【问题描述】:

我正在使用 CarrierWave 和 Fog 将附件存储到 S3 上的 ActiveRecord 模型。我可以使用它们来生成 URL,但我似乎找不到任何方法来实际读取文件。

这是 Fog gem 甚至会做的事情,还是我需要使用 S3 gem 来做这件事?

【问题讨论】:

    标签: ruby-on-rails amazon-s3 ruby-on-rails-3.2 carrierwave fog


    【解决方案1】:

    您可以在上传器对象上使用read 方法。如果我们有一个名为 MyModel 的模型,它有一个名为 attachment 的字段,该字段是使用 CarrierWave 的 mount_uploader 方法添加的,我们可以执行以下操作:

    obj = MyModel.first
    contents = obj.attachment.read
    

    这将获取文件的实际内容。查看this doc

    【讨论】:

    • 不,这适用于具有 local_root 集的本地存储文件。
    【解决方案2】:

    你只需要open-uri

    require 'open-uri'
    file_contents = open(string_url) {|f| f.read }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 2012-09-25
      相关资源
      最近更新 更多