上传文件中使用CarrierWave没有一个模型S3,这可能吗?这可、上传文件、模型、CarrierWave

2023-09-11 08:39:13 作者:青栀如初

CarrierWave有着惊人的文档,直到你需要做它没有一个模型!

我有我的上传和雾设置中设置了,他们都使用一个模型安装的上传时正常工作,但现在我想这样做,没有一个模型。

我有这样的:

 上传= CsvUploader.new
 一些= uploader.store!(File.read(FILE_PATH))
 uploader.retrieve_from_store!(self.file_name)
 

当我打电话.store!在code立即运行这是奇怪的,因为它应该需要几秒钟上传的文件?

再经过我打电话.retrieve_from_store!上载对象拥有所有正确的S3的信息,如完整的URL之类的东西。

不过,美其名曰:

  uploader.file.exists?
 

返回false。和浏览S3网址从S3返回未找到关键错误。

那么,我究竟做错了什么?要重申的是,它的工作原理在安装时,所以我不认为这是我的雾设置。

我的上传:

 类CsvUploader< CarrierWave ::上传::基地
  #选择使用此上传什么样的存储:
  存储:雾

  #覆盖的地方上传的文件将被存储在目录中。
  #这是一个合理的默认为是为了安装上传者:
  包括CarrierWave :: MIMETYPES
  过程:set_content_type

  高清store_dir
    上传/公/ extranet_csvs
  结束

  高清的cache_dir
    / tmp目录/上传
  结束

  #添加被允许上载的扩展的白名单。
  #对于图像,你可以使用这样的事情:
  高清extension_white_list
    %W(CSV)
  结束
结束
 
金山文档使用有感 在线Office办公的效率神器

解决方案

我想你想 File.open方法而不是 File.read 。后者返回原始的字符串,这CarrierWave不知道如何存储

 上传= CsvUploader.new
File.open方法(FILE_PATH)办|文件|
  东西= uploader.store!(文件)
结束
uploader.retrieve_from_store!(self.file_name)
 

这很可能会在文档更清晰,但我通过检查规范的。令人失望的是CarrierWave失败默默的在这里。

CarrierWave has amazing documentation, until you need to do it without a model!

I have my uploader and fog settings set up, and they all work fine when using the mounted uploader on a model, but now I want to do it without a model.

I have this:

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

When I call .store! the code runs immediately which is weird since it should take a few seconds to upload the file?

Then after I call .retrieve_from_store! the uploader object has all the correct S3 info, like the full urls and stuff.

However, calling:

uploader.file.exists?

returns false. And browsing the s3 urls return a key not found error from s3.

So, what am I doing wrong?? To reiterate, it works when mounted so I don't think it's my fog settings.

My uploader:

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

解决方案

I think you want File.open instead of File.read. The latter returns a raw string, which CarrierWave doesn't know how to store.

uploader = CsvUploader.new
File.open(file_path) do |file|
  something = uploader.store!(file)
end
uploader.retrieve_from_store!(self.file_name)

This could probably be clearer in the docs, but I confirmed it by checking the specs. Bummer that CarrierWave is failing silently here.

 
精彩推荐
图片推荐