将旧的回形针图像从文件系统迁移到S3

时间:2014-03-19 12:00:03

标签: ruby-on-rails amazon-s3 paperclip

我正在向AWS S3上传新的回形针附件。但是我在文件系统上有很多旧图像需要迁移到S3。我正在尝试这个rake任务,但我无法弄清楚我应该如何提供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

1 个答案:

答案 0 :(得分: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