我使用Carrierwave和Fog gems将文件存储到我的Amazon S3 bucket(到/files/file_id.txt
)。我需要将稍微不同版本的文件存储到桶(/files/file_id_processed.txt
)中的不同位置,同时(在原始存储之后)。我不想在模型上为它创建一个单独的uploader属性-还有其他方法吗?
这是我当前存储文件的方法:
def store_file(document)
file_name = "tmp/#{document.id}.txt"
File.open(file_name, 'w') do |f|
document_content = document.content
f.puts document_content
document.raw_export.store!(f)
document.save
end
# I need to store the document.processed_content
File.delete(file_name) if File.exist?(file_name)
end
这是文档模型:
class Document < ActiveRecord::Base
mount_uploader :raw_export, DocumentUploader
# here I want to avoid adding something like:
# mount_uploader :processed_export, DocumentUploader
end
这是我的Uploader类:
class DocumentUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"files/"
end
def extension_white_list
%w(txt)
end
end
这是我的最终解决方案看起来像(有点)-基于Nitin Verma的答案:
我必须在Uploader类中添加一个版本的自定义处理器方法:
# in document_uploader.rb
...
version :processed do
process :do_the_replacements
end
def do_the_replacements
original_content = @file.read
File.open(current_path, 'w') do |f|
f.puts original_content.gsub('Apples','Pears')
end
end
考虑到您需要类似但名称不同的文件。为此,您需要在上传器中为文件创建一个版本。
version :processed do
process
end
,现在第二个文件名将被处理_{origional_file}.extension。如果你想改变第二个文件的文件名,你可以使用这个链接https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Customize-your-version-file-names