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在这里默默地失败了.