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?
返回假。浏览 s3 url 会从 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 不知道如何处理store
.
uploader = CsvUploader.new
File.open(file_path) do |file|
something = uploader.store!(file)
end
uploader.retrieve_from_store!(self.file_name)
这在文档中可能更清楚,但我通过检查确认了这一点specs。令人遗憾的是 CarrierWave 在这里默默地失败了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)