2009-11-01 54 views
1

我正在运行一个Rails应用程序,使用Paperclip来处理文件附件和图像大小调整等。该应用程序当前托管在EngineYard云上,并且所有附件都存储在他们的EBS中。考虑使用S3来处理所有Paperclip附件。将数据从EC2转换为S3?

有没有人知道这种迁移的好方法?非常感谢!

回答

3

您可以处理一个rake任务,该任务遍历您的附件并将它们推送到S3。我一会儿用attachment_fu来使用这个 - 不会有太大的不同。这使用aws-s3宝石。

基本过程:1。 从数据库中选择文件需要它们被移动 2.按到S3 3.更新数据库,以反映该文件不再本地存储(这样你可以做他们分批并且不需要担心两次推送相同的文件)。

@attachments = Attachment.stored_locally 
@attachments.each do |attachment| 

    base_path = RAILS_ROOT + '/public/assets/' 
    attachment_folder = ((attachment.respond_to?(:parent_id) && attachment.parent_id) || attachment.id).to_s 
    full_filename = File.join(base_path, ("%08d" % attachment_folder).scan(/..../), attachment.filename) 
    require 'aws/s3' 

    AWS::S3::Base.establish_connection!(
    :access_key_id  => S3_CONFIG[:access_key_id], 
    :secret_access_key => S3_CONFIG[:secret_access_key] 
) 

    AWS::S3::S3Object.store(
    'assets/' + attachment_folder + '/' + attachment.filename, 
    File.open(full_filename), 
    S3_CONFIG[:bucket_name], 
    :content_type => attachment.content_type, 
    :access => :private 
) 

    if AWS::S3::Service.response.success? 
    # Update the database 
    attachment.update_attribute(:stored_on_s3, true) 

    # Remove the file on the local filesystem 
    FileUtils.rm full_filename 

    # Remove directory also if it is now empty 
    Dir.rmdir(File.dirname(full_filename)) if (Dir.entries(File.dirname(full_filename))-['.','..']).empty? 
    else 
    puts "There was a problem uploading " + full_filename 
    end 
end 
3

,我发现自己在同样的情况,把bensie的代码,并使其成为自己的工作 - 这是我想出了:

require 'aws/s3' 

# Ensure you do the following: 
# export AMAZON_ACCESS_KEY_ID='your-access-key' 
# export AMAZON_SECRET_ACCESS_KEY='your-secret-word-thingy' 
AWS::S3::Base.establish_connection! 


@failed = [] 
@attachments = Asset.all # Asset paperclip attachment is: has_attached_file :attachment.... 
@attachments.each do |asset| 
    begin 
    puts "Processing #{asset.id}" 
    base_path = RAILS_ROOT + '/public/' 
    attachment_folder = ((asset.respond_to?(:parent_id) && asset.parent_id) || asset.id).to_s 
    styles = asset.attachment.styles.keys 
    styles << :original 
    styles.each do |style| 
     full_filename = File.join(base_path, asset.attachment.url(style, false)) 


     AWS::S3::S3Object.store(
     'attachments/' + attachment_folder + '/' + style.to_s + "/" + asset.attachment_file_name, 
     File.open(full_filename), 
     "swellnet-assets", 
     :content_type => asset.attachment_content_type, 
     :access => (style == :original ? :private : :public_read) 
    ) 

     if AWS::S3::Service.response.success?   
     puts "Stored #{asset.id}[#{style.to_s}] on S3..." 
     else 
     puts "There was a problem uploading " + full_filename 
     end 
    end 
    rescue 
    puts "Error with #{asset.id}" 
    @failed << asset.id 
    end 
end 

puts "Failed uploads: #{@failed.join(", ")}" unless @failed.empty? 

当然,如果你有多个型号,你会需要根据需要进行调整...