繁体   English   中英

更改attachment_fu的存储方案涉及什么?

[英]What is involved with changing attachment_fu's storage scheme?

我有一个使用attachment_fu的rails应用程序。 目前,它正在使用:file_system进行存储,但我想将其更改为:s3 ,以便在更多文件上传时允许更好的扩展。

这涉及到什么? 我想如果我只是将代码切换为使用:s3 ,那么所有旧链接都将被破坏。 我是否只需要将现有文件从文件系统复制到S3? 谷歌搜索在这个主题上没有太多关注。

我宁愿将现有文件移到S3,所以一切都在同一个地方,但如果有必要,旧文件可以保持原样,只要新文件转到S3。

编辑:所以,它并不像将文件复制到S3那么简单; URL使用不同的方案创建。 当它们存储在:file_system ,文件最终会出现在/public/photos/0000/0001/file.name这样的位置,但是:s3的相同文件最终可能会以0/1 / file.name结尾。 我认为它是使用id的东西,只是用零填充(或不填充),但我不确定。

那是对的。 使用:file_system存储填充ID。 您可以更改s3后端模块以使用填充数字,而不是重命名所有文件。

file_system_backend.rb复制partitioned_path方法并将其放在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.rbfull_filename方法以使用partitioned_path

    def full_filename(thumbnail = nil)
      File.join(base_path, *partitioned_path(thumbnail_name_for(thumbnail)))
    end

attachment_fu现在将创建与file_system后端具有相同名称的路径,因此您只需将文件复制到s3而无需重命名所有内容。

除了nilbus的答案之外,我还必须修改s3_backend.rbbase_path方法以返回一个空字符串,否则它会插入attachment_path_id两次:

def base_path
  return ''
end

除了nilbus的答案之外,对我来说有用的是修改s3_backend.rb的base_path方法仍然使用path_prefix(默认情况下是表名):

def base_path
  attachment_options[:path_prefix]
end

而且,我必须从file_system_backend.rb获取attachment_path_id并替换s3_backend.rb中的attachment_path_id,因为否则partitioned_path总是认为我的主键是一个字符串:

def attachment_path_id
  ((respond_to?(:parent_id) && parent_id) || id) || 0
end

感谢所有那些帮助很多的回复。 它也适用于我,但我必须这样做才能使:thumbnail_class选项有效:

def full_filename(thumbnail = nil)
  prefix = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
  File.join(prefix, *partitioned_path(thumbnail_name_for(thumbnail)))
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM