如何使用CarrierWave从S3获取真实文件

Lui*_* E. 5 ruby amazon-s3 ruby-on-rails-3 carrierwave

我有一个应用程序,它读取文件的内容并为其编制索引.我将它们存储在磁盘本身,但现在我正在使用Amazon S3,因此以下方法不再起作用.

它是这样的:

def perform(docId)
    @document = Document.find(docId)
    if @document.file?

      #You should't create a new version
      @document.versionless do |doc|
        @document.file_content = Cloudoc::Extractor.new.extract(@document.file.file)
        @document.save
      end

    end
  end
Run Code Online (Sandbox Code Playgroud)

@document.file返回FileUploader,并doc.file.file返回CarrierWave::Storage::Fog::File该类.

我如何获得真实文件?

Zac*_*ker 11

调用@document.file.read将从Carrierwave中的S3获取文件的内容.

  • @document.file.read 可能更好,因为无论使用什么文件存储,它都可以工作,即它可以与本地文件系统和 S3 一起使用。 (2认同)