开发者

moving files from virtual server to cloud , in rails

开发者 https://www.devze.com 2023-03-07 18:48 出处:网络
I have a ruby on rails project I have some few thousand files associated with the site. Is there a simple way to move all the files from virtual host to cloud (amazon).

I have a ruby on rails project I have some few thousand files associated with the site. Is there a simple way to move all the files from virtual host to cloud (amazon).

Basically I hav开发者_运维技巧e a lot of images and I am using paperclip gem to store the images.There are already a few users and have thousands of images . Now I have planned to migrate to cloud(amazon). I have used aws-s3 gem for this purpose and it works fine for new images.

But How do I put the old images which were already uploaded ?

Do I have to manually zip and reupload ? or is there a better way ?

Thank you


Hi I would right a simple rake task to accomplish this. It would look like something like this (not tested):

desc "port files to s3"
task :port => :environment do
    AWS::S3::Base.establish_connection!(
        :access_key_id => S3_CONFIG['access_key_id'],
        :secret_access_key => S3_CONFIG['secret_access_key']
    )
    Images.all.each do |image|
      new_image_path = "/images/#{image.id}/#{image.file_name_with_extension}"
      AWS::S3::Object.store(new_image_path,open(image.current_path_to_image),S3_CONFIG['bucket_name'],:access => :public_read)  
    end
end

A few notes: if you are using paperclip to manage and access the files you will want to store them in s3 so paperclip can access them. To do that use Paperclip::Interpolations.interpolate method. like:

new_image_path = Paperclip::Interpolations.interpolate("/accounts/:account_id/images/:id/:style/:basename.:extension",image,:small)

And this goes without saying -- make sure you test this in a staging environment!

Good luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消