Search code examples
ruby-on-railsamazon-s3rails-activestorageruby-on-rails-7

Can I switch to Amazon S3 later in Rails 7 after using local storage?


If I'm using the server's local storage for storing images, can I access those local images later when I switch to Amazon S3 plan?


Solution

  • If you're using Active Storage, then you're in luck.

    Rails allows you to define mirror service, which is intended to be used during migrations between services

    A mirror service is intended to be used temporarily during a migration between services in production. You can start mirroring to a new service, copy pre-existing files from the old service to the new, then go all-in on the new service.

    It also mentions a pitfall you need to be careful about:

    Mirroring is not atomic. It is possible for an upload to succeed on the primary service and fail on any of the subordinate services. Before going all-in on a new service, verify that all files have been copied.

    And here's a discussion that should help you out with the synchronization of all the objects that have been uploaded before you've enabled Mirrored service: How to sync new ActiveStorage mirrors?

    To summarize the process would roghly look like this:

    Let's assume you're using Disk, and want to migrate to S3

    1. Add new storage S3 as a mirror (from now on, Rails would schedule a backend job to upload objects to the mirror after successful upload to the primary storage)
    2. Copy objects uploaded before the mirror was up (as per mentioned SO discussion)
    3. verify two storage backends are in sync
    4. switch mirror to become the primary (and primary to become a mirror): S3 primary, Disk: mirror
    5. verify there are no objects in Disk that are missing in S3 (copy them if there are any)
    6. drop the Disk mirror.