The article shows how to restructure/migrate your files using simple rake task.
I’m on a project which was started uploading files with rails for google drive, but each call to Drive API costs between one and two seconds, the project starts growing (imagine a call with 50 files), so we decide to migrate for amazon s3. It will continue to use paperclip gem but not the Google Drive API anymore.
One problem is, paperclip use to store files on amazon with the following pattern: class/column/000/000/id/original/file_name, but the previously plugin use to use another one (id_filename), so I need to restructure a thousand of files. We could override the property path (more information here), but while the project grew it could be a big problem maintain all files in the same folder in the future.
My first thought was use shell script, and I knew could make it in one line, maybe two. But I remember to have several different classes which stay on the same folder in the actual project, so I need iterate in all file classes to send each to the right directory. So I wrote a little script which can make the work:
If your classes are ContractFile and ClientFile and all your paperclip fields are called documents and your google drive folder need be on home directory. You can call like that:
The script will iterate over the classes and parses the old file structure to a new one, first creating the structure, after copying the file and the last rename it (maybe you not need the last action). The commands are similar the Linux ones.
You can find the script in Gist too, feel free to use and edit it.
That’s all folks.