ruby-on-rails-3 – 使用没有型号的CarrierWave将文件上传到S3,是否可能?

CarrierWave拥有令人惊叹的文档,直到你需要在没有模型的情况下完成它!

我设置了上传器和雾设置,并且在模型上使用已安装的上传器时它们都能正常工作,但现在我想在没有模型的情况下完成它.

我有这个:

uploader = CsvUploader.new
 something = uploader.store!(File.read(file_path))
 uploader.retrieve_from_store!(self.file_name)

当我打电话给.store!代码立即运行,这很奇怪,因为上传文件需要几秒钟?

然后我打电话给.retrieve_from_store!上传器对象具有所有正确的S3信息,如完整的URL和内容.

但是,打电话:

uploader.file.exists?

返回false.浏览s3网址会从s3返回一个未找到密钥的错误.

那么,我做错了什么?重申一下,它在安装时有效,所以我不认为这是我的雾设置.

我的上传者:

class CsvUploader < CarrierWave::Uploader::Base
  # Choose what kind of storage to use for this uploader:
  storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  include CarrierWave::MimeTypes
  process :set_content_type

  def store_dir
    "uploads/public/extranet_csvs"
  end

  def cache_dir
    "/tmp/uploads"
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    %w(csv)
  end
end

解决方法

我想你想要File.open而不是File.read.后者返回一个原始字符串,CarrierWave不知道如何存储.
uploader = CsvUploader.new
File.open(file_path) do |file|
  something = uploader.store!(file)
end
uploader.retrieve_from_store!(self.file_name)

这可能在文档中更清晰,但我通过检查specs确认了它.Bummer CarrierWave在这里默默地失败了.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...