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

前端 未结 4 791
南旧
南旧 2021-02-06 02:31

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 lo

4条回答
  •  情话喂你
    2021-02-06 03:20

    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)

    1. Figure out and setup your assets bucket, bucket policies etc for making the assets publicly accessible.
    2. 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.
    3. In your app, setup carrierwave and update your models/uploaders for :fog storage.
    4. 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.

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

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

    7. (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.

提交回复
热议问题