我有一个使用attachment_fu的rails应用程序。目前,它正在使用:file_system作为存储,但我希望将其更改为:s3,以便在上传更多文件时进行更好的缩放。
这是怎么回事?我想,如果我只是切换代码使用:s3,所有旧的链接将被打破。是否只需要将现有文件从文件系统复制到S3?谷歌搜索在这个话题上还没有出现多少。
我更愿意将现有的文件移到S3,这样所有的文件都在同一个位置,但如果有必要,只要新文件转到S3,旧文件就可以保留在原来的位置。
编辑:所以,它不像将文件复制到S3那么简单;URL是使用不同的方案创建的。当它们存储在:file_system中时,这些文件最终会出现在/public/照片/0000/0001/file.name这样的地方,但是:s3中的相同文件可能会以0/1/file.name结尾。我认为它是在使用id之类的东西,只是用零填充(或不填充),但我不确定。
发布于 2009-11-03 19:24:59
是这样的。ids使用:file_system存储填充。与其重命名所有文件,您还可以修改s3后端模块以使用填充号。
从partitioned_path复制file_system_backend.rb方法,并将其放入s3_backend.rb中。
def partitioned_path(*args)
if respond_to?(:attachment_options) && attachment_options[:partition] == false
args
elsif attachment_options[:uuid_primary_key]
# Primary key is a 128-bit UUID in hex format. Split it into 2 components.
path_id = attachment_path_id.to_s
component1 = path_id[0..15] || "-"
component2 = path_id[16..-1] || "-"
[component1, component2] + args
else
path_id = attachment_path_id
if path_id.is_a?(Integer)
# Primary key is an integer. Split it after padding it with 0.
("%08d" % path_id).scan(/..../) + args
else
# Primary key is a String. Hash it, then split it into 4 components.
hash = Digest::SHA512.hexdigest(path_id.to_s)
[hash[0..31], hash[32..63], hash[64..95], hash[96..127]] + args
end
end
end修改s3_backend.rb的full_filename方法以使用partitioned_path。
def full_filename(thumbnail = nil)
File.join(base_path, *partitioned_path(thumbnail_name_for(thumbnail)))
endattachment_fu现在将使用与file_system后端相同的名称创建路径,因此您只需将文件复制到s3中,而无需重新命名所有内容。
发布于 2011-01-21 14:35:47
除了nilbus的答案之外,我还必须修改s3_backend.rb的base_path方法以返回空字符串,否则它将插入attachment_path_id两次:
def base_path
return ''
end发布于 2011-05-12 21:09:02
除了nilbus的答案之外,对我起作用的是修改S3_backend.rb的base_path方法以仍然使用path_prefix (默认情况下是表名):
def base_path
attachment_options[:path_prefix]
end此外,我还必须从attachment_path_id中提取file_system_backend.rb并替换s3_backend.rb中的一个,否则partitioned_path总是认为我的主键是一个字符串:
def attachment_path_id
((respond_to?(:parent_id) && parent_id) || id) || 0
endhttps://stackoverflow.com/questions/1288856
复制相似问题