在 Rails 中将文件从虚拟服务器移动到云

发布于 2024-11-08 20:10:32 字数 246 浏览 0 评论 0原文

我有一个 ruby​​ on Rails 项目 我有几千个与该网站相关的文件。 有没有一种简单的方法可以将所有文件从虚拟主机移动到云(亚马逊)。

基本上我有很多图像,我使用回形针 gem 来存储图像。已经有一些用户并拥有数千张图像。现在我计划迁移到云(亚马逊)。我已将 aws-s3 gem 用于此目的,它对于新图像效果很好。

但是如何放置已经上传的旧图像?

我必须手动压缩并重新上传吗?或者有更好的方法吗?

谢谢

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 have 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ζ澈沫 2024-11-15 20:10:32

嗨,我会做一个简单的耙子任务来完成这个任务。它看起来像这样(未经测试):

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

一些注意事项:
如果您使用回形针来管理和访问文件,您将希望将它们存储在 s3 中,以便回形针可以访问它们。为此,请使用 Paperclip::Interpolations.interpolate 方法。就像:

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

这是不言而喻的——确保你在临时环境中测试它!

祝你好运。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文