这是正确的。id 使用 :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)))
end
attach_fu 现在将创建与 file_system 后端名称相同的路径,因此您只需将文件复制到 s3 而无需重命名所有内容。