There are a couple of versions of this script floating around. My inspiration has been this great article
I went through their code and found most of it is not needed and it is a relict of days long gone. Now ActiveStorage has beautiful APIs and its all quite easy.
- create a new service configuration for S3 named it amazon
- backup everything!!!
- switch your default service over to :amazon so new attachments are created there
- run this task to copy over the blobs to S3
- remove the old data in rails-app/storage/* (keep the backup!)
You can run this task repeatedly and it will only touch blobs that are in the wrong place (repeatable).
Using in-memory buffer (StringIO) instead of creating a milion Tempfiles (faster).
Does not delete original data so if you have a problem the files are still there and hopefully you made a backup of your db (safe).