2009-08-17 5 views
2

私はattachment_fuを使用しているレールアプリケーションを持っています。現在、ストレージには:file_systemが使用されていますが、アップロードするファイルが増えるほどスケーリングが改善されるように、:s3に変更します。attachment_fuのストレージスキームの変更には何が関係していますか?

これには何が関係していますか? :s3を使用するようにコードを切り替えるだけで、すべての古いリンクが壊れてしまうと思います。既存のファイルをファイルシステムからS3にコピーするだけですか? Googleの検索ではその話題はあまり出てこなかった。

私は既存のファイルをS3に移動することをお勧めします。すべて同じ場所にありますが、必要に応じて新しいファイルがS3に移動する限り、古いファイルはそのまま残ります。

EDIT:したがって、ファイルをS3にコピーするほど簡単ではありません。異なるスキームを使用してURLが作成されます。 :file_systemに格納されると、ファイルは/public/photos/0000/0001/file.nameのような場所にありますが、:s3の同じファイルは0/1/file.nameになります。私はそれがidを何か使っていると思うし、ちょうど0でそれを埋める(またはそうではない)が、私はそれを確信していない。

答えて

4

これは正しいです。これらの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 

partitioned_pathを使用するために修正s3_backend.rbfull_filename方法。

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

attachment_fuは、今ではFILE_SYSTEMバックエンドで行ったのと同じ名前を持つパスが作成されますので、あなただけのすべての名前を変更せずにS3に上にファイルをコピーすることができます。 nilbusに加えて

2

それ以外の場合は二回attachment_path_idを挿入する、空の文字列を返すためのbase_path方法答えは、私がs3_backend.rbを変更する必要がありました」:nilbus年代に加えて、私のために働いた何

def base_path 
    return '' 
end 
2

答えは、まだ(デフォルトでは、テーブルの名前です)path_prefixを使用するs3_backend.rbのbase_path方法変更しました:

def base_path 
    attachment_options[:path_prefix] 
end 

をし、また、私はを取らなければなりませんでしたfile_system_backend.rbからとそうでないpartitioned_pathはいつも私の主キーが文字列だと思っているので、s3_backend.rbに1を置き換えます。たくさん助けすべてのそれらの応答の

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

感謝。それは私のために働いたが、: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 
関連する問題