什么是涉及改变attachment_fu的存储方案? [英] What is involved with changing attachment_fu's storage scheme?

查看:248
本文介绍了什么是涉及改变attachment_fu的存储方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用attachment_fu Rails应用程序。目前,它是使用:将file_system 存储,但我想将其更改为:S3 ,以便更好地扩展为多个文件要上载。

I have a rails application that is using attachment_fu. Currently, it is using :file_system for storage, but I want to change it to :s3, to allow for better scaling as more files get uploaded.

什么是参与呢?我想,如果我只是切换code使用:S3 ,所有的旧链接将被打破。我需要的只是现有文件从文件系统到S3复制?谷歌搜索并没有打开了太多的话题。

What is involved with this? I imagine that if I just switch the code to use :s3, all the old links will be broken. Do I need to just copy the existing files from the file system to S3? A google search hasn't turned up much on the topic.

我会preFER过度到S3移动现有的文件,所以一切都在同一个地方,但如果需要,旧的文件可以留他们在哪里,只要新的进入S3。

I would prefer to move the existing files over to S3, so everything is in the same place, but if necessary, the old files can stay where they are, as long as new ones go to S3.

编辑:所以,它不是简单地复制过的文件到S3;使用不同的方案正在创建的网址。当它们被存储在:将file_system ,该文件最终会像/public/photos/0000/0001/file.name的地方,但在同一个文件:S3 可能会终止于0/1 / file.name。我认为这是使用id的东西,只是填充它(或没有)零,但我不知道这一点。

So, it is not as simple as copying over the files to S3; the URLs are created using a different scheme. When they are stored in :file_system, the files end up in places like /public/photos/0000/0001/file.name, but the same file in :s3 might end up in 0/1/file.name. I think it is using the id something, and just padding it (or not) with zeros, but I'm not sure of that.

推荐答案

这是正确的。 file_system的存储:ID被用填充。 相反,重命名所有的文件,你可以改变S3后端模块使用填充数字为好。

That's correct. The ids are padded using :file_system storage. Instead of renaming all your files, you can alter the s3 backend module to use padded numbers as well.

复制 partitioned_pa​​th 方法从 file_system_backend.rb ,并把它放在 s3_backend.rb

Copy the partitioned_path method from file_system_backend.rb and put it in 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_pa​​th

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

attachment_fu将立即创建路径使用相同的名称,因为它与file_system的后台做,这样你就可以将文件直接复制在没有重命名一切S3。

attachment_fu will now create paths with the same names as it did with the file_system backend, so you can just copy your files over to s3 without renaming everything.

这篇关于什么是涉及改变attachment_fu的存储方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆