How can I migrate CarrierWave files to a new storage mechanism?

activerecord, carrierwave, ruby-on-rails

Solution

Minimal to Possibly Zero Donwtime Procedure

In my opinion, the easiest and fastest way to accomplish what you want with almost no downtime is this: (I will assume that you will use AWS cloud, but similar procedure is applicable to any cloud service)

- Figure out and setup your assets bucket, bucket policies etc for making the assets publicly accessible.

- Using `s3cmd` (command line tool for interacting with S3) or a GUI app, copy entire assets folder from file system to the appropriate folder in S3.

- In your app, setup carrierwave and update your models/uploaders for `:fog` storage.

Do not restart your application yet. Instead bring up rails console and for your models, check that the new assets URL is correct and accessible as planned. For example, for a video model with picture asset, you can check this way:

Video.first.picture.url

This will give you a full cloud URL based on the updated settings. Copy the URL and paste in a browser to make sure that you can get to it fine.

If this works for at least one instance of each model that has assets, you are good to restart your application.

Upon restart, all your assets are being served from cloud, and you didn't need any migrations or multiple uploaders in your models.

(Based on comment by @Frederick Cheung): Using `s3cmd` (or something similar) `rsync` or `sync` the assets folder from the filesystem to S3 to account for assets that were uploaded between steps 2 and 5, if any.

PS: If you need help setting up carrierwave for cloud storage, let me know.

Problem

I have a Ruby on Rails site with models using CarrierWave for file handling, currently using local storage. I want to start using cloud storage and I need to migrate existing local files to the cloud. I am wondering if anyone can point out a method for doing this? Bonus points for using a model attribute that would allow me to do this row-by-row in the background without interrupting my site for extended downtime (in other words, some model rows would still have local storage while others used cloud storage). My first instinct is to create a new uploader for each model that uses cloud storage, so I have two uploaders on each model, then transferring the files from one to the other, setting an attribute to indicate which file should be used until they are all transferred, then removing the old uploader. That seems a little excessive.

Original source