加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:17:39 所属栏目:百科 来源:网络整理
导读:CarrierWave拥有令人惊叹的文档,直到你需要在没有模型的情况下完成它! 我设置了上传器和雾设置,并且在模型上使用已安装的上传器时它们都能正常工作,但现在我想在没有模型的情况下完成它. 我有这个: uploader = CsvUploader.new something = uploader.store
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在这里默默地失败了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读