2014-03-19 45 views
1

我正在將新回形針附件上傳到AWS S3。但是我需要將文件系統上的很多舊映像遷移到S3。我正在嘗試這個耙子任務,但我無法弄清楚我應該如何在S3上提供剷鬥路徑。將舊回形針圖像從文件系統遷移到S3

task :copy_paperclip_data => :environment do 
    @toycars = ToyCar.find :all 
    @toycars.each do |toycar| 
    unless toycar.image_file_name.blank? 
     filename = Rails.root.join('public', 'system', 'images', toycar.id.to_s, 'original', toycar.image_file_name) # this allows me to change path on filesystem, but i want to make it upload on s3 

     if File.exists? filename 
     image = File.new filename 
     toycar.image = image 
     toycar.save 

     image.close 
     end 
    end 
    end 
end 
+0

你看的AWS s3寶石:http://amazon.rubyforge.org? –

+0

@ArvindMehra嗨,你能告訴我你爲什麼搬到S3,使用文件系統不是一個好主意嗎? – medBo

回答

0

解決它......我改變了我的rake任務到這一點,我又救了我的每一個ToyCar物體圖像和它的工作....

namespace :migrate_to_s3 do 

    desc "Upload images to S3" 
    task :toy_cars => :environment do 
    toy_cars = ToyCar.all 
    toy_cars.each do |car| 
     image = Dir["#{Rails.root}/public/system/#{car.logo.path}"].first 
     if image.present? 
     car.update(:logo => File.open(image)) 
     puts "uploading" 
     end 
    end 
    end 

end 
相關問題