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.
I'd try the following steps:
rails g migration MigrateFiles
to let carrierwave get the current files, process them and upload them to the cloud.If your model looks like this:
The migration would look like this:
If you execute this migration the following should happen:
Carrierwave downloads each image because you specified a remote url for the attachment(the current location, like http://test.com/images/1.jpg) and saves it to the cloud because you changed that in the uploader.
Edit:
Since San pointed out this will not work directly you should maybe create an extra column first, run a migration to copy the current attachment_urls from all the videos into that column, change the uploader after that and run the above migration using the copied urls in that new column. With another migration just delete the column again. Not that clean and easy but done in some minutes.
I have migrated the Carrier wave files to Amazon s3 with s3cmd and it works.
Here are the steps to follow:
sudo apt-get install s3cmd
s3cmd --configure
. You would need to enter public and secret key here, provided by Amazon.s3cmd sync /path_to_your_files ://bucket_name/
--acl-public
to upload the file as public and avoid permission issues.Notes:
sync
will not duplicate your records. It will first check if the file is present on remote server or not.When we use Heroku, most of people suggest to use cloudinary. Free and simple setup. My case is when we use cloudinary service and need move into aws S3 for some reasons.
This is what i did with the uploader:
also, setup the rake task:
The trick is how to change the uploader provider by env variable. Good luck!
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)
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.: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:
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
orsync
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.